C Primer Plus 10.13 编程练习

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;
	
	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 AVERAGES:\n\n");
	
	printf(" Jan Feb Mar Ap  Ma  Jun Jul Aug Sep Oct ");
	
	printf("Nov Dec\n");
	
	for(month = 0; month < MONTHS; month++)
	{
		
		for(year = 0, subtot = 0; year < YEARS; year++)
		
			subtot += * ( * ( rain + year ) + month);
			
		printf("%4.1f", subtot / YEARS);	
	}
	
	printf("\n");
	
	return 0;
	
}

2、

#include <stdio.h>

void copy_arr(double target[], double source[], int number);

void copy_ptr(double target[], double source[], int number);

void copy_ptrs(double target[], double source[], double f_source[]);

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);
	
	for (int x = 0; x < 5; x++)
	{

		printf("target1[%d] is %.1lf,\n", x, target1[x]);

	}

	printf("\n");
	
	for (int y = 0; y < 5; y++)
	{

		printf("target2[%d] is %.1lf.\n", y, target2[y]);

	}

	printf("\n");
	
	for (int z = 0; z < 5; z++)
	{

		printf("target3[%d] is %.1lf.\n", z, target3[z]);

	}

	printf("\n");
	
	return 0;
}

void copy_arr(double target[], double source[], int number)
{

	for (int i = 0; i < number; i++)
	{

		target[i] = source[i];

	}
}

void copy_ptr(double target[], double source[], int number)
{

	for (int j = 0; j < number; j++)
	{

		*(target + j) = *(source + j);

	}
}

void copy_ptrs(double target[], double source[], double f_source[])
{

	double * k;

	int l = 0;

	for (k = source, l = 0; k < f_source; k++, l++)
	{

		*(target + l) = *(source + l);

	}
}

3、

#include <stdio.h>
#define SIZE 3

int function(int array[], int number);

int main(void)
{
	int array[SIZE] = { 1, 2, 3 };
	
	printf("The largest value stored in the array is %d.\n", function(array, SIZE));
	
	return 0;
}

int function(int array[], int number)
{
	int largest = *array;
	
	for (int i = 0; i < number; i++)
	{

		if(largest < *(array + i))
		
			largest = *(array + i);

	}
	
	return largest;
}

4、

#include <stdio.h>
#define SIZE 3

int function(double array[], int number);

int main(void)
{
	double array[SIZE] = {1.1, 2.2, 3.3};
	
	printf("The index of the largest value stored in the array is %d.\n", function(array, SIZE));
	
	return 0;
}

int function(double array[], int number)
{
	int largest = *array;
	int largest_index = 0;
	
	for (int i = 0; i < number; i++)
	{
		if (largest < *(array + i))
		{
			largest = *(array + i);
			largest_index = i;
		}
	}
	
	return largest_index;
}

5、

#include <stdio.h>
#define SIZE 6

double function(double array[], int number);

int main(void)
{
	double array[SIZE] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
	
	printf("The difference between the largest and smallest values of array"
	 		" is %.1lf.\n", function(array, SIZE));
	
	return 0;
}

double function(double array[], int number)
{

	double largest = *array;

	double smallest = *array;
	
	for(int i = 0; i < number; i++)
	{
	
		if (largest < *(array + i))
	
			largest = *(array + i);
	
		if (smallest > *(array + i))
	
			smallest = *(array + i);
	
	}
	
	return largest - smallest;
}

6、

#include <stdio.h>
#define SIZE 6

void reverse(double array[], int number);

int main(void)
{
	
	double array[SIZE] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
	
	printf("The contents of the array are:\n");
	
	for (int i = 0; i < SIZE; i++)
	
		printf("%.1lf\n", array[i]);
	
	reverse(array, SIZE);
	
	printf("\nThe contents of the array now are:\n");
	
	for (int j = 0; j < SIZE; j++)
	
		printf("%.1lf\n", array[j]);
	
	return 0;
}

void reverse(double array[], int number)
{
	
	double temp;
	
	for (int k = 0; k < number / 2; k++)
	{
	
		temp = *(array + k);
	
		*(array + k) = *(array + number - k - 1);
	
		*(array + number - k - 1) = temp;
	
	}
}

7、

#include <stdio.h>

void copy_ptr(double target[], double source[], int number);

int main(void)
{
	double source[2][3] = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 } };
	
	double target[2][3];
	
	for (int i = 0; i < 2; i++)
	{
	
	    copy_ptr(target[i], source[i], 3);
	
	}

	for (int y = 0; y < 2; y++)

	    for (int x = 0; x < 3; x++)
	    	
		    printf("target[%d][%d] is %.1lf\n", y, x, target[y][x]);
		    
	
	printf("\n");

	return 0;
}

void copy_ptr(double target[], double source[], int number)
{
	for (int j = 0; j < number; j++)

		*(target + j) = *(source + j);
	
}

8、

#include <stdio.h>

void copy_arr(double target[], double source[], int number);

int main(void)
{

	double source[7] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };

	double target[3];

	copy_arr(target, &source[2], 3);
	
/*

第一个参数target是数组名,也就是target数组的首地址: &target[0] 。

第二个参数是&source[2],这是取source数组第三个元素(索引为2)的地址。因为数组在内存中是连续存储的,
所以从source[2]开始的地址就是source数组的第三个元素的位置。

第三个参数是3,表示要复制3个元素。

因此,这个函数调用的意思是从source数组的第三个元素(索引2)开始,复制3个元素到target数组中。
也就是说,将source[2]、source[3]、source[4]分别复制到target[0]、target[1]、target[2]。

*/

	for (int x = 0; x < 3; x++)
	
		printf("target[%d] is %.1lf\n", x, target[x]);
	
	printf("\n");

	return 0;
}

void copy_arr(double target[], double source[], int number)
{
	for (int i = 0; i < number; i++)
	
		target[i] = source[i];
	
}

9、

#include <stdio.h>
#define ROWS 3
#define COLS 5

void copy(int rows, int cols, double target[rows][cols], double source[rows][cols]);

void display(int rows, int cols, double target[rows][cols], double source[rows][cols]);

int main(void)
{
	double source[ROWS][COLS] = 
	{
		
		{ 1.1, 2.2, 3.3, 4.4, 5.5 }, 
		
		{ 6.6, 7.7, 8.8, 9.9, 10.10 }, 
		
		{ 11.11, 12.12, 13.13, 14.14, 15.15 }
		
	};
	
	double target[ROWS][COLS];

	copy(ROWS, COLS, target, source);
	
	display(ROWS, COLS, target, source);

	return 0;
}

void copy(int rows, int cols, double target[rows][cols], double source[rows][cols])
{
    for (int i = 0; i < rows; i++)
    
        for (int j = 0; j < cols; j++)
	    
		    target[i][j] = source[i][j];
	    
}

void display(int rows, int cols, double target[rows][cols], double source[rows][cols])
{
    printf("Source array:\n");
    
	for (int i = 0; i < rows; i++)
    
        for (int j = 0; j < cols; j++)
	    
		    printf("source[%d][%d] is %.2lf\n", i, j, source[i][j]);
	    
	
    
    printf("\n");

    printf("Target array:\n");
    
	for (int i = 0; i < rows; i++)
    
        for (int j = 0; j < cols; j++)
	    
		    printf("target[%d][%d] is %.2lf\n", i, j, target[i][j]);
	    
}

10、

#include <stdio.h>
#define SIZE 4

void function(int array1[], int array2[], int array3[], int number);

int main(void)
{

    int array1[SIZE] = { 2, 4, 5, 8 };

    int array2[SIZE] = { 1, 0, 4, 6 };

    int array3[SIZE];

    function(array1, array2, array3, SIZE);

    printf("The contents of the third array:\n");
    
	for (int j = 0; j < SIZE; j++)
    
        printf("array[%d]:%d\n", j, array3[j]);
    
    printf("\n");

    return 0;
}

void function(int array1[], int array2[], int array3[], int number)
{
    for (int i = 0; i < number; i++)
    
        array3[i] = array1[i] + array2[i];
    
}

11、

#include <stdio.h>

void print_array(int array[][5], int rows);

void double_array(int array[][5], int rows);

int main(void)
{
	int arr[3][5] = 
	{ 
		{ 1, 2, 3, 4, 5 },
					   
		{ 6, 7, 8, 9, 10 },
		
		{ 11, 12, 13, 14, 15 }
		
	};

	printf("Original array:\n\n");
	
	print_array(arr, 3);


	putchar('\n');

	double_array(arr, 3);

	printf("Doubled array:\n\n");

	print_array(arr, 3);

	return 0;
}

void print_array(int array[][5], int rows)
{

	for(int i = 0; i < rows; i++)
	
		for (int j = 0; j < 5; ++j)
		
			printf("ar[%d][%d] is %d\n", i, j, array[i][j]);
		
}

void double_array(int array[][5], int rows)
{


	for(int i = 0; i < rows; ++i)
	
		for (int j = 0; j < 5; j++)
	
			array[i][j] *= 2;

}

12、

#include <stdio.h>
#define MONTHS 12 
#define YEARS 5 

void annual_averages(int years, int months, const float data[years][months]);

void monthly_averages(int years, int months, const float data[years][months]);

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 }
		
	};


	annual_averages(YEARS, MONTHS, rain);
	
	monthly_averages(YEARS, MONTHS, rain);

	return 0;
}

void annual_averages(int years, int months, const float data[years][months])
{
	float subtotal, total;

	printf(" YEAR RAINFALL (inches)\n");

	for (int year = 0, total = 0; year < years; year++)
	{
	
		for (int month = 0, subtotal = 0; month < months; month++)
		
			subtotal += data[year][month];

		printf("%5d %15.1f\n", 2010 + year, subtotal);

		total += subtotal; 
	}

	printf("\nThe yearly average is %.1f inches.\n\n", total/years);	

}

void print_monthly_averages(int years, int months, const float data[years][months])
{
	float subtotal;

	printf("MONTHLY AVERAGES:\n\n");

	printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct "); 
	
	printf(" Nov Dec\n");

	for (int month = 0; month < months; month++)
	{ 
		
		for (int year = 0, subtotal = 0; year < years; year++)
	
			subtotal += data[year][month];

		printf("%4.1f ", subtotal/YEARS);
	}
	printf("\n");
}

13、

#include <stdio.h>
#define ROWS 3
#define COLS 5

void Store(double array[][COLS], int n);

double Compute1(double array[][COLS], int n);

double Compute2(double array[][COLS], int n);

double Largest(double array[][COLS], int n);

void Report(double array[][COLS], int n);

int main(void)
{
    double array[ROWS][COLS];

    Store(array, ROWS);

    Report(array, ROWS);

    return 0;
}

void Store(double array[][COLS], int n)
{

    printf("Please enter three sets of five double numbers:\n");
    
    for(int j = 0; j < n; j++)
	{
	
		if(j < n)
    
			printf("Please enter the %dth set:\n", j + 1);
 
    	for (int i = 0; i < COLS; i++)

        	scanf("%lf", &array[j][i]);
        	
	
    } 
}

double Compute1(double array[][COLS], int n)
{
    double average = 0;
    
	double sum = 0;

    for (int i = 0; i < COLS; i++)
        
		sum += array[n][i];

    average = sum / COLS;

    return average;
}

double Compute2(double array[][COLS], int n)
{
    double average = 0;
    
	double sum = 0;

    for(int i = 0; i < COLS; i++)
        
		for(int j = 0; j < n; j++)
        
		    sum += array[j][i];

    average = sum / (n * COLS);

    return average;
}

double Largest(double array[][COLS], int n)
{
    double largest = array[0][0];

    for(int i = 0; i < n; i++)
    {
       
	    for(int j = 0; j < COLS; j++)
        {
          
		    if (array[i][j] > largest)
            
                largest = array[i][j];
            
        }
    }

    return largest;
}

void Report(double array[][COLS], int n)
{
    for (int i = 0; i < n; i++)
        printf("The average of the %dth set of five values is: %.3lf.\n", i + 1, Compute1(array, i));

    printf("\nThe average of all values is: %lf.\n", Compute2(array, ROWS));

    printf("\nThe largest value among these values is: %.3lf\n", Largest(array, ROWS));
}

14、

#include <stdio.h>
#define ROWS 3
#define COLS 5

void store(double ar[], int n);

double average2d(int rows, int cols, double ar[rows][cols]);

double max(int rows, int cols, double ar[rows][cols]);

void show(int rows, int cols, double ar[rows][cols]);

double average(const double ar[], int n);

int main(void)
{
  
    double stuff[ROWS][COLS];
   
    int row;

    for(row = 0; row < ROWS; row++)
    {
       
	    printf("Enter %d numbers for row %d\n", COLS, row + 1);
       
	    store(stuff[row], COLS);
    }

    printf("array contents:\n");
    
	show(ROWS, COLS, stuff);

    for(row = 0; row < ROWS; row++)
      
	    printf("average value of row %d = %lu\n", row + 1, average(stuff[row],COLS));
    
	printf("average value of all rows = %lu\n", average2d(ROWS, COLS, stuff));
    
	printf("largest value = %lu\n", max(ROWS, COLS, stuff));
    
	printf("Bye!\n");
    return 0;
}

void store(double ar[], int n)
{
    int i;
    
	for(i = 0; i < n; i++)
    {
      
	    printf("Enter value #%d: ", i + 1);
      
	    scanf("%lf", & ar[i]);
    
	}
}

double average2d(int rows, int cols, double ar[rows][cols])
{
    
	int r, c;
    
	double sum = 0.0;

    for(r = 0; r < rows; r++)
        
		for(c = 0; c < cols; c++)
        
		    sum += ar[r][c];
    
	if(rows * cols > 0)
    
	    return sum / (rows * cols);
   
    else
   
        return 0.0;
}

double max(int rows, int cols, double ar[rows][cols])
{
    
	int r, c;
    
	double max = ar[0][0];

    for(r = 0; r < rows; r++)
      
	    for(c = 0; c < cols; c++)
        
		    if(max < ar[r][c])
          
		        	max = ar[r][c];
    return max;
}

void show(int rows, int cols, double ar[rows][cols])
{
    int row, col;

    for(row = 0; row < rows; row++)
    
        for(col = 0; col < cols; col++)
           
		    printf("%lu\n", ar[row][col]);
  
    
}

double average(const double ar[], int n)
{
  
    int i;
  
    double sum = 0.0;

    for(i = 0; i < n; i++)
       
	    sum += ar[i];
    
	if(n > 0)
      
	    return sum / n;
    
	else
     
	    return 0.0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值