Load values into an array

How would I load these 10 values into an array that can later be searched? I think I'm missing something simple.


19 for (int x = 0; x < 10; x++) //Asks user to input 10 integers
20 {
21 cout << "Enter Value:";
22 cin >> value;
23 }
24

Thanks for any help.
closed account (3hM2Nwbp)
To give an answer we'll need to see lines 0-18, as well as 25 through the end of your program, properly wrapped in code tags.
Last edited on
Please put your code in between [code][/code] tags, and do not put in line numbers manually like that.
1
2
3
4
5
for(unsigned long i = 0; i < 10; ++i)
{
  cout << "Enter value: ";
  cin >> a[i];
}
Notice: the thread OP moved his post to be below mine, for whatever reason.
Last edited on
10 #include <iostream>
11 using namespace std;
12
13 int main()
14 {
15 int i, value, minimum, maximum;
16 int a[10];
17
18 for (int x = 0; x < 10; x++) //Asks user to input 10 integers
19 {
20 cout << "Enter Value:";
21 cin >> value;
22 }
23
24
25 minimum = a[0];
26
27 for (i = 0; i < 10; i++)
28 if (a[i] < minimum) minimum = a[i];
29
30 cout << "min = " << minimum << endl;
31
32
33 maximum = a[0];
34
35 for (i = 0; i < 10; i++)
36 if (a[i] > maximum) maximum = a[i];
37
38 cout << "max = " << maximum << endl;
39
40
41 cout << "PROGRAM ENDS" << endl;
42
43 return 0;
44 }
First, you never actually did input to the array pointed to by 'a'. You actually don't need the variable 'value', since in that first for loop, you could take the input directly into 'a[i]'. The rest of the code looks good, but I assume you know that when you output the minimum and maximum, it will actually (unless the first number is the largest or smallest) output every candidate for the minimum and maximum that it comes across. If you want it just to output one number for each, move the output statement out of the for loop.
Last edited on
closed account (zb0S216C)
Looking at your code, I get the impression that you wanted to do this:
1
2
3
4
5
6
for (int x = 0; x < 10; x++) //Asks user to input 10 integers
{
    cout << "Enter Value:";
    cin >> value;
    a[ x ] = value;
}

It would be better to do this:
1
2
3
4
5
for (int x = 0; x < 10; x++) //Asks user to input 10 integers
{
    cout << "Enter Value:";
    cin >> a[ x ];
}
Last edited on
That worked. Thanks. The last bit of the program that I can't figure out is the following:
I need to display how many times the max and min were entered.
Example: 1 was entered four times
minimum count = 4

I'm not necessarily asking for the code, but a nudge in the right direction would be appreciated. I know how to add sums of integers and even all elements in an array, but when I tried code that worked for those, it doesn't work for this.
hint: Maybe you could use a counter that increments every time a number is input?
Topic archived. No new replies allowed.