`

第十章 数组和指针

 
阅读更多

 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;
}
 
1
2
分享到:
评论

相关推荐

    C语言书籍<<C primer plus>>第十章数组与指针课后习题代码参考(自制)

    内容包含所有第十章相关要求的代码,仅供参考.

    C语言查询用书

    10.3 数组指针和指向数组的指针变量 13 10.3.1 指向数组元素的指针 13 10.3.2 通过指针引用数组元素 14 10.3.3 数组名作函数参数 16 10.3.4 指向多维数组的指针和指针变量 22 10.4 字符串的指针指向字符串的针指变量...

    C++primer 课后题答案

    第四章 数组和指针 21 第五章 表达式 31 第六章 语句 37 第七章 函数 37 第八章 标准IO库 37 第九章 顺序容器 43 第十章 关联容器 60 第十一章 泛型算法 75 第十二章 类和数据抽象 86 第十三章 复制控制 94 第十四章...

    C语言全套资料 C语言程序设计 C语言算法 C语言课件

    C语言全套资料 C语言程序设计 C语言算法 C语言课件 C语言顺序程序设计 C语言数组 C语言循环控制 C语言预处理命令 C语言文件操作指针 C语言选择结构程序设计 C语言...第十章 指针 第十一章 结构体与共用体 第十三章 文件

    C++ PPT课件。。。。

    第一章 C++概述,第二章 数据类型、运算符与表达式,第三章 简单的输入输出,第四章 C++的流程控制语句,第五章 函数与编译预处理,第六章 数组 ,第七章 结构体、共同体和枚举类型 ,第八章 指针和引用,第九章 类...

    上海交大C++面向对象

    第十章 结构 第十一章 类 第十二章 构造函数与析构函数 第十三章 面向对象程序设计 第十四章 堆与拷贝构造函数 第十五章 静态成员与友员   第十六章 继承 第十七章 多重继承 第十八章 运算符重载 ...

    C++程序设计_谭浩强.part2.rar

    作者:谭浩强 出版社:清华大学出版社 ISBN:7-302-08599-4/TP.6159 第一章 C++的初步知识 ...第十章 运算符重载 第十一章 继承与派生 第十二章 多态性与虚函数 第十三章 输入输出流 第十四章 C++工具 附录A、B

    C++程序设计_谭浩强.part1.rar

    作者:谭浩强 出版社:清华大学出版社 ISBN:7-302-08599-4/TP.6159 第一章 C++的初步知识 ...第十章 运算符重载 第十一章 继承与派生 第十二章 多态性与虚函数 第十三章 输入输出流 第十四章 C++工具 附录A、B

    C++程序设计_谭浩强.part3.rar

    作者:谭浩强 出版社:清华大学出版社 ISBN:7-302-08599-4/TP.6159 第一章 C++的初步知识 ...第十章 运算符重载 第十一章 继承与派生 第十二章 多态性与虚函数 第十三章 输入输出流 第十四章 C++工具 附录A、B

    C语言常见问题

    第七章 指针和内存分配 37 第八章 函数 44 第九章 数组 50 第十章 位(bit)和字节(byte) 56 第十一章 调试 59 第十二章 标准库函数 67 第十三章 时间和日期 79 第十四章 系统调用 84 第十五章 可移植性 92 第十六章 ...

    C++语言程序设计习题与实验指导

    第十章:群体数据的组织 第十一章:流类库与输入/输出 第十二章:异常处理 第十三章:MFC库与Windows程序开发概述 第二部分:实验指导 实验1:Visual C++ 6.0开发环境应用入门(2学时) 实验2:C++简单程序...

    C语言程序设计(第二版)谭浩强编写

    第一章 C语言程序设计语言概述 ...第十章 函数 第十一章 指针的概念 第十二章 多维数组的指针变量 第十三章 结构 第十四章 C语言程序设计基础之联合 第十五章 枚举与位运算 第十六章 预处理 第十七章 文件

    C语言高级程序员编程指南

    第八章 数组、指针和结构 第九章 DOS和BIOS服务 第十章 内存管理 第十一章 日期和时间 第十二章 重定向I/O和进程命令行 第十三章 编程工具 第十四章 高级C语言编程 第十五章 C++入门 第十六章 对象...

    Pascal的多种退出语句用法.doc

    第十章 记录与文件类型 第十一章 指针 第十二章 程序调试 常用算法与策略 第一章 算法的概念 第二章 递归 第三章 回溯 第四章 排序 第五章 查找 第六章 穷举策略 第七章 贪心算法 第八章 分治策略 数据...

    《C++ 入门》.PDF

    第十章 结构 第十一章 类机制 第十二章 类的作用域和类成员访问 第十三章 派生类 第十四章 派生类的存取权限 第十五章 构造函数和析构函数 第十六章 虚函数与多态性 第十七章 虚函数与抽象类 第十八章 运算符重载 第...

    计算机程序设计与分析课件

    第一章 绪论 第二章 C++简单程序设计 第三章 函数 第四章 类 第五章C++程序的基本结构 第六章 数组、指针与字符串 ...第十章 群体数据的组织 第十一章 流类库 第十二章 异常处理 第十三章 Windows应用程序设计基础

    C++精品ppt课件

    第一章 绪论 第二章 C++简单程序设计 第三章 函数 第四章 类 第五章 C++程序的基本结构 第六章 数组、指针与字符串 ...第十章 群体数据的组织 第十一章 流类库 第十二章 异常处理 第十三章 Windows应用程序设计基础

    C语言第一章概述

    第一章:C语言程序设计概述 2课时 第二章:基本数据类型与表达式 4课时 第三章:顺序程序设计 4课时 第四章:选择结构程序设计 4课时 ...第十章:共用体与枚举类型 4课时 第十一章:文件

    C语言程序设计考试大纲及试题类型2010级

    第一章:程序设计基本概念。主要掌握基本概念。 第二章:数据类型、运算符与表达式。重点掌握: 第三章:顺序结构程序设计...第十章:指针。主要掌握§10.1~§10.4的内容。 第十一章:结构体与共用体和用户定义类型。

    《C 语言程序设计》课件

    序言 第一章 程序设计概述 第二章 C语言的基本知识 第三章 顺序结构 第四章 选择结构 第五章 循环控制 第六章 模块化 第七章 数组 第八章 指针 第九章 编译预处理 第十章 文件

Global site tag (gtag.js) - Google Analytics