a.
int *ptr;
int torf[2][2] = {12, 14, 16};
ptr = torf[0];
b.
int *ptr;
int fort[2][2] = { {12}, {14, 16} };
ptr = fort[0];
a.
b.
a.
int (*ptr) [2];
int torf[2][2] = {12, 14, 16};
ptr = torf;
b.
int (*ptr) [2];
int fort[2][2] = { {12}, {14, 16} };
ptr = fort;
a.
b.
a.
b.
c.
&grid[0][0] or grid[0] or int * grid(這里grid[0]是整數元素grid[0][0]的地址,grid是具有100個元素的數組grid[0]的地址。這兩個地址具有相同的數值但是類型不同,類型指派可以使他們的類型相同)。
a.
b.
c.
d.
e.
a.
b.
c.
a.
b.
c.
a.
b.
編程練習(哈哈哈?。。☆}目感覺越來越簡單了呢!除了最后一題外,好高興?。。。?/span>
1、
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
int main(void)
{
const float rain[YEARS][MONTHS] = {
{4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
{9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
{7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
{7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
};
int year, month;
float subtot, total;
const float (*po) [MONTHS] = rain;
printf(" YEAR RAINFALL (inches) \n");
for(year = 0, total = 0; year < YEARS; year++)
{
for(month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(po + year) + month);
printf("%5d %15.1f\n", 2000 + year, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
printf("MONTHLY AVERAGES: \n\n");
printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf("Nov Dec\n");
for(month = 0; month < MONTHS; month++)
{
for(year = 0, subtot = 0; year < YEARS; year++)
subtot += *(*(po + year) + month);
printf("%4.1f ", subtot/YEARS);
}
printf("\n");
return 0;
}
2、
#include <stdio.h>
void copy_arr(const double sou[], double tar[], int n);
void copy_ptr(const double *sou, double *tar, int n);
int main(void)
{
int i;
const double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
copy_arr(source, target1, 5);
copy_ptr(source, target2, 5);
printf("--------------輸出驗證----------------\n");
for(i = 0; i < 5; i++)
printf("target1[%d] = %.1f\ttarget2[%d] = %.1f\n", i, target1[i], i, target2[i]);
return 0;
}
void copy_arr(const double sou[], double tar[], int n)
{
int i;
for(i = 0; i < n; i++)
tar[i] = sou[i];
}
void copy_ptr(const double *sou, double *tar, int n)
{
int i;
for(i = 0; i < n; i++)
*(tar + i) = *(sou + i);
}
3、
#include <stdio.h>
int get_max(const int *ar, int n);
int main(void)
{
const int source[5] = {16, 2, 78, 990, 123};
printf("--------------輸出驗證----------------\n");
printf("max is %d\n", get_max(source, 5));
return 0;
}
int get_max(const int *ar, int n)
{
int i, max;
max = *ar;
for(i = 1; i < n; i++)
{
max = *(ar + i) > max ? *(ar + i) : max;
}
return max;
}
4、(沒必要做兩次循環,一次循環就夠了,真是寫的羅里吧嗦的?。?br />#include <stdio.h>
int get_max_index(const double *ar, int n);
int main(void)
{
const double source[5] = {16.3, 2.2, 78.78, 990.99, 123};
printf("--------------輸出驗證----------------\n");
printf("index is %d\n", get_max_index(source, 5));
return 0;
}
int get_max_index(const double *ar, int n)
{
int i, index;
double max = *ar;
for(i = 1; i < n; i++)
{
max = *(ar + i) > max ? *(ar + i) : max;
}
for(i = 0; i < n; i++)
{
if(*(ar + i) == max)
{
index = i;
break;
}
}
return index;
}
改進之后的程序代碼:
#include <stdio.h>
#define SIZE 5
int max(double arr[], int n);
int main(void)
{
double source[SIZE] = {1.89, 90.00, 56.78, 789.78, 23.34};
printf("The max index is %d\n", max(source, SIZE));
return 0;
}
int max(double arr[], int n)
{
int i = 0, index;
int max = arr[i];
for(i = 1; i < n; i++)
if(arr[i] > max)
{
max = arr[i];
index = i;
}
return index;
}
5、
#include <stdio.h>
double get_max_min(const double *ar, int n);
int main(void)
{
const double source[5] = {16.3, 2.2, 78.78, 990.99, 123};
printf("--------------輸出驗證----------------\n");
printf("max - min = %.2f\n", get_max_min(source, 5));
return 0;
}
double get_max_min(const double *ar, int n)
{
int i;
double max = *ar;
double min = *ar;
for(i = 1; i < n; i++)
{
max = *(ar + i) > max ? *(ar + i) : max;
min = *(ar + i) < min ? *(ar + i) : min;
}
return max - min;
}
6、
#include <stdio.h>
#define ROWS 3
#define COLS 4
void copy_ptr(double (*sou) [COLS], double (*tar) [COLS], int rows);
int main(void)
{
int i;
int j;
double source[ROWS][COLS] = {
{2.1, 3.4, 78.9, 23.3},
{231.1, 45.5, 34, 12},
{23.7, 567.8, 56.5, 32}
};
double target[ROWS][COLS];
copy_ptr(source, target, ROWS);
printf("--------------Output verification----------------\n");
for(i = 0; i < ROWS; i++)
{
for(j = 0; j < COLS; j++)
printf("%.1f\t", *(*(target + i) + j));
printf("\n");
}
return 0;
}
void copy_ptr(double (*sou) [COLS], double (*tar) [COLS], int rows)
{
int i;
int j;
for(i = 0; i < rows; i++)
{
for(j = 0; j < COLS; j++)
*(*(tar + i) + j) = *(*(sou + i) + j);
}
}
7、
#include <stdio.h>
void copy_ptr(double *sou, double *tar, int n);
int main(void)
{
int i;
double source[7] = {12.12, 23.4, 34.23, 1, 1.2, 5.6, 67.78};
double target[3];
copy_ptr(source, target, 3);
printf("--------------Output verification----------------\n");
for(i = 0; i < 3; i++)
printf("%.2f\n", *(target + i));
return 0;
}
void copy_ptr(double *sou, double *tar, int n)
{
int i;
for(i = 0; i < n; i++)
*(tar + i) = *(sou + i + n - 1);
}
8、
#include <stdio.h>
#define ROWS 3
#define COLS 5
void copy_ptr(double (*sou)[COLS], int n, int m, double tar[n][m]);
void show_arr(int n, int m, double ar[n][m]);
int main(void)
{
double source[ROWS][COLS] = {
{23.12, 45.66, 45.0, 89.9, 77.6},
{11.1, 22.22, 4.45, 34.3, 4},
{22.1, 789.99, 34.23, 12.12, 56}
};
double target[ROWS][COLS];
copy_ptr(source, ROWS, COLS, target);
printf("--------------show array source----------------\n");
show_arr(ROWS, COLS, source);
printf("--------------show array target----------------\n");
show_arr(ROWS, COLS, target);
return 0;
}
void copy_ptr(double (*sou)[COLS], int n, int m, double tar[n][m])
{
int r;
int c;
for(r = 0; r < n; r++)
{
for(c = 0; c < m; c++)
*(*(tar + r) + c) = *(*(sou + r) + c);
}
}
void show_arr(int n, int m, double ar[n][m])
{
int r;
int c;
for(r = 0; r < n; r++)
{
for(c = 0; c < m; c++)
printf("%.2f\t", ar[r][c]);
printf("\n");
}
}
9、
#include <stdio.h>
void sum_array(int *ar1, int *ar2, int *ar3, int n);
int main(void)
{
int i;
int array1[4] = {2, 4, 5, 8};
int array2[4] = {1, 0, 4, 6};
int array3[4];
sum_array(array1, array2, array3, 4);
printf("--------------Output verification----------------\n");
for(i = 0; i < 4; i++)
printf("%d\t", *(array3 + i));
return 0;
}
void sum_array(int *ar1, int *ar2, int *ar3, int n)
{
int i;
for(i = 0; i < n; i++)
*(ar3 + i) = *(ar1 + i) + *(ar2 + i);
}
10、
#include <stdio.h>
#define ROWS 3
#define COLS 5
void show_array(int (*ar)[COLS], int rows);
void double_array(int (*ar)[COLS], int rows);
int main(void)
{
int source[ROWS][COLS] = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5, 6},
{3, 4, 5, 6, 7}
};
printf("--------------show array source----------------\n");
show_array(source, ROWS);
double_array(source, ROWS);
printf("--------------again show array source----------------\n");
show_array(source, ROWS);
return 0;
}
void show_array(int (*ar)[COLS], int rows)
{
int r;
int c;
for(r = 0; r < rows; r++)
{
for(c = 0; c < COLS; c++)
printf("%d\t", *(*(ar + r) + c));
printf("\n");
}
}
void double_array(int (*ar)[COLS], int rows)
{
int r;
int c;
for(r = 0; r < rows; r++)
{
for(c = 0; c < COLS; c++)
*(*(ar + r) + c) *= 2;
}
}
11、(感覺代碼越寫越多了,與之前沒簡便到哪兒去)
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
//對于每一年,計算各月的總降水量并把各個值存儲在一個數組中
void fun1(float (*ye)[MONTHS], float * yea);
// 對于每一年,顯示各月的總降水量
void show_array1(float *ar);
// 計算年降水平均量
float sum1(float *ar);
// 對于每個月,計算月降水平均量并把各個值存儲在一個數組中
void fun2(float (*ye)[MONTHS], float * mon);
// 對于每個月,顯示月降水平均量
void show_array2(float *ar);
int main(void)
{
const float rain[YEARS][MONTHS] = {
{4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
{9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
{7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
{7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
};
float year_rain[YEARS];
float month_rain[MONTHS];
fun1(rain, year_rain);
fun2(rain, month_rain);
printf(" YEAR RAINFALL (inches) \n");
show_array1(year_rain);
printf("\nThe yearly average is %.1f inches.\n\n", sum1(year_rain));
printf("MONTHLY AVERAGES: \n\n");
printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf("Nov Dec\n");
show_array2(month_rain);
printf("\n");
return 0;
}
void fun1(float (*ye)[MONTHS], float * yea)
{
float subtot;
int year, month;
for(year = 0 ; year < YEARS; year++)
{
for(month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(ye + year) + month);
*(yea + year) = subtot;
}
}
void show_array1(float *ar)
{
int i;
for(i = 0; i < YEARS; i++)
printf("%5d %15.1f\n", 2000 + i, *(ar + i));
}
float sum1(float *ar)
{
int i;
float total;
for(i = 0; i < YEARS; i++)
total += *(ar + i);
return total / YEARS;
}
void fun2(float (*ye)[MONTHS], float * mon)
{
int year, month;
float subtot;
for(month = 0; month < MONTHS; month++)
{
for(year = 0, subtot = 0; year < YEARS; year++)
subtot += *(*(ye + year) + month);
*(mon + month) = subtot / YEARS;
}
}
void show_array2(float *ar)
{
int i;
for(i = 0; i < MONTHS; i++)
printf("%4.1f", *(ar + i));
}
有必要寫那么多的函數嗎?只須寫兩個函數就可以搞定的,非得寫那么多,改進之后程序如下:
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
// 計算年降水總量與所有年度的總降水量
double calculate1(const float arr[][MONTHS], int y);
// 計算各年該月份的總降水量
void calculate2(const float arr[][MONTHS], int y);
int main(void)
{
const float rain[YEARS][MONTHS] = {
{4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
{9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
{7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
{7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
};
float total;
printf(" YEAR RAINFALL (inches) \n");
total = calculate1(rain, YEARS);
printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
printf("MONTHLY AVERAGES: \n\n");
printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf("Nov Dec\n");
calculate2(rain, YEARS);
printf("\n");
return 0;
}
double calculate1(const float arr[][MONTHS], int y)
{
int year, month;
double total, subtot;
for(year = 0, total = 0; year < y; year++)
{
for(month = 0, subtot = 0; month < MONTHS; month++)
subtot += arr[year][month];
printf("%5d %15.1f\n", 2000 + year, subtot);
total += subtot;
}
return total;
}
void calculate2(const float arr[][MONTHS], int y)
{
int year, month;
double subtot;
for(month = 0; month < MONTHS; month++)
{
for(year = 0, subtot = 0; year < y; year++)
subtot += arr[year][month];
printf("%4.1f ", subtot/YEARS);
}
}
12、(關于如何輸入數字,沒搞明白,還是借鑒CSDN----vs9841前輩的做法,不過后面都是自己寫了)
#include <stdio.h>
#define ROWS 3
#define COLS 5
// 從鍵盤獲取一個double數
double get_double(void);
// 向source[ROWS][COLS]中輸入數值
void input_double(int n, int m, double (*dou)[COLS]);
// 計算每個數集的平均值
void get_average(int n, int m, double (*dou)[COLS]);
// 計算所有數值的平均值
double get_all_average(int n, int m, double (*dou)[COLS]);
// 找出所有數中的最大值
double get_max(int n, int m, double (*dou)[COLS]);
int main(void)
{
double source[ROWS][COLS];
input_double(ROWS, COLS, source);
printf("----------------------------------------------------\n");
get_average(ROWS, COLS, source);
printf("----------------------------------------------------\n");
printf("Mean values of all the numbers: %5.2f\n", get_all_average(ROWS, COLS, source));
printf("----------------------------------------------------\n");
printf("Maximum value for all: %5.2f\n", get_max(ROWS, COLS, source));
return 0;
}
double get_double(void)
{
double input;
char ch;
while(scanf("%lf", &input) != 1)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a double.\nPlease enter a ");
printf("double value, such as 23.3, -12.1, or 3: \n");
}
return input;
}
void input_double(int n, int m, double (*dou)[COLS])
{
int i, j;
printf("Please enter data of %dx%d two dimensional array\n", n, m);
for(i = 0; i < n; i++)
{
printf("Start with %d sets of numbers: \n", i+1);
for(j = 0; j < m; j++)
{
printf("%d number: ", j+1); // 記住每次只能處理輸入一個數
dou[i][j] = get_double();
}
}
printf("Data entry is complete, as shown below: \n");
for(i = 0; i < n; i++)
{
printf("%d sets of numbers: \n", i+1);
for(j = 0; j < m; j++)
printf("%5.2f\t", dou[i][j]);
printf("\n");
}
}
void get_average(int n, int m, double (*dou)[COLS])
{
int r;
int c;
double total = 0;
for(r = 0; r < n; r++)
{
printf("average of the %d sets of numbers: ", r + 1);
for(c = 0, total = 0; c < m; c++)
total += dou[r][c];
printf("%5.2f\n", total / m);
}
}
double get_all_average(int n, int m, double (*dou)[COLS])
{
int r;
int c;
double total = 0;
for(r = 0; r < n; r++)
{
for(c = 0; c < m; c++)
total += dou[r][c];
}
return total / (n * m);
}
double get_max(int n, int m, double (*dou)[COLS])
{
int r;
int c;
double max = dou[0][0];
for(r = 0; r < n; r++)
{
for(c = 0; c < m; c++)
{
max = max > dou[r][c] ? max : dou[r][c];
}
}
return max;
}
第二次更新如下,完全自己手寫:(可能對自己好理解一點)
#include <stdio.h>
void task_a(double arr[][5], int n);
void task_b(double arr[][5], int n);
double task_c(double arr[][5], int n);
double task_d(double arr[][5], int n);
void task_e(double arr[][5], int n);
int main(void)
{
double array[3][5];
task_a(array, 3);
task_b(array, 3);
printf("所有數值的平均數為:%.2f\n", task_c(array, 3));
printf("這15個數中的最大值為:%.2f\n", task_d(array, 3));
printf("該3x5數組為:\n");
task_e(array, 3);
printf("\n");
return 0;
}
void task_a(double arr[][5], int n)
{
int i = 0;
int count;
double num;
printf("請輸入3個數集\n");
while(i < n)
{
printf("請輸入第%d個數集:\n", i + 1);
count = 0;
printf("請輸入第%d個數:", count + 1);
while(scanf("%lf", &num) == 1 && count < 5)
{
arr[i][count] = num;
if(count == 4)
break;
count++;
printf("請輸入第%d個數:", count + 1);
}
i++;
}
}
void task_b(double arr[][5], int n)
{
double tot;
for(int r = 0; r < n; r++)
{
tot = 0;
for(int c = 0; c < 5; c++)
tot += arr[r][c];
printf("第%d個數集的平均值為: %.2f\n", r + 1, tot / 5);
}
}
double task_c(double arr[][5], int n)
{
double total = 0;
for(int r = 0; r < n; r++)
for(int c = 0; c < 5; c++)
total += arr[r][c];
return total / 15;
}
double task_d(double arr[][5], int n)
{
double max;
max = arr[0][0];
for(int r = 0; r < n; r++)
for(int c = 0; c < 5; c++)
if(arr[r][c] > max)
max = arr[r][c];
return max;
}
void task_e(double arr[][5], int n)
{
for(int r = 0; r < n; r++)
{
for(int c = 0; c < 5; c++)
printf("%.2f ", arr[r][c]);
printf("\n");
}
}
13、
同上,第二次更新如下:
#include <stdio.h>
void task_a(int n, int m, double arr[n][m]);
void task_b(int n, int m, double arr[n][m]);
double task_c(int n, int m, double arr[n][m]);
double task_d(int n, int m, double arr[n][m]);
void task_e(int n, int m, double arr[n][m]);
int main(void)
{
double array[3][5];
task_a(3, 5, array);
task_b(3, 5, array);
printf("所有數值的平均數為:%.2f\n", task_c(3, 5, array));
printf("這15個數中的最大值為:%.2f\n", task_d(3, 5, array));
printf("該3x5數組為:\n");
task_e(3, 5, array);
printf("\n");
return 0;
}
void task_a(int n, int m, double arr[n][m])
{
int i = 0;
int count;
double num;
printf("請輸入3個數集\n");
while(i < n)
{
printf("請輸入第%d個數集:\n", i + 1);
count = 0;
printf("請輸入第%d個數:", count + 1);
while(scanf("%lf", &num) == 1 && count < m)
{
arr[i][count] = num;
if(count == 4)
break;
count++;
printf("請輸入第%d個數:", count + 1);
}
i++;
}
}
void task_b(int n, int m, double arr[n][m])
{
double tot;
for(int r = 0; r < n; r++)
{
tot = 0;
for(int c = 0; c < m; c++)
tot += arr[r][c];
printf("第%d個數集的平均值為: %.2f\n", r + 1, tot / 5);
}
}
double task_c(int n, int m, double arr[n][m])
{
double total = 0;
for(int r = 0; r < n; r++)
for(int c = 0; c < m; c++)
total += arr[r][c];
return total / 15;
}
double task_d(int n, int m, double arr[n][m])
{
double max;
max = arr[0][0];
for(int r = 0; r < n; r++)
for(int c = 0; c < m; c++)
if(arr[r][c] > max)
max = arr[r][c];
return max;
}
void task_e(int n, int m, double arr[n][m])
{
for(int r = 0; r < n; r++)
{
for(int c = 0; c < m; c++)
printf("%.2f ", arr[r][c]);
printf("\n");
}
}
首次做的如下:
#include <stdio.h>
#define ROWS 3
#define COLS 5
// 從鍵盤獲取一個double數
double get_double(void);
// 向source[ROWS][COLS]中輸入數值
void input_double(int n, int m, double dou[n][m]);
// 計算每個數集的平均值
void get_average(int n, int m, double dou[n][m]);
// 計算所有數值的平均值
double get_all_average(int n, int m, double dou[n][m]);
// 找出所有數中的最大值
double get_max(int n, int m, double dou[n][m]);
int main(void)
{
double source[ROWS][COLS];
input_double(ROWS, COLS, source);
printf("----------------------------------------------------\n");
get_average(ROWS, COLS, source);
printf("----------------------------------------------------\n");
printf("Mean values of all the numbers: %5.2f\n", get_all_average(ROWS, COLS, source));
printf("----------------------------------------------------\n");
printf("Maximum value for all: %5.2f\n", get_max(ROWS, COLS, source));
return 0;
}
double get_double(void)
{
double input;
char ch;
while(scanf("%lf", &input) != 1)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a double.\nPlease enter a ");
printf("double value, such as 23.3, -12.1, or 3: \n");
}
return input;
}
void input_double(int n, int m, double dou[n][m])
{
int i, j;
printf("Please enter data of %dx%d two dimensional array\n", n, m);
for(i = 0; i < n; i++)
{
printf("Start with %d sets of numbers: \n", i+1);
for(j = 0; j < m; j++)
{
printf("%d number: ", j+1); // 記住每次只能處理輸入一個數
dou[i][j] = get_double();
}
}
printf("Data entry is complete, as shown below: \n");
for(i = 0; i < n; i++)
{
printf("%d sets of numbers: \n", i+1);
for(j = 0; j < m; j++)
printf("%5.2f\t", dou[i][j]);
printf("\n");
}
}
void get_average(int n, int m, double dou[n][m])
{
int r;
int c;
double total = 0;
for(r = 0; r < n; r++)
{
printf("average of the %d sets of numbers: ", r + 1);
for(c = 0, total = 0; c < m; c++)
total += dou[r][c];
printf("%5.2f\n", total / m);
}
}
double get_all_average(int n, int m, double dou[n][m])
{
int r;
int c;
double total = 0;
for(r = 0; r < n; r++)
{
for(c = 0; c < m; c++)
total += dou[r][c];
}
return total / (n * m);
}
double get_max(int n, int m, double dou[n][m])
{
int r;
int c;
double max = dou[0][0];
for(r = 0; r < n; r++)
{
for(c = 0; c < m; c++)
{
max = max > dou[r][c] ? max : dou[r][c];
}
}
return max;
}