请帮助我如何解决这个问题......
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
int main(int iics, char*argv[]){
int a,n,sum=0,average=0,highest=0,lowest=0,num[100];
for (a=1;a<iics;++a)
sum= sum + atoi (argv[a]);
printf ("The sum is %.2d \n", sum);
average=sum/a;
printf ("The average is %.2d \n", average);
highest=num[0];
lowest=num[0];
for (a=1;a<num[100];++a){
if (num[a]>highest)
highest=num[a];
if(num[a]<lowest)
lowest=num[a];}
printf ("The highest element is %.2d \n",highest);
printf ("The lowest element is %.2d \n", lowest);
return 0;
}
我得到了总和和平均值,但最高和最低的是怎样的?
答案 0 :(得分:0)
您正在使用未初始化的数组num
。所以这样做
int i=0;
for (a=1;a<iics;++a)
{
sum= sum + atoi (argv[a]);
num[i++]=atoi9argv[a]);// just for an idea. Take care about i value
}
自己尝试......
答案 1 :(得分:0)
您希望从1
到iics-1
枚举您的其他循环,而不是从1
枚举到num[a]-1
变化:
for (a=1;a<num[100];++a){
为:
for (a=1;a<iics;a++){
在您之前的循环中,将atoi (argv[a])
分配给num[a]
。然后这些将需要从num[1]
开始:
highest=num[1];
lowest=num[1];
答案 2 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
int main(int iics, char*argv[]){
int a,n=0,sum=0,average,highest,lowest,num[100];
for (a=1; a<iics; ++a)
sum = sum + (num[n++] = atoi(argv[a]));
printf ("The sum is %.2d \n", sum);
average = sum / n;
printf ("The average is %.2d \n", average);
highest = num[0];
lowest = num[0];
for (a = 1; a < n; ++a){
if(num[a] > highest)
highest = num[a];
if(num[a] < lowest)
lowest = num[a];
}
printf ("The highest element is %.2d \n",highest);
printf ("The lowest element is %.2d \n", lowest);
return 0;
}