1 、没有使用数组的程序

编写一个程序,计算十个同学的平均分(使用FOR循环)
记住名词:grade count sum average

//program 5.1 Averaging ten grades without storing them
#include<stdio.h>int main(void)
{int grade = 0;unsigned int count =10;long sum = 0L;float average = 0.0f;for(unsigned  int i=0 ; i <count; ++i){printf("Enter a grade:");scanf("%d",&grade);sum += grade;}average = (float)sum/count;printf("\nAverage of the ten grades entered is: %f\n", average);return 0;
}

没有存储分数,无法重复使用

试试看:使用数组计算平均分

//program 5.2 Averaging then grades - storing values the hard way
#include<stdio.h>int main (void)
{int grade0 = 0, grade1 =0,grade2 =0,grade3=0,grade4=0;int grade5=0,int grade6 =0,int grade7 =0, int grade8=0, int grade9=0;long sum = 0L; float average =0.0f;printf("Enter the first five grades,\n")printf("use a space or press Enter between each number.\n")scanf("%d%d%d%d%d",&grade0,&grade1,&grade2,&grade3,&grade4);printf("Enter the last five numbers in the same manner.\n");scanf("%d%d%d%d%d",&grade5,&grade6,grade7,grade8,&grade9);sum = grade0+grade1+grade2+grade3+grade4+grade5+grade6+grade7+grade8+grade9;
average =(float)sum/10.f;printf("\nAverage of the ten grades entered is: %f\n",average);return 0;
}

存储所有的分数,声明10个整数变量来存储分数

3、试试看:使用数组计算平均分

使用数组可以存储所有要平均的分数即存储所有的分数,以便重复使用他们,现在重写这个程序,计算10个分数的平均值:

//program 5.3 Averaging ten grades - storing the values the easy way
#include<stdio.h>int main(void)
{int grades[10];//声明数组unsigned int count =10;long sum =0L;float average = 0.0f;printf("\nEnter the 10  grades:\n");for(unsigned int i =0; i< count ; ++i){printf("%2u> ",i +1);scanf("%d", &grades[i]);sum += grades[i];}average = (float)sum/count;printf("\nAverage of the ten grades entered is: %.2f\n", average);return 0;
}

4、试试看:检索元素值

现在这个程序显示所有输入的值,把这些值存储在数组中,就可以随时用各种不同的方法访问和处理他们。

//program 5.4 Reusing the numbers stored
#include<stdio.h>
int main(void)
{int grades[10];unsigned int count = 10;long sum = 0L;float average = 0.0f;printf("\nEnter the 10 grades:\n");for(unsigned int i = 0; i < count; ++i){printf("%2u> ", i + 1);scanf("%d", &grades[i]);sum += grades[i];}average = (float)sum/count;for(unsiged int i =0; i < count; ++i)printf("\nGrade Number %2u is %3d", i + 1, grades[i]);printf("\n Average of the grades entered is :%.2f\n",average);return 0;
}

5、试试看: 使用寻址运算符

//program 5.5 Using the & operator
#include <stdio.h>int main(void)
{long a =2L;long b =3L;long c =3L;double a =4.0;double d =5.0;double c =6.0;printf("A variable of type long occupies %u bytes.", sizeof(long));printf("\nHere are the addresses of some variables of type long: ");printf("\nThe address of a is: %p The address of b is: %p", &a, &b);printf("\nThe address of c is: &p", &c);printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));printf("\nHere are the addresses of  some Variables of type double: ");printf("\nThe address of  d  is: %pThe address of e is: %p", &d, &e);printf("\nThe address of  f is: %p\n",&f);return 0;
}

7、试试看:使用变长数组

但这次数组包含的实际分数是输入的

//program 5.7 Averaging a variable number of grades
#include<stdio.h>int main(void)
{size_t   nGrades = 0;printf("Enter the number of grades: ");scacnf("%zd", &nGrades);int grades[nGrades];long sum =0L;float average =0.0f;printf("\Enter the number of grades:");for(size_t i =0; i< nGrades; ++i) {printf("%2zd> ",i +1);scanf("%d", &grades[i]);sum += grades[i];}printf("The grades you entered are:\n");for(size_t i=0; i <nGrades ; ++i){printf("Grade[%2zd] = %3d ", i+1, grades[i]);if((i+1) % 5 == 0)printf("\n");}average =float)sum/nGrades;printf("\nAverage of the %zd grades entered is : %.2f\n", nGrades. average);}

6、 试试看:多维数组
对于这个应用程序,只需要输入帽子的周长(英寸),然后,显示帽子的尺寸。

//program 5.6 Know your hat size - if you dare....
#include <stdio.h>
#include <stdbool.h>int main (void)
{char size[3][12] = {{'6', '6', '6', '6', '7', '7', '7', '7','7', '7', '7', '7'},{'1', '5', '3', '7', ' ', '1', '1', '3', '1', '5', '3', '7'},{'2', '8', '4', '8', ' ', '8', '4', '8', '2', '8', '4', '8'},
};
int  headsize[12] = {164,166,169,172,175,178,181,184,188, 191,194,197};
float cranium = 0.0f;
int your_head = 0;
bool hat_found = false;printf("\nEnter the circumference of your head above your eyebrows "  "in inches as  a decimal value : ");
scanf(" %f",&cranium);your_head = (int)(8.0f*cranium);size_t i = 0;
if(your_head == headsize[i])hat_found = true;
else
{for ( i =1 ; i < sizeof(headsize) ;  ++i){if( your_head > headsize[i -1] && your_head <= headsize[i]){hat_found = true;break;}}
}
if(hat_found)
{printf(“\nYour hat size is %c %c%c%c\n",size[0][i], size[1][i],(size[1][i] ==' ') ? ' ' : '/', size[2][i]);
}
else
{if(your_head < headsize[0])printf("\nYou are the proverbial pinhead,NO hat for" "you I'm afraid.\n");elseprintf("\nYou, in technical parlance, are a fathead.""No hat for you, I'm afraid.\n”);
}return 0;
}
`````

第五章 数组程序(Ivor Horton)相关推荐

  1. 【JAVA SE】第五章 数组、多维数组和Arrays类

    第五章 数组.多维数组和Arrays类 文章目录 第五章 数组.多维数组和Arrays类 一.数组 1.概述 2.声明数组变量 3.创建数组 4.For-Each 循环 二.多维数组 1.概述 2.多 ...

  2. 信息学奥赛一本通(C++版)第一部分 C++语言 第五章 数组

    第五章 数组 第一节 一维数组 T1102 : 与指定数字相同的数的个数 时间限制: 1000 ms 内存限制: 65536 KB [题目描述]   输出一个整数序列中与指定数字相同的数的个数. [输 ...

  3. 第五章 数组Ivor Horton

    5.1数组简介 说明数组的概念的及其作用的最好方法,是通过一个例子,来说明使用数组后程序会变得非常简单 5.1.1 不用数组的程序 如何声明一个数组 数据类型 数组名称[长度]; C语言中的数组初始化 ...

  4. 程序员代码面试指南刷题--第五章.数组中两个字符串的最小距离

    题目描述 给定一个字符串数组strs,再给定两个字符串str1和str2,返回在strs中str1和str2的最小距离,如果str1或str2为null,或不在strs中,返回-1. 输入描述: 输入 ...

  5. 第五章 数组和指针的关系

    如果对声明数组的语句不太明白的话,请参阅我前段时间贴出的文章<<如何 理解c和c++的复杂类型声明>>.     数组的数组名其实可以看作一个指针.看下例: 例八:       ...

  6. Java基础-第五章(数组高级)

    一.方法参数的值传递机制 Java方法的参数传递只有一种: 值传递,所谓值传递,就是将实际参数值的副本,传入方法内,而参数本身不会收到任何影响; main方法的数组参数 public class He ...

  7. 深入理解计算机系统第五章------优化程序性能

    课程网站 引言 编写高效程序的要点: 1,选择适当的算法和数据结构:2,编写出编译器能够有效优化以转换成高效可执行代码的源代码:3,将计算量特别大的任务分解成多个部分,使其可以在多核或者多处理器上并行 ...

  8. JAVA-初步认识-第五章-数组-常见操作-最值

    一. 最值 数组的操作是对数组中的元素进行操作,有时需要对元素的位置重新进行安排,有时仅需要提供题目需要的那个数据. 在视频没有讲解怎么做之前,我先提供一个思路.得出最值肯定是一个一个地比较出来的,这 ...

  9. JAVA-初步认识-第五章-数组-常见操作-进制转换(查表法)

    一. 数组的常见应用 数组在开发中什么时候用? 举例说明: 需求:获取一个整数的十六进制表现形式(要明白十六进制的表现形式是什么样子,有数字有字母) 本来应该返回一个字符串,但是还没有学到返回字符串, ...

最新文章

  1. 在网络通讯中应用Protobuf
  2. 人脸关键点 HRNet-Facial-Landmark-Detection
  3. 重构与模式:改善代码三部曲中的第三部
  4. 《系统集成项目管理工程师》必背100个知识点-98大数据的特点
  5. 2017 多校2 hdu 6053 TrickGCD
  6. 正则表达式替换排除特定情况
  7. java日期时间轴_生成时间轴数据, 从当前 年月日 往前推到几天的日期(前一周时间、、、)...
  8. 微软服务器最高版本,企业用的最多的服务器是Windows Server 那个版本,当前,,,
  9. 【2017-12-06】c#基础-分支语句and循环语句
  10. “ORA-01017(:用户名/口令无效; 登录被拒绝)” ORA-28000: the accout is locked(用户账号被锁)解决方法
  11. mysql根据经纬度计算距离并排序
  12. DSP BIOS任务通信和同步(pend/post举例)
  13. 数据库连接超时和go away、如何检测数据库的最大连接数
  14. Spring MVC集成Swagger2.0
  15. sudo vim找不到
  16. 用于优化广告展示的深度强化学习实践
  17. 复旦发布报告!70后度过中年危机?80后珍惜家庭?90后高压期来临?
  18. 织梦教程新建ID丨重置栏目ID丨文章ID不从1开始的解决方法
  19. 基于数据安全的风险评估-脆弱性识别
  20. php gif裁剪,ci裁剪gif图片如何让gif保持是动态的。

热门文章

  1. Moodle LMS 开源教育系统
  2. python打印实心菱形图案_python输入三角形三边求面积|python打印实心菱形图案
  3. 不怕新歌有多嗨,就怕老歌带DJ,Python批量对DJ歌曲进行下载
  4. 【react实战小项目:笔记】用React 16写了个订单页面
  5. Ch11. Threads 线程
  6. Not allowed to access vertices on mesh ‘Combined Mesh (root: scene)
  7. 消费升级背景下零食行业发展报告_上海日报奥纬陈闻:疫情之下,“小”零食,“大”产业...
  8. endnoteX9导入英文PDF文献,可以识别Doi,但无法显示参考文献信息,如何解决?
  9. 深圳南山区学位申请特殊住房需要的材料有哪些
  10. 每日分享 环境报错:Exception in thread “main“ java.lang.RuntimeException: Cannot create staging directory