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);
}
网友评论