你好程序员请看看这个代码,并告诉我在哪里问题为什么我无法得到最小指数我必须用数组出错逻辑错误请有人跟踪。 谢谢你的进步
#include<iostream>
using namespace std;
int main()
{
int number = 0;
int sum = 0;
int average = 0;
int loop = 0;
int total = 0;
int counter = 1;
int minimum = 0;
int maximum = 0;
int minimumIndex = 0;
int maximumIndex = 0;
cout<<"please enter Loop Limit\n";
cin>>loop;
total = loop;
while (loop > 0)
{
cout<<"please enter value "<< counter<<" ";
cin>>number;
if(minimum == 0)
{
minimum=number;
}
else if (number<minimum)
{
minimum=number;
minimumIndex = counter;
}
else if(number > maximum)
{
maximum = number;
maximumIndex= counter;
}
sum = sum + number;
counter++;
loop--;
}
cout<<"Sum of Entered Values = " << sum<<endl;
average=sum/total;
cout<<"Average Values = "<< average<<endl;
cout<<"Minimum Value = "<<minimum<<endl;
cout<<"Minimum Value Enter At Index "<<minimumIndex<<endl;
cout<<"Maximum Value = "<<maximum<<endl;
cout<<"Maximum Value Enter At Index "<<maximumIndex<<endl;
return 0;
}
答案 0 :(得分:0)
这会有所帮助:
#include<iostream>
using namespace std;
int main()
{
int number = 0;
int sum = 0;
int average = 0;
int loop = 0;
int total = 0;
int counter = 1;
int minimum = INT_MAX; //initially set to Maximum value to find minimum
int maximum = INT_MIN; //Similarily
int minimumIndex = 0;
int maximumIndex = 0;
cout<<"please enter Loop Limit\n";
cin>>loop;
total = loop;
while (loop > 0)
{
cout<<"please enter value "<< counter<<" ";
cin>>number;
//check for minimum
if (number<minimum) {
minimum=number;
minimumIndex = counter;
}
//exclusive check for maximum
if(number > maximum) {
maximum = number;
maximumIndex= counter;
}
sum = sum + number;
counter++;
loop--;
}
cout<<"Sum of Entered Values = " << sum<<endl;
average=sum/total;
cout<<"Average Values = "<< average<<endl;
cout<<"Minimum Value = "<<minimum<<endl;
cout<<"Minimum Value Enter At Index "<<minimumIndex<<endl;
cout<<"Maximum Value = "<<maximum<<endl;
cout<<"Maximum Value Enter At Index "<<maximumIndex<<endl;
return 0;
}