Hello everyone. I have this pretty simple program. I have to write a C++ program that will let the user enter 10 values into an array. The program should then display the largest and the smallest values stored in the array.
#include <iostream>
usingnamespace std;
int a (int[]);
int main ()
{
constint Numb = 12;
int i;
int a[Numb];
int max = a[0];
int min = a[0];
for (i = 0; i < 12; i++)
{
if (a[i] > max)
{
max = a[i];
}
elseif (a[i] < min)
{
min = a[i];
}
}
cout << max << endl;
cout << min << endl;
return 0;
}
I get an error talking about an 'a' is an uninitialized variable. I know it's probably a silly error, but I cannot get this thing to go away. Help?
#include <iostream>
usingnamespace std;
int main ()
{
constint Numb = 10;
int i;
int a[Numb];
int max = a[0];
int min = a[0];
cout << "Enter 10 values" << endl;
cin >> a[Numb];
for (i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
elseif (a[i] < min)
{
min = a[i];
}
}
cout << max << endl;
cout << min << endl;
return 0;
}
I changed that 12 to a 10 now since it was originally asking for 10; however, I'm still getting that same error. I'm probably doing something wrong with the cout / cin section.
Remember that after you create an array, the [brakets] are used to index one of its elements.
IE:
1 2
int a[3]; // creates an array with 'Numb' elements
a[3] = 5; // assigns 5 to index 3 (the 4th element) in the array
This is effectively what you're doing with your cin line. You're not filling the entire array, you're only filling index 10 (which, I might add, is out of bounds of the array because the highest legal index is 9!)
In order to read 10 numbers you'll have to cin 10 times, once for each number.