Showing the Highest and Lowest Integers in an Array

Hello everyone

Some prior information: I am a novice coding in Windows 7 using Visual C++ 2010 Express. I have spent a good long while searching for solutions to problems similar to mine using Google, and have asked my slightly less novice sister for help, to no avail. I apologize in advance if any part of this post annoys anyone. It is not my intent to cause problems for this forum, or any forum.

I have recently discovered some beginner assignments on the MIT open courseware website. An early problem asks the following:

Given a list of N integers, find its mean (as a double), maximum value, minimum value,
and range. Your program will first ask for N, the number of integers in the list, which the
user will input. Then the user will input N more numbers.


As I have recently learned about arrays, I decided that I would issue myself the above challenge, and would complete it using an array. I also decided that at first I would only write code to find the minimum value and maximum value. The code which I have come up with is as follows:

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
#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
	int  N, i;      //N is the number of integers to be entered, i is the first integer
	int z = 0;      //z refers to the array element in question
	int m_array [80];			
		
	cout << "How many integers would you like to enter? ";
	cin >> N;
		
	cout << "Please enter integer 1 "; //Assigns the first integer to both minval and maxval
	cin >> i; 
	int maxval = i;
	int minval = i;
	{
		while (N > 1)		//Assigns the next integers to either maxval or minval if they is higher or lower
		{
			cout << "Please enter integer "<< z+1<<" ";  					
					
			cin >> m_array [z];

			if (m_array [z] > maxval)
			{
				(m_array [z]) = maxval;
			}

			if (m_array [z] < minval)
			{
				m_array [z] = minval;
			}
					
		z++;
		N--;
				
		}
		cout << "Lowest integer:"<< minval << endl<<"Highest integer:" <<maxval;  //Displays minval and Maxval		
	}
	cin.get();
	cin.get();	
}


The issue with this is that instead of outputting the highest and lowest integers, it simply outputs the first entered integer as maxval and minval. The solution to the original problem is very similar, compiles and functions correctly and is as follows:

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
#include "stdafx.h"
#include <iostream >
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
int acc = 0;
// handle the first number separately
cin >> acc;
int minVal = acc;
int maxVal = acc;
// then process the rest of the input
for(int i = 1; i < N; ++i)
{
int a;
cin >> a;
acc += a;
if(a < minVal)
{
minVal = a;
}
if(a > maxVal)
{
maxVal = a;
}
}
cout << "Max: " << maxVal << "\n";
cout << "Min: " << minVal << "\n";
return 0;
}


My hunch is that this is an issue of scope. The brackets at line 18 and 40 were the only solution I could think of, but they did not work.

What part of my own code is causing this issue?
1
2
3
4
5
6
7
8
9
if (m_array [z] > maxval)
			{
				(m_array [z]) = maxval;
			}

			if (m_array [z] < minval)
			{
				m_array [z] = minval;
			}


You are not changing maxval or minval in your loop. Instead you are changing the variable at m_array[z].
Also, the first value read is not enterred in your array. You may wanna look at it.
Moreover, your code will prompt the user for "Please enter integer 1" twice.
Last edited on
I do not see any sense to use an array in the task as you are doing it. Nevertheless try to substitute

1
2
3
4
5
6
7
8
9
			if (m_array [z] > maxval)
			{
				(m_array [z]) = maxval;
			}

			if (m_array [z] < minval)
			{
				m_array [z] = minval;
			}


for


1
2
3
4
5
6
7
8
9
			if (m_array [z] > maxval)
			{
				maxval = m_array [z];
			}

			if (m_array [z] < minval)
			{
				minval = m_array [z];
			}
You are not changing maxval or minval in your loop. Instead you are changing the variable at m_array[z].
Also, the first value read is not enterred in your array. You may wanna look at it.
Moreover, your code will prompt the user for "Please enter integer 1" twice.


Thank you for informing me of what the problem was

@vlad from moscow Thankyou, that fixed it :)
Topic archived. No new replies allowed.