概述
在C中使用数组,必须先定义
类型说明符 数组名 [常量表达式];
例子:
- int a[10]; /* 说明整型数组a,有10个元素 */
注意
1.数组名和变量名不能重复。
int a[10];//不合法
int a;//不合法
2. a[10]方括号内的常量表达式10表示有10个元素,而数组是从下标a[0];开始的,所有a[10]包含。
a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]
3.不能用变量来表示,数组元素的个数,常量表达式和符号常数。
合法:
//合法
#define ARR 5
int a[ARR];//合法
不合法:
//不合法,即使编译器可能编译通过
int ARR=5;
int a[ARR];//不合法
4.必须先定义数组,才能使用下标变量。
赋值
int a[10]={ 0,1,2,3,4,5,6,7,8,9 };
其中, 如给全部元素赋值,则在数组说明中,可以不给出数组元素的个数。
int a[]={1,2,3,4,5};//是被允许的
应用1
题目:输出0或1或2.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
//int a[3]={1,2,3};
int a,b;
int c[3];
srand((unsigned)time(NULL));
for(a=0;a<10;a++)
{
b=rand()%3;
// printf("输出=%dn",a[2]);
c[a]=b;
printf("随机数输出=%dn",c[a]);
}
// printf("随机数输出=%d",c[2]);
return 0;
}
运行结果:
!!!!反复执行!!!!
问题出在哪里?
可以看到a在for中循环了10次,然而c数组中只定义了三个元素。
修改数组的元素个数后
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
//int a[3]={1,2,3};
int a,b;
int c[10];
srand((unsigned)time(NULL));
for(a=0;a<10;a++)
{
b=rand()%3;
// printf("输出=%dn",a[2]);
c[a]=b;
printf("随机数输出=%dn",c[a]);
}
// printf("随机数输出=%d",c[2]);
return 0;
}
运行结果:
问题1其他
1.随机数的产生
2.time函数的运用
3.范围随机数后的取值的问题,以取0,1,2,3为例子
1.1srand和rand的使用,rand取随机数,srand播种。
1.2srand在播种比较好的是用time()函数,time函数需要time.h头文件
2.time()函数的运用
srand((unsigned)time(NULL));//播种
3.范围随机数后的取值的问题,以取0,1,2,3为例子
valume_rand012=rand()%3;//余3,可以得到0,1,2,那如果是想到的1,2,3那?
valume_rand123=rand()%3+1;//
应用问题2 printf用数组输出字符串。
例子:
#include<stdio.h>
int main()
{
char strings[]={73,32,99,97,110,32,100,111,32,105,116,33};
printf("%sn",strings);
return 0;
}
输出结果:
其他
1.%s可以输出字符串。//%s可以输出字符串
2.ASCII值的一个运用,在char的数组中直接用。
有意思的运用:
刷题例子:
#include<stdio.h>
int main()
{
char a[10];
scanf("%s",a);
printf("%c%c%c%c",a[3],a[2],a[1],a[0]);
return 0;
}
输出结果:
其他:
1.字符和字符串的一个理解,这个地方把1234当成一个字符串,当每个元素数字到数组中,实际也是每个字符元素到数组中,取的时候%c取出去。
3数组形式字符串格式
char str[]="ROBOT";//字符用""约定起来
两种格式打印
/*两种打印格式*/ printf("%s",str);//第一种格式打印 while(str[i]) { putchar(tolower(str[i]));//第二种格式打印 i++; }
那么问题就来了,我可以这样去写吗?
printf("%c",tolower(str));
备注:tolower是大写转小写的函数,调用可以直接大变小。
试试?
error!
编译器告诉我们什么事情?
expected "int"but argument is of type "char".
翻译下,tolower()函数返回值是个整数,%s需要输入一个字符型。
4.
#include <stdio.h>
int main()
{
int arr[50]={0};//数组这边定义尽量加上{0}
int i=0,s,j,k,box;
int student;
scanf("%d",&student);
getchar();
for(i=0;i<student;i++)
{
scanf("%d",&arr[i]);
}
for(k=0;k<student;k++)//冒泡算法
{
for(i=0;i<(student-k-1);i++)
{
if(arr[i]<arr[i+1])//这边能到个11
{
box=arr[i+1];
arr[i+1]=arr[i];
arr[i]=box;
}
}
}
for(i=0;i<5;i++)//输出前5个
{
printf("%d ",arr[i]);
}
return 0;
}
应用5
scanf("%d",&arr[i]);//数组这个位置也要有取址的,arr是首地址
最后
以上就是冷酷项链为你收集整理的C语言数组(一维数组)在C中使用数组,必须先定义注意赋值应用1其他 有意思的运用:其他: 3数组形式字符串格式 两种格式打印那么问题就来了,我可以这样去写吗?备注:tolower是大写转小写的函数,调用可以直接大变小。 试试?error!编译器告诉我们什么事情?expected "int"but argument is of type "char".4.应用5的全部内容,希望文章能够帮你解决C语言数组(一维数组)在C中使用数组,必须先定义注意赋值应用1其他 有意思的运用:其他: 3数组形式字符串格式 两种格式打印那么问题就来了,我可以这样去写吗?备注:tolower是大写转小写的函数,调用可以直接大变小。 试试?error!编译器告诉我们什么事情?expected "int"but argument is of type "char".4.应用5所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复