簡體   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

一個 C++ 程序,它讀取 100 名學生中獲得的分數。它還計算平均值; 最低和最高分。 然后顯示每個學生的分數與平均分數的差異。

這是我為這個問題編寫的代碼。 但是當我運行程序時,我得到的平均分和低分值為 0,而且平均分和獲得的分數的差異與我需要的不同。 有人可以解釋一下我缺乏的地方,我是編程新手。

#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;
}

它可以做得更好,嘗試改進它,這是一個很好的做法。

首先,您需要分別獲取所有輸入,然后將低和高初始化為第一個數字,然后通過數組 go 如果數字高於或低於當前高/低值,則更新您的高/低。

您的比較不正確您比較每兩個數字。 如果是 10 5 4 3,您的最大值將為 4。

#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