当前位置: 首页 > news >正文

C Primer Plus 第6版 编程练习——第10章(上)

本章共12踢,分上下两篇。

1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。

#define MONTHS 12 //一年的月份数
#define YEARS  5  //年数
int main(void)
{//用2010~2014年的降水量初始化数组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 }};float subtot = 0.0, total = 0.0;printf(" Year RAINFALL(inches)\n");for (int year = 0; year < YEARS; year++){subtot = 0.0;for(int month = 0; month < MONTHS; month++)subtot += *(*(rain + year) + month);printf("%5d %15.1f\n", year + 2010, subtot);total += subtot;}printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);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++){subtot = 0.0;for (int year = 0; year < YEARS; year++){subtot += *(*(rain + year) + month);}printf(" % 4.1f ", subtot / YEARS);}printf("\n");return 0;
}

2.编写一个程序,初始化一个double数组,然后把该数组的内容拷贝至3个其他数组中(在main()中声明这4个数组)。使用带数组表示发的函数进行第1份拷贝。使用带指针表示法和指针递增的函数进行第2份拷贝。把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。第3个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素指针。也就是说,给定以下声明,则函数调用如下所示:
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);

void copy_arr(double target[], double source[], int size)
{for (int i = 0; i < size; i++){target[i] = source[i];}
}
void copy_ptr(double* target, double* source, int size)
{for (int i = 0; i < size; i++){*target++ = *source++;}
}
void copy_ptrs(double* target, double* source, double* end)
{for (double *p = source; source < end; source++){*target++ = *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 i = 0; i < 5; i++){printf("%f %f %f\n", target1[i], target2[i], target3[i]);}return 0;
}

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

int max_array(int a[], int n)
{int max = a[0];for (int i = 1; i < n; i++){if (a[i] > max){max = a[i];}}return max;
}
int main()
{int a[] = { 1, 2, 3, 7, 8, 9, 10, 4, 5, 6 };printf("max:%d\n", max_array(a, 10));
}

4.编写一个函数,返回存储在double类型数组中最大值的下标,并在一个简单的程序中测试该函数。

int max_array(double a[], int n)
{int max_idx = 0;for (int i = 1; i < n; i++){if (a[i] > a[max_idx]){max_idx = i;}}return max_idx;
}
int main()
{double a[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };printf("The index of max value is %d\n", max_array(a, 10));
}

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

double max_min_diff(double a[], int n)
{double max = a[0];double min = a[0];for (int i = 1; i < n; i++){if (a[i] > max){max = a[i];}else if (a[i] < min){min = a[i];}}return max - min;
}
int main()
{double a[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };printf("max - min : %.2f", max_min_diff(a, 10));
}

6.编写一个程序,把double类型数组中的数据倒叙排列,并在一个简单的程序中测试该函数。

void DescSort(double a[], int n)
{ for (int i = 0; i < n - 1; i++){for (int j = i + 1; j < n; j++){if (a[i] < a[j]){double temp = a[i];a[i] = a[j];a[j] = temp;}}}
}
int main()
{double a[5] = { 2.2, 1.1, 4.4, 5.5, 3.3 };DescSort(a, 5);for (int i = 0; i < 5; i++){printf("%f ", a[i]);}printf("\n");
}

http://www.dtcms.com/a/293866.html

相关文章:

  • 漫画机器学习播客对话图文版
  • TGD第二篇:破局——去除导数计算中的无穷小极限
  • CodeBuddy IDE实战:用AI全栈能力快速搭建课程表网页
  • 【机器学习深度学习】比较 LLaMA-Factory、vLLM 和 LMDeploy 的量化导出:为何 LLaMA-Factory 不是首选?
  • 2025暑期—06神经网络-常见网络3
  • UWA DAY 2025 游戏开发者大会|全议程
  • Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
  • Python进阶知识之pandas库(一)基础数据类型
  • 论文略读:REMEDY: RECIPE MERGING DYNAMICS IN LARGE VISION-LANGUAGE MODELS
  • 深入解析预训练语言模型在文本生成中的革命性应用:技术全景与未来挑战
  • 【Elasticsearch】跨集群检索(Cross-Cluster Search)
  • 18.设备虚拟化
  • Java 堆(优先级队列)
  • Linux基本指令:掌握系统操作的钥匙
  • Unity3D性能优化全攻略
  • 原创-基于 PHP 和 MySQL 的证书管理系统 第三版
  • OpenLayers 快速入门(四)View 对象
  • springboot苍穹外卖实战:十二、添加购物车+查看购物车+清空购物车
  • React学习——美团小案例——Day3
  • 一种集合式方法:实现高效且有效的大语言模型零样本排序
  • PHP:经典与现代交织的编程语言,持续赋能Web开发
  • 浙大Fast Lab:融合3D激光雷达与强化学习的「端到端导航」,让无人机“飞”在点云上!
  • javaSE(List集合ArrayList实现类与LinkedList实现类)day15
  • OSPF(多区域)
  • Android14 锁屏密码修改为至少6位
  • 开源深度学习新宠:Burn框架助您无忧高效建模
  • USB4.0:开启高速数据传输的新时代
  • Upload-Labs通关全攻略详细版
  • Keepalived高可用模型
  • [matlab]matlab上安装xgboost安装教程简单版