第五章 陣列程式(Ivor Horton)

Carb_5683發表於2016-10-26

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");
   else
      printf("\nYou, in technical parlance, are a fathead."
                                                                "No hat for you, I'm afraid.\n”);
}
 return 0;
}
`````

相關文章