第五章 陣列程式(Ivor Horton)
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;
}
`````
相關文章
- 第五章 陣列Ivor Horton陣列
- 第一章 C語言程式設計(Ivor Horton)C語言程式設計
- 小程式 - 陣列匹配陣列
- 第五章 字串專題 ---------------- 字串匹配(三)----字尾陣列演算法字串匹配陣列演算法
- javascript將類陣列轉換為陣列程式碼例項JavaScript陣列
- JavaSE 陣列:一維陣列&二維陣列Java陣列
- 《Java從入門到失業》第三章:基礎語法及基本程式結構(3.9):陣列(陣列基本使用、陣列的迴圈、陣列拷貝、陣列排序、多維陣列)Java陣列排序
- 陣列,陣列類,SyStem類陣列
- 陣列結構之陣列陣列
- Java陣列03:陣列使用Java陣列
- 陣列1——求一個陣列的最大子陣列陣列
- 陣列--移除陣列中指定的元素,不改變原陣列和改變原陣列陣列
- 陣列二:使用陣列可變函式為陣列排序陣列函式排序
- 指標陣列和陣列指標與二維陣列指標陣列
- jQuery陣列相關操作程式碼jQuery陣列
- js陣列求和程式碼例項JS陣列
- 二維陣列程式碼案例分析陣列
- Javascript - 陣列和陣列的方法JavaScript陣列
- 陣列指標,指標陣列陣列指標
- 陣列指標 指標陣列陣列指標
- C語言程式設計基礎:指標陣列與陣列指標C語言程式設計指標陣列
- 陣列陣列
- 記一次陣列操作:陣列 A 根據陣列 B 排序陣列排序
- [求解]陣列,分成倆個陣列,陣列值之和的相差最小。陣列
- 矩陣和陣列矩陣陣列
- Rust與Java程式碼比較:將二維陣列轉為三維陣列RustJava陣列
- 一句程式碼搞定陣列去重(去除陣列中的重複元素)陣列
- PHP陣列轉換為js陣列PHP陣列JS
- iOS 字典轉陣列,陣列轉字典iOS陣列
- 2-7 陣列:動態陣列陣列
- scala陣列與java陣列對比陣列Java
- 指標陣列與陣列指標指標陣列
- 陣列指標和指標陣列陣列指標
- 陣列演算法-差分陣列陣列演算法
- 【程式碼隨想錄】一、陣列:5.螺旋矩陣陣列矩陣
- c程式設計--指標與陣列C程式程式設計指標陣列
- 小程式 - 陣列追加兼本地儲存陣列
- 過濾陣列元素程式碼例項陣列