这是练习使用一维数组的一个小程序,笔者编程菜鸡,只能先写个这个了,因为笔者在写这个程序之前连一维数组都不会用。
1.一维数组:(数组名称由num代替)
现在假如你定义了yi个数组num[N]
(1)数组的定义方式:
第一部分:num,变量值为num的num[0]地址
第二部分:num[0~N-1],每个数组由num[0]开始,值就是数组的元素
(2)数组的存储
首地址:num存储num[0]地址
数组地址:num[i]的地址为num[0](num)+数据类型大小*i
数据:num[i]存储数据
(3)数组初始化
初始化方式 | 初始化理由 |
---|---|
num[3]={1,2,3}; | 完全定义 |
num[3]={0}; | 全0 |
num[]={1,2,3}; | 位置位数,自动识别,但内容必须有 |
num[3]={1}; | 除了首位外,其余全0 |
2.下面给出代码()
输入:10个人的成绩
输出:最高分以及位置
#include
#include
int main() {
const int n = 10;//定义
int i=0, maxstu=0;
float score[n] = {0}, maxsco = 0;
for (i = 0; i < n; i++) {//键盘输入方式获得输入10个人的成绩
scanf_s("%f",score+i);
}
for (i = 0; i < n; i++) {//求最大值算法
if (score[i] > maxsco) {
maxsco = score[i];
maxstu = i + 1;
}
}
printf("the maxscore is %.2f,the maxstudent is %d\n",maxsco,maxstu);//输出
//system("pause");//程序暂停
return 0;
}
stdlib.h(system("pause");)
stdio.h(printf和scanf_s)