Lowest value not printing

Hello, I'm working on this assignment and the instructions are to ask the user to enter 10 numbers and store it in an array. Then we're supposed to print the numbers entered followed by the lowest and highest integer they entered. My problem is printing the lowest number but it prints the highest. What am I missing?
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>

using namespace std;

const int gSIZE = 10;

void userInput(int myArray[gSIZE]);
void findLowestValue(int myArray[gSIZE]);
void findLargestValue(int myArray[gSIZE]);

int main()
{
    int myArray[gSIZE];
    
    userInput(myArray);
    findLowestValue(myArray);
    findLargestValue(myArray);
    
    return 0;
    
} 

void userInput(int myArray[gSIZE])
{
	int index = 0;

	cout << "Enter 10 numbers:" << endl;
	
	for(int row = 0; row < gSIZE; ++row)
	{
		cout << ++index << ". Enter a number: ";
		cin >> myArray[row];

	} 

    cout << endl;
    
} 

void findLowestValue(int myArray[gSIZE])
{
    cout << "Of the values you entered: ";
    
	for(int row = 1;row < gSIZE; ++row)
    {
       cout << myArray[row] << ", ";
      
       if( myArray[10] > myArray[row])
           myArray[10] = myArray[row];
    }
    
    cout << endl;
    
    cout << "The lowest value you entered was: " << myArray[10] << endl;

}

void findLargestValue(int myArray[gSIZE])
{

     for(int row = 1;row < gSIZE; ++row)
    {
       
       if( myArray[10] < myArray[row])
           myArray[10] = myArray[row];
    }
    
    cout << "The largest value you entered was: " << myArray[10] << endl;
    
}
Last edited on
myArray[10] is not a valid index when using the array.
You need to keep a variable for "lowest value so far" and compare/assign to that.
Hello av16352,

I do not know if you can use this or not. From the "<algorithm>" header file you have "std::sort" where after all the numbers are input you could sort the array then element (0)zero would contain the lowest number and element 9 would contain the highest.

If you can not use "std::sort" you could always write your own sort routine.

If you are not familiar with this yer C++ like other languages are (0)zero based, so you array elements start at (0)zero and got to 9 for 10 elements in the array.

Andy
The easiest way to find the lowest and the highest number is as the numbers are being entered:

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>
#include <limits>

const size_t gSIZE {10};

int main()
{
	int myArray[gSIZE] {};
	int mymin {std::numeric_limits<int>::max()}, mymax {std::numeric_limits<int>::min()};

	std::cout << "Enter " << gSIZE << " numbers: ";

	for (size_t row = 0; row < gSIZE; ++row) {
		std::cin >> myArray[row];

		if (myArray[row] > mymax)
			mymax = myArray[row];

		if (myArray[row] < mymin)
			mymin = myArray[row];
	}

	for (size_t row = 0; row < gSIZE; ++row)
		std::cout << myArray[row] << ' ';

	std::cout << "\nLowest is " << mymin;
	std::cout << "\nHighest is " << mymax << '\n';

}

Topic archived. No new replies allowed.