min & max of an array

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.

This is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int a (int[]);
int main ()
{
const int 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];
        }
      else if (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?

Thanks!
What is line 3 supposed to be?

Also, you never actually get the 10 numbers from the user and put them in 'a'. That's what your error is talking about.
I was trying to make a prototype for the array since my professor showed us how to do that. I am assuming I don't need that?
You only need to make prototypes of functions.

For variables and arrays, you just delcare them. What you have on line 8 is fine, you don't need line 3.
Ahhh, okay. So now, I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
int main ()
{
const int 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];
        }
      else if (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.

You should be able to fit that in a loop.
Last edited on
yes! It works!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main ()
{
int mn,mx;
const int Numb = 10;
int a[Numb]; //10 elements
cout<<"Enter 10 values:"; //prompts user for 10 values.
for(int i=0;i<10;i++)
{
cout<< "\nEnter value: ";
cin>> a[i]; // puts values in array
}

mn=a[0];
mx=a[0];
for(int i=1;i<10;i++)
	{
		if(mn>a[i])
		{
			mn=a[i];
		}
		else if(mx<a[i])
		{
			mx = a[i];
		}
	}

cout<<"Maximum number is: "<< mx << endl;
cout<<"Minimum number is: "<< mn << endl;

return 0;

}


I changed some of the code so it looked a little better, but thank you so much!!
Topic archived. No new replies allowed.