美文网首页C语言入门经典
第五章数组的程序

第五章数组的程序

作者: 全無 | 来源:发表于2016-07-28 11:22 被阅读35次

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

相关文章

  • 第五章数组的程序

    1 、没有使用数组的程序 编写一个程序,计算十个同学的平均分(使用FOR循环)记住名词:grade count ...

  • 【C语言】 数组和字符串-005

    第五章 数组和字符串 5.1 概述 在程序设计中,为了方便处理数据把具有相同类型的若干变量按有序形式组织起来——称...

  • 深入理解es6(附代码-中)

    深入理解es6 ES6 第五章:数组的空位 数组的空位,数组的某个索引位置没有任何值,undefined不是空位 ...

  • 【Java】数组

    声明数组变量 首先必须声明数组变量,才能在程序中使用数组。下面是声明数组变量的语法: 实例 创建数组 Java语言...

  • 【Java】基础篇之数组-005

    第五章 数组 5.1 数组定义和访问 5.1.1 容器概述 1.案例分析 现在需要统计某公司员工的工资情况,例如计...

  • 微信小程序bug

    1.小程序云开发移动端数组使用splice删除某些数据时,数据传输到后台时被删除的数组会变成null 小程序数组 ...

  • 数组的定义与使用

    数组的定义数组类型 数组名称[] = new 数组类型[长度]; 数组的初始化 静态初始化:初始化时由程序员显示...

  • Objective-C runtime 方法交换

    防止数组为nil 程序崩溃

  • logback官方文档中文翻译第五章:Encoder

    第五章:Encoder encoder 将日志事件转换为字节数组,同时将字节数组写入到一个 OutputStrea...

  • Swift入门 - 数组实战

    数组作为程序中最常使用的对象之一,在任何程序语言中数组的重要性不言而喻,在Swift中也不例外。 数组是相同数据类...

网友评论

    本文标题:第五章数组的程序

    本文链接:https://www.haomeiwen.com/subject/qcgtrttx.html