因此,他们告诉我制作一个阵列程序,只有一个尺寸也加起来所以我的代码是我的代码 [英] so they told me to make a array program and only one dimention that also adds up so heres my codes i

查看:66
本文介绍了因此,他们告诉我制作一个阵列程序,只有一个尺寸也加起来所以我的代码是我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助你可以查看我的代码是不是错了它仍然在C中而不是在C ++中



i只是想看到这个



总数:313



最低数字:0



但它一直这样做



总人数:79

总人数:79

总人数:80
总人数:86

总人数:166

总人数:231

总人数:281

总人数:295

总人数:302

总人数:313



最低人数: 0









 < span class =code-keyword> #include   <   stdio.h  >  
#include < conio.h >
void main(){

int x [ 10 ] = { 79 0 2 5 80 65 50 14 7 11 };
int i;
int total = 0 ;
int 最低= 0 ;

for (i = 0 ; i< 10; i ++){
total = total + x [i];
printf( \ n \ n总数为%i,总计) ;
}

for (i = 0 ; i< 10 ; i ++){
if (最低> = x [i]){
printf( \ n \最小数字是:%i,最低);

}
}
getch();
}

解决方案

你的 printf 来电是 循环,因此它们被多次调用。将这些调用保留在循环之外。此外,最小数字的循环将始终输出0,因为最低的初始值为零。改为将其设置为第一个数组元素,当数字刚好低于前一个时,不要立即打印最低数字是...;仍然可能会有较低的一个。此外,您不必为此创建两个循环:您可以将两个循环合并为一个:

  int  total =  0 ; 
int 最低= x [i];

for (i = 0 ; i< 10 ; i ++){
total = total + x [i];
if (最低> x [i]){
最低= x [i];
}
}

printf( \ n \ n总数是%i,总数);
printf( \ n \最小数字为:%i,最低);


need help can u check my code whats wrong with it were still in C not in C++

i just want to see this

total number: 313

lowest number: 0

but it keeps doing this

total number:79
total number:79
total number:80
total number:86
total number:166
total number:231
total number:281
total number:295
total number:302
total number:313

lowest number: 0




#include <stdio.h>
#include <conio.h>
void main(){

    int x[10]={79,0,2,5,80,65,50,14,7,11};
   int i;
   int total=0;
   int lowest=0;

   for(i=0;i<10;i++){
      total= total + x[i];
        printf("\n\nThe total number is %i", total);
      }

   for(i=0;i<10;i++){
        if(lowest >= x[i]){
      printf("\n\nthe lowest number is: %i", lowest);

      }
    }
   getch();
   }

解决方案

Your printf calls are inside the loop, so they get called multiple times. Keep these calls outside the loops. Also, the loop for the lowest number would always output 0 because the initial value of lowest is zero. Set it to the first array element instead, and don't immediately print "the lowest number is..." when the number is just lower than the previous; there still might be a lower one. Additionally, you don't have to make two loops for this: you can just combine the two loops into one:

int total = 0;
int lowest = x[i];

for(i = 0; i < 10; i++) {
    total = total + x[i];
    if (lowest > x[i]) {
        lowest = x[i];
    }
}

printf("\n\nThe total number is %i", total);
printf("\n\nthe lowest number is: %i", lowest);


这篇关于因此,他们告诉我制作一个阵列程序,只有一个尺寸也加起来所以我的代码是我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆