C Primer Plus (Fifth edition) chapter tenth array and pointer programming exercises

Source: Internet
Author: User
Tags function prototype

Tenth chapter arrays and pointers


Programming exercises

1. Modify the program in Listing 10.7 rain so that it does not use array subscripts, but instead use pointers for calculations (you still need to declare and initialize the array in your program).

# include <stdio.h># define months 12# define years 5int  Main (void) {//initializes the array to precipitation data from 2000 to 2004 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;//Total precipitation for  (year = 0, total = 0; year <  years; year++) {for  (month = 0, subtot = 0; month < months; month++) {Subtot  += * (* (rain + year)  + month);//(Rain+year) indicates that the row address is increased, and the moving range is an array size. *rain + year indicates that the column address is incremented, and the moving range is an int type size. }printf ("Total precipitation in%d years:%.2f\n",  2010+year, subtot); total += subtot;} printf ("Average rainfall in%d years:%.2lf\n",  years, total / years);p rintf ("Average precipitation per month for 5 years: \ n"   ); printf (" 1 month   2 month   3 month   4 month   5 month   6 month   7 month    8 month   9 month  10 month  11 month  12 month \ n ");for  (month = 0; month <  months; month++) {for  (year = 0, total = 0; year <  years; year++) total += * (* (rain + year)  + month);p rintf ("%4.1f ",  total / years);} Putchar (' \ n '); return 0;}


2. Write a program that initializes a double array, and then copies the contents of the array to the other two (3 arrays need to be declared in the main program).

The function that makes the first copy uses the array notation. The function that makes the second copy uses the pointer symbol and uses the pointer's increment action.

Pass the target array name and the number of elements to be copied as arguments to the function. That is, if the following declaration is given, the function call should be as follows:

Double source [5]={1.1, 2.2, 3.3, 4.4, 5.5};

Double targetl[5];

Double Target2 [5];

Copy_arr (source, Target1, 5);

Copy_ptr (source, target1,5);

#include  <stdio.h>void copy_ptr (Double * source, double * target,  int n); Void copy_arr (double source[], double target[], int n); void  show (Double *, int); Int main (void) {double source[5] = { 1.1, 2.2 ,  3.3, 4.4, 5.5 };d ouble target1[5] = { 0 };d ouble target2[5 ] = { 0 };copy_arr (SOURCE,&NBSP;TARGET1,&NBSP;2); copy_ptr (source, target2, 4); printf ("source: "),  show (source, 5);p rintf ("target1: "),  show (target1, 5);p rintf ("target2: ");  show (target2, 5); return 0;} Void copy_ptr (double * source, double * target, int n) {int i;for   (i = 0; i < n; i++) *target++ = *source++;} Void copy_arr (double source[], double target[], int n) {int i;for  (i = 0; i < n; i++) target[i] = source[i];} Void show (double * a, int n) {int i;for  (i = 0; i <  n; i++) printf ("%.2lf ",  a[i]);p rintf ("\ n");}


3. Write a function that returns the maximum value stored in an int array and test the function in a simple program.

#include <stdio.h>int max (int *, int); int main (void) {int a[5] = {2, 4, 1, 0,-3};p rintf ("Maximum value in array:%d\n", Max (A, 5)) ; return 0;} int max (int * A, int n) {int I, temp = a[0];for (i = 1; i < n; i++) {if (temp < a[i]) temp = a[i];} return temp;}


4. Write a function that returns the index of the largest value stored in a double array and test the function in a simple program.

#include <stdio.h>int max (double *, int), int main (void) {double a[5] = {25.6, 41.2, 13.0, 0.5, -0.03};p rintf ("Maximum in array Index of the value:%d\n ", Max (A, 5)); return 0;} int Max (double * A, int n) {int I, temp = 0;for (i = 1; i < n; i++) {if (A[temp] < a[i]) {temp = i;}} return temp;}


5. Write a function that returns the difference between the largest and smallest number in a double array and test the function in a simple program.

#include <stdio.h>double sub (const double *, int); int main (void) {double a[5] = {25.6, 41.2, 13.0, 0.5, 0.03};p rint F ("Difference between the maximum and minimum values in the array:%.2lf\n", Sub (A, 5)); return 0;} Double sub (const double * A, int n) {int i, max = 0, min = 0;for (i = 1; i < n; i++) {if (A[max] < a[i]) max = I;if (a[ Min] > a[i]) min = i;} return A[max]-a[min];}


6. Write a program, initialize a two-dimensional double array, and use any of the functions in Exercise 2 to copy the array to another two-dimensional array (since the two-dimensional array is an array of arrays, you can use a function that handles one-dimensional arrays to copy each of the array's sub-arrays).

#include <stdio.h>void copy_ptr (double * source, double * target, int n); int main (void) {double Source[2][5] = {{25}. 6, 41.2, 13.0, 0.5, 0.03},{12.0, 23.0, -12.0, 3.0, 0.9}};d ouble target[2][5] = {0};copy_ptr (source[0], target[0], 8); printf ("target:"); for (int i = 0; I < 2; i++) {for (int j = 0; J < 5; j + +) printf ("%.2lf", Target[i][j]);} printf ("\ n"); return 0;} void Copy_ptr (double * source, double * target, int n) {int i;for (i = 0; i < n; i++) *target++ = *source++;}


7. Using the copy function in Exercise 2, the 3rd to 5th element of an array containing 7 elements is copied into an array of 3 elements, the function itself does not need to be modified, just choose the appropriate actual parameters (the actual parameters do not need to be array names and arrays size, Just the address of the array element and the number of elements that need to be copied)

#include <stdio.h>void copy_ptr (double * source, double * target, int n); int main (void) {double source[7] = {25.6, 4 1.2, 13.0, 0.5, 0.03, 3.0, 0.9};d ouble target[3] = {0};copy_ptr (&source[2], Target, 3);p rintf ("target:"); for (int i = 0; I < 3; i++) printf ("%.2lf", Target[i]);p rintf ("\ n"); return 0;} void Copy_ptr (double * source, double * target, int n) {int i;for (i = 0; i < n; i++) *target++ = *source++;}


8. Write a program that initializes a 3*5 two-dimensional double array and copies the array to a two-dimensional array using a function that is based on variable-length arrays. Also write a function based on variable-length arrays to display the contents of two arrays. These two functions should be able to handle any n*m array (if there are no compilers that can support variable-length arrays, use traditional C as a function method to process n*5 arrays).

#include  <stdio.h>void copy_ptr (Int x, int y, double source[x][y],  double target[x][y]); Void show (Int x, int y, double source[x][y],  double target[x][y]); Int main (void) {double source[3][5] = {{ 25.6,  41.2, 13.0, 0.5, 0.03},{ 21.0, 32.0, 10.9, 5.6, 4.2 },{ 41.2 ,  13.0, 0.5, 2.0, 32.1 }};d Ouble target[3][5] = { 0 };copy_ PTR (3, 5, source, target);p rintf ("source: \n");  show (3, 5, source,  Target);p rintf ("target: \n"); show (3, 5, source, target); return 0;} /*&NBSP;VC2013 does not support variable-length arrays, which can be compiled with GCC by  */void copy_ptr (int x, int y, double source [x] [Y], double target[x][y]) {int i, j;for  (i = 0; i < 3;  i++) {for  (j = 0; j < 5; j++) target[i][j] = source[i][j];}} Void show (Int x, int y, double source[x][y], double target[x][y]) {int  i, j;for  (i = 0; i < 3; i++) {for  (j = 0;  j < 5; j++) printf ("%.2lf ",  target[i][j]);p rintf ("\ n");}}


9. Write a function that adds the corresponding elements in the two array and stores the result in the 3rd array. That is, if the array L has a value of 2, 4, 5, 8, and the array 2 has a value of 1, 0, 4, 6, then the functions of the function array 3 are assigned 3, 4, 9, and 140 functions, including 3 array names and arrays of sizes. and test the function in a simple program.

#include  <stdio.h> #define &NBSP;LEN&NBSP;5VOID&NBSP;ADD (const int *, const int  *, int *, int); Void show (Int *, int); Int main (void) {int array1[ Len] = { 1, 3, 6, 8, 2 };int array2[len] = { 2,  4, 7, 4, 9 };int array3[len] = { 0 };add (Array1,array2, Array3,len);p rintf ("array1: \n"),  show (Array1, len);p rintf ("array2: \n");  show (array2 ,  len);p rintf ("array3: \n");  show (Array3, len); return 0;} Void add (const int *a1, const int *a2, int *a3, int n) {for   (int i = 0; i < n; i++) a3[i] = a1[i] + a2[i];} Void show (int *a, int n) {for  (int i = 0; i < n;  i++) printf ("%5d ",  a[i]);p rintf ("\ n");} 


10. Write a program, declare an array of 3x5 and initialize it, and the values can be arbitrary. The program prints out the values, then turns the value 1 times, then prints the new value again. Write a function to display the contents of the array, and then write another function to perform the doubling function. Array name and number of rows as parameters passed to the function by the program

#include <stdio.h>void Add (int array[][5], int c), void Show (int (*a) [5], int), int main (void) {int array[3][5] ={{1, 3, 6, 8, 2},{2, 4, 7, 4, 9},{3, 2, 1, 2, 3}};p rintf ("array: \ n"); Show (array, 3), add (array, 3);p rintf ("array: \ n"); Show (array, 3); return 0;} void Add (int array[][5], int c) {for (int i = 0; i < C; i++) for (int j = 0; J < 5; j + +) Array[i][j] *= 2;} void Show (int (*a) [5], int n) {for (int i = 0; i < n; i++) {for (int j = 0; J < 5; j + +) printf ("%5d", A[i][j]);p rintf ( "\ n");}}


11. The main functionality in the program Rain,main () of the rewrite program listing 10.7 is changed to be performed by a function. (for several years of precipitation data, the total annual precipitation, annual rainfall average, and monthly rainfall average)

# include <stdio.h># define months 12# define years 5float  Year_total (float a[][months], int n);//five-year total precipitation float month_sub (float a[][months],  INT&NBSP;N)///monthly average precipitation 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 }};year_total (Rain, YEARS) ; Month_sub (RAIN,&NBSp years); return 0;} Float year_total (float a[][months], int n) {int i, j;float total_y,  total;for  (i = 0, total = 0; i < n; i++) {for  (j  = 0, total_y = 0; j < months; j++) total_y += a[i][j ];p rintf ("Total rainfall in%d years:%.1f\n",  2010 + i, total_y); total += total_y;} printf ("%d year-%d annual precipitation:%.1f\n",  2010, 2010 + i-1, total / years);return  Total;} Float month_sub (float a[][months], int n) {int i, j;float total;for  (i  = 0; i < months; i++) {for  (j = 0, total = 0;  j < n; j++) Total += a[j][i];p rintf ("%d year-%d  %d month average precipitation%.1f\n",  2010 ,  2010+j-1, i+1, total/years);} Return total;}


12. Writing ... A program that prompts the user to enter 3 sets of numbers, each of which includes 5 double values. The program should implement all of the following functions:

A Store input information in an array of 3x5

b Calculates the average of each set (with 5 values)

C Calculates the average of all values

D Find the maximum value in these 15 numbers.

E Print out results

Each task needs to be implemented with a separate function (using the traditional C method of working with arrays). For task B, you need to write a function that calculates and returns the average of a one-dimensional array, looping 3 times to invoke the function to implement task B.

For other tasks, the function should take the entire array as arguments, and the function that completes the tasks C and D should return the answer to its calling function.

#include  <stdio.h>void input (double (*a) [5], int n);d ouble sub (const double  *a, int n);d ouble sub_total (const double a[][5], int n);d ouble  Max (const double a[][5], int n); Void show (Const double a[][5], int  n); Int main (void) {double array[3][5];int i;input (array, 3);for  (i = 0 ;  i < 3; i++) printf ("The average of the number of%d sets:%.2lf\n", I+1, sub (array[i], 5));p rintf (" Average of all numbers:%.2lf\n ",  sub_total (array, 3));p rintf (" the largest number of all numbers:%.2lf\n ",  max (array, 5));p rintf (" Whole number of sets: \ n "); show (array, 3); return 0;} Void input (double (*a) [5], int n) {int i, j;for  (i = 0; i <  n; i++) {printf ("Please enter 5 values in the number set%d:",  i+1);for  (j = 0; j < 5;  j++) scanf ("%lf",  &a[i][j]);}} Double sub (Const double *a,&nbsP;int n) {int i;double total = 0;for  (i = 0; i < n;  i++) Total += a[i];return total / n;} Double sub_total (const double a[][5], int n) {int i, j;double total  = 0;for  (i = 0; i < n; i++) {for  (j = 0; j  < 5; j++) total += a[i][j];} return total /  (n * 5);} Double max (const double a[][5], int n) {int i, j;double temp =  a[0][0];for  (i = 0; i < n; i++) {for  (j = 0; j  < 5; j++) if  (Temp < a[i][j]) temp = a[i][j];} Return temp;} Void show (const double a[][5], int n) {int i, j;double max =  a[0][0];for  (i = 0; i < n; i++) {for  (j = 0; j < 5; j++) printf ("%.2lf ",  a[i][j]);p rintf ("\ n");}} 


13. The following is a two function prototype:

void Show (double ar[], int n); n is the number of elements

void Show2 (double ar2[][3], int n); n is the number of rows

A, write a function call to pass the compound text containing the values 8, 3, 9, and 2 to the function shows ().

b, write a function call to pass a compound literal containing 2 rows and 3 columns of value to the function Show2 (), where the first behavior is 8, 3, 9, and the second behavior is 5, 4, 1.

#include <stdio.h>void show (double ar[], int n), void Show2 (double ar2[][3], int n); int main (void) {Show ((double[]) { 8, 3, 9, 2}, 4); Show2 ((double[][3)) {{8, 3, 9}, {5, 4, 1}}, 2); return 0;} void Show (double ar[], int n) {int i;for (i = 0; i < n; i++) printf ("%.2lf", Ar[i]);p rintf ("\ n");} void Show2 (double ar2[][3], int n) {int I, j;for (i = 0; i < n; i++) {for (j = 0; J < 3; j + +) printf ("%.2lf", ar2[i][j ]);p rintf ("\ n");}}


This article is from the "30 years old school Programming" blog, please be sure to keep this source http://xiongyi85.blog.51cto.com/8756903/1655700

C Primer Plus (Fifth edition) chapter tenth array and pointer programming exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.