简体   繁体   English

一个 C++ 程序,它读取 100 名学生中获得的分数。它还计算平均值; 最低和最高分

[英]A C++ Program that reads the marks obtained of ten students out of 100. It also computes the average; lowest and highest marks

A C++ Program that reads the marks obtained of ten students out of 100. It also computes the average;一个 C++ 程序,它读取 100 名学生中获得的分数。它还计算平均值; lowest and highest marks.最低和最高分。 Then shows the difference of marks of every student from the average marks.然后显示每个学生的分数与平均分数的差异。

Here goes the code written by me for this problem.这是我为这个问题编写的代码。 But when I run the program I am getting value of average and low marks as 0 and also the difference of average and marks obtained is not coming the same what I needed it to be.但是当我运行程序时,我得到的平均分和低分值为 0,而且平均分和获得的分数的差异与我需要的不同。 Can someone explain me that where I am lacking, I am a novice in programming.有人可以解释一下我缺乏的地方,我是编程新手。

#include<iostream>
using namespace std;

int main(){
int marks[10],avg=0,high,low;
cout<<"Enter the marks of 10 students:";
for(int i=0;i<10;i++){
  cin>>marks[i];
  if(marks[i]>marks[i+1]){
    low=marks[i+1];
  }
  else 
  {
    low=marks[i];
  }
  if(marks[i]>marks[i+1]){
    high=marks[i];
  }
  else 
  {
    high=marks[i+1];
  }
  
  avg=avg+(marks[i])/100;
}
for(int i=0;i<10;i++){
cout<<"Marks obtained by "<<i+1<<" student:"<<marks[i]<<endl;
  cout<<"Difference of marks of student "<<i+1<<" with average marks:";
  
  if((avg-marks[i])>0){cout<<avg-marks[i];}
  else{cout<<marks[i]-avg;}
  cout<<endl<<endl;
  }
cout<<"Lowest marks:"<<low<<endl;
cout<<"Highest marks:"<<high<<endl;
cout<<"Average marks:"<<avg<<endl;
return 0;
}

It can be done even better, try to improve it, it's a good practice.它可以做得更好,尝试改进它,这是一个很好的做法。

first you need to get all the input separately, then you init your low and high as the first number, then you go through the array and if number is higher or lower from the current value of high/low you update you high/low.首先,您需要分别获取所有输入,然后将低和高初始化为第一个数字,然后通过数组 go 如果数字高于或低于当前高/低值,则更新您的高/低。

your compare was incorrect you compare each two numbers.您的比较不正确您比较每两个数字。 in case of 10 5 4 3 your max will be 4.如果是 10 5 4 3,您的最大值将为 4。

#include using namespace std; #include 使用命名空间标准;

int main(){
int marks[10],avg=0,sum =0 ,high,low=;
cout<<"Enter the marks of 10 students:";
for(int j=0;j<10;j++){
  cin>>marks[j];
 }
 high = marks[0],low = marks[0];
 for(int i=1;i<10;i++){ 
  if (marks[i] > high){
      high = marks[i];
  }
  else if(marks[i] < low){
      low = marks[i];
  }
  sum=sum+(marks[i]);
 }
 avg = sum /10;
for(int i=0;i<10;i++){
cout<<"Marks obtained by "<<i+1<<" student:"<<marks[i]<<endl;
  cout<<"Difference of marks of student "<<i+1<<" with average marks:";
  
  if((avg-marks[i])>0){cout<<avg-marks[i];}
  else{cout<<marks[i]-avg;}
  cout<<endl<<endl;
  }
cout<<"Lowest marks:"<<low<<endl;
cout<<"Highest marks:"<<high<<endl;
cout<<"Average marks:"<<avg<<endl;
return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM