Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is wrong with this code? It works fine if I enter different numbers, but wh

ID: 3646456 • Letter: W

Question

What is wrong with this code? It works fine if I enter different numbers, but when I use the same integrals the index does not work. For example 1 1 1 1 1 1 1 2 1 1, the max is 2 and is in the 8 and the min is 1 and is in the 21230394985, Can you tell me what is wrong ? Thanks!







#include <iostream>
using namespace std;

int main()
{
const int MAXint = 10;
int i, fmax[MAXint], total = 0;
int max, min;
int maxindex, minindex;

for (i = 0; i < MAXint; i++) // Enter the numbers
{
cout << "Enter an integral number: ";
cin >> fmax[i];
}

max = fmax[0];
min = fmax[0];

for (i = 1; i < MAXint; i++)
{
if (max < fmax[i]){
max = fmax[i];
maxindex = i+1;
}
if (min > fmax[i]){
min = fmax[i];
minindex=i+1;
}
}

cout << "The maximum value is " << max << " This is an element number "<<maxindex<< "in the list of numbers"<< endl;
cout << "The minimum value is " << min << " This is an element number "<<minindex<< "in the list of numbers "<< endl;


system("pause>nul");
return 0;
}

Explanation / Answer

max = fmax[0]; replace by max = fmax[10]; now try the code #include using namespace std; int main() { const int MAXint = 10; int i, fmax[MAXint], total = 0; int max, min; int maxindex, minindex; for (i = 0; i fmax[i]; } max = fmax[10]; min = fmax[0]; for (i = 1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote