国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言小程序之數組操作實例代碼

2020-02-24 14:37:35
字體:
來源:轉載
供稿:網友
為了能讓大家能循序漸進地學習C語言示例代碼,我們將從最基礎的C語言示例程序開始學習,那接下來先附上這篇C語言小程序之數組操作實例代碼,一起來
了解下其中的詳情吧。

#include
#include
#include
int size = 0;
int flag = 0;
void output(int *arry)
{
?int i = 0;
?for(i=0; i?{
??printf("arry[%d]=%d/t",i,arry[i]);
??if((i+1)%5 == 0)
???printf("/n");
?}
?printf("/n");
}
void getarry(int *arry)
{
?int i = 0;
?srand(time(NULL));
?for(i=0; i?{
??arry[i] = rand() % 100;
?}
}
void add(int *arry, int pos, int num)
{
?int i = 0;
?if(pos>=0 && pos?{
??if(pos ??{
???for(i=size; i>pos; i--)
???{
????arry[i] = arry[i-1];
???}
???arry[pos] = num;
??}
??else?????//在最后的位置插入
??{
???arry[size] = num;
??}
??size++;
?}
?else
??printf("只能在0-%d的位置插入。/n",size);
}
int search(int *arry, int num)
{
?static int pos = 0;
?if(flag)
??pos++;
?for(; pos?{
??if(arry[pos] == num)
??{
???flag = 0;
???return pos;
??}
?}
?return -1;
}
void mod(int *arry, int pos, int num)
{
?if(pos>=0 && pos?{
??arry[pos] = num;
?}
?else
?{
??printf("輸入位置錯誤。/n");
?}
}
int del(int *arry, int num)
{
?int count = 0;
?int pos = 0;
?int i = 0;
?pos=search(arry, num);
?while(pos+1)
?{
??for(i=pos; i??{
???arry[i] = arry[i+1];
??}
??count++;
??pos=search(arry, num);
?}
?return count;
}
int main()
{
?//pos 0到size-1
?int *arry = NULL;
?int count = 0;
?int pos = 0;
?int num = 0;
?printf("輸入要產生多少個隨機數:");
?scanf("%d",&size);
?arry = malloc(2*size*sizeof(int));
?getarry(arry);
?output(arry);
?printf("輸入要添加的位置(0-%d):",size);
?scanf("%d",&pos);
?printf("輸入要添加的數字:");
?scanf("%d",&num);
?add(arry, pos, num);
?output(arry);
?printf("輸入要查找的數字:");
?scanf("%d",&num);
?pos=search(arry, num);
?while(pos+1)
?{
??flag = 1;
??count++;
??printf("arry[%d]=%d/n",pos, num);
??pos=search(arry, num);
?}
?printf("共找到%d個匹配數字/n",count);
?printf("輸入要修改的位置:");
?scanf("%d",&pos);
?printf("輸入要修改為數字:");
?scanf("%d",&num);
?mod(arry, pos, num);
?output(arry);
?printf("輸入要刪除的數字:");
?scanf("%d",&num);
?del(arry, num);
?output(arry);
?free(arry);
?arry = NULL;
?return 0;
}

以上就是C語言小程序之數組操作實例代碼,如果大家想了解更多相關內容,請持續關注本站,本站小編將在第一時間為大家帶來更好的經典內容。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 舟曲县| 嘉禾县| 确山县| 广汉市| 内丘县| 筠连县| 连江县| 张家口市| 凤庆县| 西乌珠穆沁旗| 沈阳市| 敖汉旗| 五峰| 台北市| 新河县| 乌兰县| 扎囊县| 凌海市| 景谷| 齐齐哈尔市| 陆川县| 西安市| 武义县| 吴桥县| 碌曲县| 闻喜县| 玉门市| 定南县| 大港区| 田阳县| 汽车| 北京市| 屏山县| 洛南县| 澄城县| 丹阳市| 昭觉县| 东乡县| 图木舒克市| 长沙县| 从江县|