10-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.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;
printf ( " YEAR RAINFALL(inches)\n" ) ;
for ( year = 0 , total = 0 ; year < YEARS; year++ ) {
for ( month = 0 , subtot = 0 ; month < MONTHS; month++ ) {
subtot += * ( * ( rain + year) + month) ;
}
printf ( "%5d %15.1f\n" , 2010 + year, subtot) ;
total += subtot;
}
printf ( "\nThe yearly average is %.1f inches.\n\n" , total / YEARS) ;
printf ( "MONTHLY AVERAFES:\n\n" ) ;
printf ( " Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n" ) ;
for ( month = 0 ; month < MONTHS; month++ ) {
for ( year = 0 , subtot = 0 ; year < YEARS; year++ ) {
subtot += * ( * ( rain + year) + month) ;
}
printf ( "%5.1f" , subtot / YEARS) ;
}
printf ( "\n" ) ;
return 0 ;
}
10-2
# include <stdio.h>
void copy_arr ( double arr[ ] , double arr2[ ] , int num) ;
void copy_ptr ( double * arr, double * parr, int num) ;
void copy_ptrs ( double * arr, double * parr, double * i) ;
void show ( double arr[ ] , int n) ;
int main ( void ) {
double source[ 5 ] = { 1.1 , 2.2 , 3.3 , 4.4 , 5.5 } ;
double target1[ 5 ] ;
double target2[ 5 ] ;
double target3[ 5 ] ;
copy_arr ( target1, source, 5 ) ;
copy_ptr ( target2, source, 5 ) ;
copy_ptrs ( target3, source, source + 5 ) ;
printf ( "target1:\n" ) ;
show ( target1, 5 ) ;
printf ( "target2:\n" ) ;
show ( target2, 5 ) ;
printf ( "target3:\n" ) ;
show ( target3, 5 ) ;
return 0 ;
}
void copy_arr ( double arr[ ] , double arr2[ ] , int num) {
for ( int i = 0 ; i < num; i++ ) {
arr[ i] = arr2[ i] ;
}
return ;
}
void copy_ptr ( double * arr, double * parr, int num) {
for ( int j = 0 ; j < num; j++ ) {
* ( arr + j) = * ( parr + j) ;
}
return ;
}
void copy_ptrs ( double * arr, double * parr, double * i) {
for ( int n = 4 ; n >= 0 ; n-- ) {
* ( arr + n) = * ( parr + n) ;
}
}
void show ( double arr[ ] , int n) {
for ( int i = 0 ; i < n; i++ ) {
printf ( "%lf," , arr[ i] ) ;
}
printf ( "\n" ) ;
return ;
}
10-3
# include <stdio.h>
int find_max ( int arr[ ] , int i) ;
int main ( void ) {
int arr[ ] = { 1 , 4 , 2 , 5 , 7 } ;
int max = 0 ;
max = find_max ( arr, 5 ) ;
printf ( "最大值为:%d" , max) ;
return 0 ;
}
int find_max ( int arr[ ] , int i) {
int f_max = arr[ 0 ] ;
for ( int n = 0 ; n < i; n++ ) {
if ( arr[ n] >= f_max) {
f_max = arr[ n] ;
}
}
return f_max;
}
10-4
# include <stdio.h>
int Max_index ( double arr[ ] , int n) ;
int main ( void ) {
int max_index = 0 ;
double test_arr[ ] = { 1.1 , 56.2 , 3.4 , 7.8 , 34.7 } ;
max_index = Max_index ( test_arr, 5 ) ;
printf ( "最大值的下标为(从0开始):%d" , max_index) ;
return 0 ;
}
int Max_index ( double arr[ ] , int n) {
int m_index = 0 ;
double max_value = arr[ 0 ] ;
for ( int i = 0 ; i < n; i++ ) {
if ( arr[ i] >= max_value) {
max_value = arr[ i] ;
m_index = i;
}
}
return m_index;
}
10-5
# include <stdio.h>
double D_value ( double arr[ ] , int n) ;
int main ( void ) {
double dif = 0 ;
double arr[ ] = { 1.1 , 2.3 , 5.3 , 1.5 , 7.5 } ;
dif = D_value ( arr, 5 ) ;
printf ( "差值为:%.2lf" , dif) ;
return 0 ;
}
double D_value ( double arr[ ] , int n) {
double max_v = arr[ 0 ] ;
double min_v = arr[ 0 ] ;
double difference = 0 ;
for ( int i = 0 ; i < n; i++ ) {
if ( arr[ i] >= max_v) {
max_v = arr[ i] ;
}
if ( arr[ i] <= min_v) {
min_v = arr[ i] ;
}
}
difference = max_v - min_v;
return difference;
}
10-6
# include <stdio.h>
void Reverse ( double arr[ ] , int n) ;
void show ( double arr[ ] , int nu) ;
int main ( void ) {
double test[ ] = { 1.2 , 3.4 , 7.6 , 3.6 , 8.9 } ;
double test2[ ] = { - 2.1 , 2.4 , 3.5 , 67.8 , 9.7 , 4.5 } ;
printf ( "原来:\n" ) ;
show ( test, 5 ) ;
show ( test2, 6 ) ;
Reverse ( test, 5 ) ;
Reverse ( test2, 6 ) ;
printf ( "现在:\n" ) ;
show ( test, 5 ) ;
show ( test2, 6 ) ;
return 0 ;
}
void Reverse ( double arr[ ] , int n) {
double temp = arr[ 0 ] ;
for ( int i = 0 ; i < n / 2 ; i++ ) {
temp = arr[ i] ;
arr[ i] = arr[ n - i - 1 ] ;
arr[ n - i - 1 ] = temp;
}
return ;
}
void show ( double arr[ ] , int nu) {
for ( int j = 0 ; j < nu; j++ ) {
printf ( "%lf," , arr[ j] ) ;
}
printf ( "\n" ) ;
return ;
}
10-7
# include <stdio.h>
# define MONTHS 12
# define YEARS 5
void copy_arr ( double arr[ ] , double arr2[ ] , int num) ;
int main ( void ) {
double arr1[ YEARS] [ MONTHS] = {
{ 4.3 , 4.3 , 4.3 , 3.0 , 2.0 , 1.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 }
} ;
double arr2[ YEARS] [ MONTHS] ;
for ( int i = 0 ; i < YEARS; i++ ) {
copy_arr ( arr2[ i] , arr1[ i] , MONTHS) ;
}
for ( int x = 0 ; x < YEARS; x++ ) {
for ( int y = 0 ; y < MONTHS; y++ ) {
printf ( "%lf," , arr2[ x] [ y] ) ;
}
printf ( "\n" ) ;
}
return 0 ;
}
void copy_arr ( double arr[ ] , double arr2[ ] , int num) {
for ( int i = 0 ; i < num; i++ ) {
arr[ i] = arr2[ i] ;
}
return ;
}
10-8
# include <stdio.h>
void copy_arr ( double arr[ ] , double arr2[ ] , int num) ;
void show ( double arr[ ] , int n) ;
int main ( void ) {
double arr1[ ] = { 1.2 , 3.4 , 2.5 , 6.5 , 1.4 , 7.5 , 8.4 } ;
double arr2[ 3 ] ;
copy_arr ( arr2, & arr1[ 2 ] , 3 ) ;
show ( arr2, 3 ) ;
return 0 ;
}
void copy_arr ( double arr[ ] , double arr2[ ] , int num) {
for ( int i = 0 ; i < num; i++ ) {
arr[ i] = arr2[ i] ;
}
return ;
}
void show ( double arr[ ] , int n) {
for ( int j = 0 ; j < n; j++ ) {
printf ( "%lf," , arr[ j] ) ;
}
printf ( "\n" ) ;
return ;
}
10-9
# include <stdio.h>
void copy_arr ( int n, double arr[ ] [ 5 ] , double arr2[ ] [ 5 ] ) ;
void show ( int n, double arr[ ] [ 5 ] ) ;
int main ( void ) {
double arr[ 3 ] [ 5 ] = {
{ 2.3 , 1.4 , 6.4 , 3.5 , 8.3 } ,
{ 3.4 , 1.5 , 3.9 , 4.5 , 6.7 } ,
{ 4.5 , 3.2 , 1.6 , 6.3 , 9.9 }
} ;
double arr2[ 3 ] [ 5 ] = { 0 , 0 } ;
printf ( "没拷贝前:\n" ) ;
show ( 3 , arr2) ;
copy_arr ( 3 , arr, arr2) ;
printf ( "拷贝后:\n" ) ;
show ( 3 , arr2) ;
return 0 ;
}
void copy_arr ( int n, double arr[ ] [ 5 ] , double arr2[ ] [ 5 ] ) {
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < 5 ; j++ ) {
arr2[ i] [ j] = arr[ i] [ j] ;
}
}
}
void show ( int n, double arr[ ] [ 5 ] ) {
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < 5 ; j++ ) {
printf ( "%lf," , arr[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
}
10-10
# include <stdio.h>
void sum_arr ( double arr1[ ] , double arr2[ ] , double arr3[ ] , int n) ;
void show ( double arr[ ] , int n) ;
int main ( void ) {
double arr1[ ] = { 1.1 , 2.2 , 3.3 , 4.4 } ;
double arr2[ ] = { 2.2 , 3.3 , 4.4 , 5.5 } ;
double arr3[ 4 ] = { 0 } ;
sum_arr ( arr1, arr2, arr3, 4 ) ;
show ( arr3, 4 ) ;
return 0 ;
}
void sum_arr ( double arr1[ ] , double arr2[ ] , double arr3[ ] , int n) {
for ( int i = 0 ; i < n; i++ ) {
arr3[ i] = arr1[ i] + arr2[ i] ;
}
return ;
}
void show ( double arr[ ] , int n) {
for ( int j = 0 ; j < n; j++ ) {
printf ( "%lf," , arr[ j] ) ;
}
printf ( "\n" ) ;
return ;
}
10-11
# include <stdio.h>
void two_arr ( int arr[ ] [ 5 ] , int n) ;
void show ( int arr[ ] [ 5 ] , int n) ;
int main ( void ) {
int arr1[ 3 ] [ 5 ] = {
{ 4 , 5 , 2 , 1 , 7 } ,
{ 6 , 3 , 4 , 2 , 4 } ,
{ 6 , 3 , 2 , 8 , 2 }
} ;
two_arr ( arr1, 3 ) ;
show ( arr1, 3 ) ;
return 0 ;
}
void two_arr ( int arr[ ] [ 5 ] , int n) {
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < 5 ; j++ ) {
arr[ i] [ j] = 2 * arr[ i] [ j] ;
}
}
}
void show ( int arr[ ] [ 5 ] , int n) {
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < 5 ; j++ ) {
printf ( "%d," , arr[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
}
10-12
# include <stdio.h>
# define MONTHS 12
# define YEARS 5
void year_total ( int y, float arr[ ] [ MONTHS] ) ;
void month_total ( int y, float arr[ ] [ MONTHS] ) ;
int main ( void ) {
const float rain[ YEARS] [ MONTHS] = {
{ 4.3 , 4.3 , 4.3 , 3.0 , 2.0 , 1.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 }
} ;
year_total ( 5 , rain) ;
month_total ( 5 , rain) ;
return 0 ;
}
void year_total ( int y, float arr[ ] [ MONTHS] ) {
int year, month;
float subtot, total;
printf ( " YEAR RAINFALL(inches)\n" ) ;
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" , 2010 + year, subtot) ;
total += subtot;
}
printf ( "\nThe yearly average is %.1f inches.\n\n" , total / y) ;
}
void month_total ( int y, float arr[ ] [ MONTHS] ) {
int year, month;
float subtot, total;
printf ( "MONTHLY AVERAFES:\n\n" ) ;
printf ( " Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n" ) ;
for ( month = 0 ; month < MONTHS; month++ ) {
for ( year = 0 , subtot = 0 ; year < y; year++ ) {
subtot += arr[ year] [ month] ;
}
printf ( "%5.1f" , subtot / y) ;
}
printf ( "\n" ) ;
}
10-13
# include <stdio.h>
void input_arr ( int n, double arr[ ] [ 5 ] ) ;
void average_arr ( double arr[ 3 ] [ 5 ] ) ;
double find_max ( double arr[ 3 ] [ 5 ] ) ;
int main ( void ) {
double arr[ 3 ] [ 5 ] = { 0 } ;
double max = 0 ;
printf ( "接下来请输入3行5列的数据。\n" ) ;
printf ( "请输入第一行数据:" ) ;
input_arr ( 1 , arr) ;
printf ( "请输入第二行数据:" ) ;
input_arr ( 2 , arr) ;
printf ( "请输入第三行数据:" ) ;
input_arr ( 3 , arr) ;
average_arr ( arr) ;
printf ( "\n\n" ) ;
max = find_max ( arr) ;
printf ( "最大值为:%lf\n" , max) ;
return 0 ;
}
void input_arr ( int n, double arr[ ] [ 5 ] ) {
for ( int i = 0 ; i < 5 ; i++ ) {
scanf ( "%lf" , & arr[ n - 1 ] [ i] ) ;
}
return ;
}
void average_arr ( double arr[ 3 ] [ 5 ] ) {
printf ( "行号 平均数\n" ) ;
double sum = 0 ;
for ( int i = 0 ; i < 3 ; i++ ) {
for ( int j = 0 ; j < 5 ; j++ ) {
sum = sum + arr[ i] [ j] ;
}
printf ( "%d %lf\n" , ( i + 1 ) , sum / 5.0 ) ;
}
return ;
}
double find_max ( double arr[ 3 ] [ 5 ] ) {
double max_value = arr[ 0 ] [ 0 ] ;
for ( int i = 0 ; i < 3 ; i++ ) {
for ( int j = 0 ; j < 5 ; j++ ) {
if ( arr[ i] [ j] >= max_value) {
max_value = arr[ i] [ j] ;
}
}
}
return max_value;
}