C-索引问题
所以我做了一个简单的排序算法,将数字按升序排列在一个数组中 这就是我的代码现在的样子:C-索引问题,c,C,所以我做了一个简单的排序算法,将数字按升序排列在一个数组中 这就是我的代码现在的样子: #include <stdio.h> #include <stdlib.h> int main(void){ int x,smallest,z,n,count; int * numbers = NULL; printf("Sisestage arvude hulk: "); scanf("%d",&n); numbers = mall
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x,smallest,z,n,count;
int * numbers = NULL;
printf("Sisestage arvude hulk: ");
scanf("%d",&n);
numbers = malloc(sizeof(int)*n);
if(numbers==NULL){
return -1;
}
for(count=0;count<n;count++){
printf("Arv %d: ",count+1);
scanf("%d",&numbers[count]);
}
smallest = numbers[0];
for(count=0;count<n;count++){
printf("Number %d Index %d\n",numbers[count],count);
if(numbers[count]<smallest)
smallest = numbers[count];
z = count;
}
/*numbers[0] = smallest;
numbers[z] = x;*/
printf("Smallest: %d, It's index: %d Array size: %d\n",smallest,z,n);
}
#包括
#包括
内部主(空){
整数x,最小值,z,n,计数;
int*numbers=NULL;
printf(“Sissetage arvude绿巨人”);
scanf(“%d”和“&n”);
数字=malloc(sizeof(int)*n);
如果(数字==NULL){
返回-1;
}
对于(count=0;count您忘记将if块括在大括号中:
if(numbers[count]<smallest) {
smallest = numbers[count];
z = count;
}
如果可以有多条语句,则需要在if
中使用括号{}
这应该可以解决问题
解决方案:
if(numbers[count]<smallest) {
smallest = numbers[count];
z = count; }
if(numbers[count]if(numbers[count]这就是你应该学习缩进的原因,这是给你的礼物哈…谢谢!如此愚蠢的打字错误和如此多的麻烦。嗯。haccks,是的,当我将索引括在括号中时,它似乎是一些随机数。使用正确的缩进格式(见答案),它看起来很明显。@Veske:您还必须初始化z。行末尾的尾随右大括号不是理想的布局。事实上,它确实令人反感。
if(numbers[count]<smallest) {
smallest = numbers[count];
z = count; }
if(numbers[count]<smallest)
smallest = numbers[count];
z = count;
if(numbers[count]<smallest)
{
smallest = numbers[count];
}
z = count;