练习题
1.
假设:int a[10];
对于原表达式 sizeof(a) / sizeof(a[0]) ,如果数组的类型变更了,比如从int变成double,对其没有影响
但如果是 sizeof(a) / sizeof(int) ,需要手动变更为:sizeof(a) / sizeof(double) ,会比较麻烦
2.
int digit_count[10];
char ch;
digit_count[ch - '0'] = 0;
3.
bool weekend[7] = { true,false,false,false,false,false,true };
4.
bool weekend[7] = { [0] = true,[6] = true };
5.
#include<stdio.h>
#define N 40
int main(void)
{
int fib_number[N] = { 0,1 };
for (int i = 2; i < N; i++)
{
fib_number[i] = fib_number[i - 1] + fib_number[i - 2];
}
for (int i = 0; i < N; i++)
{
printf("%d ", fib_number[i]);
}
return 0;
}
6.
const int segments[10][7] = { {1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 1},
{0, 1, 1, 0, 0, 1, 1},
{1, 0, 1, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1} };
7.
const int segments[10][7] = { {1, 1, 1, 1, 1, 1},
{0, 1, 1},
{1, 1, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 1},
{0, 1, 1, 0, 0, 1, 1},
{1, 0, 1, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 1},
{1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1} };
8.
double temperature_readings[30][24];
9.
#include<stdio.h>
int main(void)
{
int temperature_readings[30][24];
double sum = 0.0, average = 0.0;
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 24; j++)
{
sum += temperature_readings[i][j];
}
}
average = sum / (30 * 24);
}
10.
char chess_board[8][8] = { {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{' ', '.', ' ', '.', ' ', '.', ' ', '.'},
{'.', ' ', '.', ' ', '.', ' ', '.', ' '},
{' ', '.', ' ', '.', ' ', '.', ' ', '.'},
{'.', ' ', '.', ' ', '.', ' ', '.', ' '},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'} };
11.
#include<stdio.h>
int main(void)
{
char checker_board[8][8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if ((i + j) % 2 == 0)
{
checker_board[i][j] = 'B';
}
else
{
checker_board[i][j] = 'R';
}
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
printf("%c ", checker_board[i][j]);
}
printf("\n");
}
return 0;
}
编程题
1.
#include<stdio.h>
#include<stdbool.h>
int main(void)
{
bool digit_seen[10] = { false }, a[10] = { false };
int digit, n;
printf("Enter a number: ");
scanf_s("%d", &n);
while (n > 0)
{
digit = n % 10;
if (digit_seen[digit] == true)
{
a[digit] = digit_seen[digit];
}
digit_seen[digit] = true;
n /= 10;
}
printf("Repeated digit: ");
for (int i = 0; i < 10; i++)
{
if (a[i] == true)
{
printf("%d ", i);
}
}
return 0;
}
2.
#include<stdio.h>
int main(void)
{
int a[10] = { 0 };
int digit, n;
printf("Enter a number: ");
scanf_s("%d", &n);
while (n > 0)
{
digit = n % 10;
a[digit]++;
n /= 10;
}
printf("Digit:\t\t");
for (int i = 0; i < 9; i++)
{
printf("%d ", i);
}
printf("9\n");
printf("Occurrences: ");
for (int i = 0; i < 10; i++)
{
if (a[i] > 0)
{
printf("%d ", a[i]);
}
else
{
printf("0 ");
}
}
return 0;
}
3.
#include<stdio.h>
#include<stdbool.h>
int main(void)
{
bool digit_seen[10] = { false }, a[10] = { false };
int digit, n = 1;
while (1)
{
printf("Enter a number: ");
scanf_s("%d", &n);
if (n > 0)
{
while (n > 0)
{
digit = n % 10;
if (digit_seen[digit] == true)
{
a[digit] = digit_seen[digit];
}
digit_seen[digit] = true;
n /= 10;
}
}
else
{
break;
}
}
printf("Repeated digit: ");
for (int i = 0; i < 10; i++)
{
if (a[i] == true)
{
printf("%d ", i);
}
}
return 0;
}
4.
#include<stdio.h>
#define N 10
#define SIZE ((int) (sizeof(a)/sizeof(a[0])))
int main(void)
{
int a[N];
printf("Enter %d numbers: ", N);
for (int i = 0; i < SIZE; i++)
{
scanf_s("%d", &a[i]);
}
printf("In rever order: ");
for (int i = SIZE - 1; i >= 0; i--)
{
printf("%d ", a[i]);
}
return 0;
}
5.
#include<stdio.h>
#define NUM_RATES ((int) (sizeof(value)/sizeof(value[0])))
#define INITIAL_BALANCE 100.00
int main(void)
{
int low_rate, num_years, year;
double interest = 0.0;
double value[5];
printf("Enter interest rate: ");
scanf_s("%d", &low_rate);
printf("Enter number of years: ");
scanf_s("%d", &num_years);
printf("\nYears");
for (int i = 0; i < NUM_RATES; i++)
{
printf("%6d%%", low_rate + i);
value[i] = INITIAL_BALANCE;
}
printf("\n");
for (year = 1; year <= num_years; year++)
{
printf("%3d ", year);
for (int i = 0; i < NUM_RATES; i++)
{
interest = value[i] * ((low_rate + i) / 12.0 / 100.0);
value[i] += interest * 12;
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
6.
#include<stdio.h>
#include<ctype.h>
#define N 50
int main(void)
{
char ch, str[N] = { 0 };
printf("Eter message: ");
for (int i = 0; (ch = getchar()) != '\n'; i++)
{
str[i] = ch;
}
printf("In B1FF-speak: ");
for (int i = 0; i < N && str[i] != 0; i++)
{
str[i] = toupper((unsigned char)str[i]);
switch (str[i])
{
case 'A':
printf("4");
break;
case 'B':
printf("8");
break;
case 'E':
printf("3");
break;
case 'I':
printf("1");
break;
case 'O':
printf("0");
break;
case 'S':
printf("5");
break;
default:
printf("%c", str[i]);
break;
}
}
printf("!!!!!!!!!!");
return 0;
}
7.
#include<stdio.h>
int main(void)
{
int m[5][5];
for (int i = 0; i < 5; i++)
{
printf("Enter row %d: ", i + 1);
for (int j = 0; j < 5; j++)
{
scanf_s("%d", &m[i][j]);
printf("\n");
}
}
printf("\n");
printf("Row totals: ");
for (int i = 0, sum = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
sum += m[i][j];
}
printf("%d ", sum);
sum = 0;
}
printf("\n");
printf("Column totals: ");
for (int i = 0, sum = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
sum += m[j][i];
}
printf("%d ", sum);
sum = 0;
}
return 0;
}
8.
#include<stdio.h>
int main(void)
{
int m[5][5];
int min, max;
for (int i = 0; i < 5; i++)
{
printf("Enter student %d grade for five courses: ", i + 1);
for (int j = 0; j < 5; j++)
{
scanf_s("%d", &m[i][j]);
}
}
printf("\n");
for (int i = 0, sum = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
sum += m[i][j];
}
printf("%d student total grade: %d\n", i + 1, sum);
printf("%d student average grade: %.2f\n", i + 1, sum / 5.0);
sum = 0;
}
printf("\n");
for (int i = 0, sum = 0; i < 5; i++)
{
min = m[0][i];
max = m[0][i];
for (int j = 0; j < 5; j++)
{
sum += m[j][i];
if (m[j][i] <= min)
{
min = m[j][i];
}
if (m[j][i] >= max)
{
max = m[j][i];
}
}
printf("%d course average grade: %f\n", i + 1, sum / 5.0);
printf("max: %d\n", max);
printf("min: %d\n", min);
sum = 0;
}
return 0;
}