第五章数组的程序

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

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

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");
   else
      printf("\nYou, in technical parlance, are a fathead."
                                                                "No hat for you, I'm afraid.\n”);
}
 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);
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,969评论 19 139
  • 特别说明,为便于查阅,文章转自https://github.com/getify/You-Dont-Know-JS...
    杀破狼real阅读 3,247评论 0 0
  • 官方中文版原文链接 感谢社区中各位的大力支持,译者再次奉上一点点福利:阿里云产品券,享受所有官网优惠,并抽取幸运大...
    HetfieldJoe阅读 6,848评论 1 21
  • 今天和合伙人聊了很多,有种茅塞顿开的感觉。合伙人是我的老乡,现在在老家,我从大学的时候出来,就没有在家待过,对老家...
    闵伟阅读 4,132评论 0 0
  • 文/晴天过后上一章 目录 等雨娴给木识治疗完,已是深夜,红豆递过来一块热毛巾,雨娴有些惊讶,红豆笑了笑...
    晴天过后阅读 4,446评论 6 22

友情链接更多精彩内容