第十章 数组和指针

 1.修改程序清单10.7中的程序rain,使它不使用数组下标,而是使用指针进行计算(程序中仍然需要声明并初始化数组)

 

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

int main()
{
	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 *rainPtr;
	
	rainPtr = rain[0];
	
	printf("YEAR RAINFALL (inches)\n");
	for(year = 0,total = 0; year < YEARS; year++)
	{
		
		for(month = 0,subtot = 0; month < MONTHS; month++)
		{
			
			subtot += *rainPtr;
		    rainPtr++;
		} 
		printf("200%d %.1f\n",year,subtot);
		total += subtot;
	} 
	printf("\nThe yearly average is %.1f inches.\n",total/MONTHS);
	printf("MONTHLY AVERAGES:\n\n");
 	printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct ");
    printf(" Nov  Dec\n");
    
    rainPtr = rain[0]; //reset pointer  
    for(month = 0; month < MONTHS ; month++)
    {
    	rainPtr = rainPtr + month;
    	for(year = 0,subtot = 0; year < YEARS ; year++)
    	{
	    	subtot += *rainPtr;
	    	rainPtr += MONTHS;
	    }
	    rainPtr = rain[0];
	    printf(" %.1f ",subtot/YEARS);
    }
	printf("\n");
	return 0; 
	
}

 

 

2.编写一个程序,初始化一个double数组,然后把数组内容复制到另外两个数组(3个数组都需要在主程序中声明)。制作第一份拷贝的函数使用数组符号。制作第二份拷贝的函数使用指针符号,并使用指针的增量操作。把目标数组名和要复制的元素数目做为参数传递给函数。也就是说,如果给定了下列声明,函数调用应该如下面所示:

 

#include<stdio.h>

#define SIZE 5

void copy_arr(double target1[],double source[],int n);
void copy_ptr(double *target2,double *source,int n);
void display(double *target,int n);

int main()
{
	double source[SIZE]={
		1.1,1.2,1,6.4,7.8
	};
	double target1[SIZE];
	double target2[SIZE];
	
	copy_arr(target1,source,SIZE);
	copy_ptr(target2,source,SIZE);
	
	display(target1,SIZE);
	printf("\n");
	display(target2,SIZE);
	
	printf("\n");
	return 0;
}

void copy_arr(double target1[],double source[],int n)
{
	for(int i=0;i < n;i++)
	{
		target1[i] = source[i];
	}
}
void copy_ptr(double *target2,double *source,int n)
{
	for(int i = 0; i < n; i++)
	{
		//*target2 = *source;
		//source++;
		//target2++;
		*target2++ = *source++;
	}
}
void display(double *target,int n)
{
	for(int i = 0 ; i < n ;i++)
	{
		printf("%2.2f ",*target++);
	//	target++;
	}
}

 

 

 3.编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数。

 

#include<stdio.h>

#define SIZE 5

int select_max(int *ptr,int n);

int main()
{
	int array[SIZE] = {
		1,2,3,4,5
	};
	int max;
	
	max = select_max(array,SIZE);
	printf("max is %d\n",max);
	return 0;
}

int select_max(int *ptr,int n)
{
	int max = *ptr;
	for(int i = 0 ; i < SIZE ; i++)
	{
		if(max < *ptr)
		   max = *ptr;
        ptr++;
	}
	return max;
}

 

 

 4.编写一个函数,返回一个double数组中存储的最大数值的索引,并在一个简单程序中测试这个函数。

#include<stdio.h>

#define SIZE 5

int select_max(int *ptr,int n);

int main()
{
	int array[SIZE] = {
		1,2,3,4,5
	};
	int max_index;
	
	max_index = select_max(array,SIZE);
	printf("max index is %d\n",max_index);
	return 0;
}

int select_max(int *ptr,int n)
{
	int max = *ptr;
	int max_index = 0;
	for(int i = 0 ; i < SIZE ; i++)
	{
		if(max < *ptr)
		{
			 max = *ptr;
			 max_index = i;
		}
		  
        ptr++;
	}
	return max_index;
}

 

 5.编写一个函数,返回一个double数组中最大的和最小的数之间的差值,并在一个简单的程序中测试这个函数。

#include<stdio.h>

#define SIZE 5

int D_value(int *ptr,int n);

int main()
{
	int array[SIZE] = {
		1,2,3,4,5
	};
	int value;
	
	value = D_value(array,SIZE);
	printf("max - min = %d\n",value);
	return 0;
}

int D_value(int *ptr,int n)
{
	int max = *ptr;
	int min = *ptr;
	for(int i = 0 ; i < SIZE ; i++)
	{
		if(max < *ptr )
		{
			 max = *ptr;
		}
		if(min > *ptr)
		 min = *ptr;
  
        ptr++;
	}
	return max-min;
}

 

6.编写一个程序,初始化一个二维double数组,并利用练习2中的任一函数来把这个数组复制到另一个二维数组(因为二维数组是数组的数组,所以可以使用处理一维数组的函数来复制数组的每个子数组)。

#include<stdio.h>
#define SIZE 6

void copy_ptr(double *target1,double *source,int n);
void display(double *target,int n);

int main()
{
	double source[2][3]={
		1.1,1.2,1,6.4,7.8
	};
	double target1[2][3];
	
	copy_ptr(target1[0],source[0],SIZE);
	
	display(target1[0],SIZE);
	printf("\n");
	
	printf("\n");
	return 0;
}
void copy_ptr(double *target1,double *source,int n)
{
	for(int i = 0; i < n; i++)
	{
		//*target2 = *source;
		//source++;
		//target2++;
		*target1++ = *source++;
	}
}
void display(double *target,int n)
{
	for(int i = 0 ; i < n ;i++)
	{
		printf("%2.2f ",*target++);
	//	target++;
	}
}

  7.利用练习2中的复制函数,把—个包含7个元素的数组内第3到第5元素复制到一个包含3个元素的数组中。函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,而只须是数组元素的地址和需要复制的元素数目)。

#include<stdio.h>

#define SIZE 5
#define TARGET_SIZE 3


void copy_ptr(double *target1,double *source,int n);
void display(double *target,int n);

int main()
{
	double source[SIZE]={
		1.1,1.2,1,6.4,7.8
	};
	double target1[TARGET_SIZE];
	
	copy_ptr(target1,source+2,TARGET_SIZE);
	
	display(target1,TARGET_SIZE);
	printf("\n");
	
	printf("\n");
	return 0;
}
void copy_ptr(double *target1,double *source,int n)
{
	for(int i = 0; i < n; i++)
	{
		*target1++ = *source++;
	}
}
void display(double *target,int n)
{
	for(int i = 0 ; i < n ;i++)
	{
		printf("%2.2f ",*target++);
	//	target++;
	}
}

  8.编写一个程序,初始化一个3x5的二维double数组,并利用一个基于变长数组的函数把该数组复制到另一个二维数组。还要编写。个基于变长数组的函数来显示两个数组的内容。这两个函数应该能够处理任意的NxM数组(如果没有可以支持变长数组的编译器,就使用传统C中处理Nx5数组的函数方法)。

#include<stdio.h>

#define ROW 3
#define COL 5

void copy_ptr(double *target1,double *source,int n);
void copy_arr(double target2[][COL],double source[][COL],int rows);
void display(double target[][COL],int rows);

int main()
{
	double source[ROW][COL] = {
		{1,1.2,4,5.6,7.8},
		{1,1.2,4,5.6,7.8},
		{1,1.2,4,5.6,7.8}
	};
	double target1[ROW][COL],target2[ROW][COL];
	
	copy_ptr(target1[0],source[0],ROW*COL);
	copy_arr(target2,source,ROW);
	
	display(target1,ROW); 
	printf("\n");
	display(target2,ROW); 
	printf("\n");
	return 0;
}
 
 void copy_ptr(double *target1,double *source,int n)
 {
 	for(int i = 0; i < n; i++)
 	   *target1++ = *source++;
 }
void copy_arr(double target2[][COL],double source[][COL],int rows)
{
	for(int i = 0 ; i < rows ; i++)
	   for(int j = 0; j < COL ; j++)
	       target2[i][j] = source[i][j];
	
}
void display(double target[][COL],int rows)
 {
 	for(int i = 0 ; i < rows ; i++)
 	{
	 	for(int j = 0; j < COL ; j++)
	       printf("%2.1f ",target[i][j]);
	   printf("\n");
    }
	  	   
 }

  9.编写一个函数,把两个数组内的相应元素相加,结果存储到第3个数组内。也就是说,如果数组l具有值2、4、5、8,数组2具有值1、0、4、6,则函数对数组3赋值为3、4、9、140函数的参数包括3个数组名和数组大小。并在一个简单的程序中测试这个函数。

#include<stdio.h>

#define SIZE 6

void sum(double *target1,double *target2,double *result,int n);
void display(double *target,int n);

int main()
{
	double source1[SIZE]={
		1.1,1.2,1,6.4,7.8
	};
	double source2[SIZE]={
		1.1,1.2,1,6.4,7.8
	};
	double result[SIZE];
		
	sum(source1,source2,result,SIZE);
	
	display(result,SIZE);
	printf("\n");

	return 0;
}
void sum(double *target1,double *target2,double *result,int n)
{
	for(int i = 0; i < n; i++)
	{
	   result[i] = target1[i] + target2[i];
	}
}
void display(double *target,int n)
{
	for(int i = 0 ; i < n ;i++)
	{
		printf("%2.2f ",*target++);
	//	target++;
	}
}

   10.编写…个程序,声明一个3x5的数组并初始化,具体数值可以随意。程序打印出数值,然后数值翻1番,接着再次打印出新值。编写一个函数来显示数组的内容,再编写另一个函数执行翻倍功能。数组名和数组行数作为参数由程序传递给函数

#include<stdio.h>

#define ROW 3
#define COL 5

void double_arr(double target[][COL],int rows);  //arrry*2
void display(double source[][COL],int rows);

int main()
{
	double source[ROW][COL] = {
		{1,1.2,4,5.6,7.8},
		{1,1.2,4,5.6,7.8},
		{1,1.2,4,5.6,7.8}
	};
	
    display(source,ROW);
	double_arr(source,ROW);	
	printf("\n");
	display(source,ROW); 
	
	printf("\n");
	return 0;
}
 
 
void double_arr(double source[][COL],int rows)
{
	for(int i = 0 ; i < rows ; i++)
	   for(int j = 0; j < COL ; j++)
	       source[i][j] = source[i][j] * 2;
	
}
void display(double source[][COL],int rows)
 {
 	for(int i = 0 ; i < rows ; i++)
 	{
	 	for(int j = 0; j < COL ; j++)
	       printf("%2.1f ",source[i][j]);
	   printf("\n");
    }
	   
	   
 }

  11.重写程序清单10.7的程序rain,main()中的主要功能改为由函数来执行。

#include <stdio.h>

#define MONTHS 12    // number of months in a year
#define YEARS   5    // number of years of data

void every_total(const float source[][MONTHS],int years); //rainfull of every year and total rainfull
void average_month(const float source[][MONTHS],int years);//average rainfull of every month

int main(void)
{
 // initializing rainfall data for 2000 - 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}
    };
    
    every_total(rain,YEARS);
    average_month(rain,YEARS);
    
    printf("\n");
    return 0;
}

void every_total(const float source[][MONTHS],int years)
{
	printf("YEAR RAINFULL(inches)\n");
	float subtotal,total;
	subtotal = 0;
	total = 0;
	for(int i = 0; i < years ; i++)
	{
		for(int j = 0; j < MONTHS; j++)
		 {
 			subtotal += source[i][j];
 		 }
		printf("200%d %2.1f\n",i,subtotal);   
        total += subtotal;
        subtotal = 0 ; //reset subtotal to 0
	}
	printf("The yearly average is %2.1f inches\n",total/YEARS);
}
void average_month(const float source[][MONTHS],int years)
{
    float sub = 0;
    
    printf("MONTHLY AVERAGES:\n\n");
    printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct ");
    printf(" Nov  Dec\n");
    for(int i = 0; i < MONTHS ; i++)
	{
		for(int j = 0; j < years; j++)
		 {
 			sub += source[j][i];
 		 }
		printf(" %2.1f ",sub);   
        sub = 0; //reset sub= 0 
	} 
}
    12.编写…个程序,提示用户输入3个数集,每个数集包括5个double值。程序应当实现下列所有功能:

    a.把输入信息存储到一个3x5的数组中
    b.计算出每个数集(包含5个数值)的平均值
    c.计算所有数值的平均数
    d.找出这15个数中的最大值.
    e.打印出结果
    每个任务需要用一个单独的函数来实现(使用传统C处理数组的方法)。对于任务b,需要编写计算并返回一维数组平均值的函数,循环3次调用该函数来实现任务b。对于其他任务,函数应当把整个数组做为参数,并且完成任务c和d的函数应该向它的调用函数返回答案。

#include<stdio.h>

#define ROWS 3
#define COLS 5

void input_arrays(double source[][COLS],int rows); //input arrays
void average_array(double source[][COLS],double averages[],int rows); //compute the average of every arrays
void average_total(double *source,int rows); //compute average of all arrays
void output_arrays(double source[][COLS],int rows); //output arrays
double max(double source[][COLS],int rows);// find the max one

int main(void)
{
	double source[ROWS][COLS];
	double averages[ROWS];
	
	input_arrays(source,ROWS); //input array
	printf("the array you input is:\n"); 
	output_arrays(source,ROWS); //output array
    average_array(source,averages,ROWS); //compute the average value of each array
	printf("\n the average value of every array is :\n");
	for(int i = 0;i < ROWS ;i++ )
	{
		printf("%.2f ",averages[i]);
	}
	printf("\n the max one is %.2f\n",max(source,ROWS));
	return 0;
}

void input_arrays(double source[][COLS],int rows) //input arrays
{
	printf("please input 3 arrays, 5 counts for each array:\n ");
	for(int i = 0 ; i < rows ; i++)
		for(int j = 0; j < COLS ; j++)
		  scanf("%lf",&source[i][j]);
  
}
//compute the average of every arrays
void average_array(double source[][COLS],double averages[],int rows) 
{
 
	double subtotal = 0;

	for(int i = 0 ; i < rows; i++)
	{
		for(int j = 0 ; j < COLS; j++)
		{
			subtotal += source[i][j];
		}
		averages[i] = subtotal/COLS;
		subtotal = 0; //reset subtotal = 0
	} 
}

//output arrays
void output_arrays(double source[][COLS],int rows) 
{
	
	for(int i = 0 ; i < rows ; i++)
 	{
	 	for(int j = 0; j < COLS ; j++)
	       printf("%.2lf ",source[i][j]);
	   printf("\n");
    }
  
}

double max(double source[][COLS],int rows)
{
	double max = source[0][0];
	for(int i = 0 ; i < rows ; i++)
 	{
	 	for(int j = 0; j < COLS ; j++)
	       if(source[i][j] > max)
	       		max = source[i][j];
  
    }
	return max;
}
 

你可能感兴趣的:(数组)