Code is not giving correct output?

Hi,
I am trying to get a highest number from a list of 70 numbers.I have written the following code which is not giving a correct output. Can anyone pls guide me?
[
#include <cstdlib>
#include <iostream>


using namespace std;


const int ARRAY_SIZE=70;


int main()
{
int Numbers[ARRAY_SIZE];
int Highest;
Highest=Numbers[0];
int Counter;
for(Counter=0;Counter<71;Counter++)
{
cout<<"Enter 70 Numbers:";
cin>>Numbers[ARRAY_SIZE];
cin.ignore();
if(Numbers[Counter]>Highest)
{
Highest=Numbers[Counter];
cout<<"The highest number is: "<<Highest<<"\n";
}
}

cin.get();
}
]
cin>>Numbers[ARRAY_SIZE]; use Counter instead of ARRAY_SIZE

You should initialize Highest before comparing something to it
not working?!?
Which errors?
http://www.cplusplus.com/reference/algorithm/max_element.html

Here is one way to do it with algorithms. There is no cin.get at the end so run it in the debugger or from a cmd prompt. the cin.ignore() works as long as you only press enter after the number. I can't remember the longer version that uses numeric limits but I just wanted to point out that potential problem with the program.

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
#include <iostream>
#include <algorithm>

void max_element_example()
{
    const int ARRAY_SIZE(10);
    int Numbers[ARRAY_SIZE];
    int* pHighest(0);
    std::cout << "Enter 10 Numbers:" << std::endl;
    for(int Counter(0); Counter < ARRAY_SIZE; ++Counter)
    {
	std::cout << "Enter next number: " << std::endl;
	std::cin >> Numbers[Counter];
	std::cin.ignore();
    }

    // now determine highest number
    pHighest = std::max_element(Numbers, Numbers + ARRAY_SIZE);
    std::cout << "The highest number = " << *pHighest << std::endl;
}

int main()
{
    max_element_example();
    return 0;
}
Last edited on
There are no compilor errors...when i run the program...it gives me the following output:

Enter 70 Numbers: 1 2 4 56 67....n. When i press enter after putting in 70 numbers, it just repeats the string " Enter 70 Numbers:" 70 times:-(.
You should change the loop a bit:
1
2
3
4
5
6
7
8
cout<<"Enter 70 Numbers:";
for(Counter=0; Counter<ARRAY_SIZE; Counter++)
{
     cin>>Numbers[ARRAY_SIZE Counter];
     if(Numbers[Counter]>Highest)
          Highest = Numbers[Counter];
}
cout << Highest;


By the way, do you really need an array?
Last edited on
no i don't really need an array...the output is still not correct...i have tried putting in your suggestion...it gives the following output:

Enter 70 Numbers: 23 34 45 67...so on...now when i press enter it says...

Enter 70 numbers:The Highest Numbers is: 2368344..and repeats this 70 times...
I kept the error I've already find but correcting that the program should work fine, are you using that in another loop?
here is the amended program ive writtten:
[
#include <cstdlib>
#include <iostream>


using namespace std;

const int ARRAY_SIZE=70;


int main()
{
int Numbers[ARRAY_SIZE];
int Highest;
Highest=Numbers[0];
int Counter;
for(Counter=0;Counter<ARRAY_SIZE;Counter++)
{
cout<<"Enter 70 Numbers:";
cin>>Numbers[ARRAY_SIZE];
if(Numbers[Counter]>Highest)
{
Highest=Numbers[Counter];
cout<<"The highest number is: "<<Highest<<"\n";
}
else
{
cout<<"The program is not executed correctly."<<"\n";
}
}
cin.get();
}
]

This program gives me the output as follows:

Enter 70 Numbers: 21 23 34 56...
The program is not executed correctly.
Enter 70 Numbers:The Highest number is: 4064440

no i am not using it in another loop...
Last edited on
Thanks Bazzy...following is the new solution program i have written and this worked...
[
#include <cstdlib>
#include <iostream>


using namespace std;

int main()
{
int Counter;
Counter=0;
int Number;
int Highest=0;
cout<<"Enter 70 numbers:";
while(Counter<70)
{
cin>>Number;
cin.ignore();
if(Highest<Number)
{
Highest=Number;

}
Counter++;

}
cout<<"The highest number is: "<<Highest<<"\n";
cin.get();
}

]
Last edited on
First of all you are setting highest to the first element of an uninitialized array. Who knows what that initial value will be. If you are lucky it will be 0. It could be 12053122 or some other random number that happened to be at that memory location.

Since you are obviously not listening to anyone this will be my last post on this thread. I already wrote you the program that will work fine with arrays. Bazzy has already shown you why the program is messed up. You are using ARRAY_SIZE as an index into the array which is attempting to store each of the 70 numbers into the same invalid location of the array. Just compare what you have done to the program I wrote and you'll see the difference. Just change the constant from 10 to whatever you wish. Initially get the program working with a small number and then simply increase the number later. It is pretty tedious to enter 70 numbers just to find out that the program doesn't work.
thanks for your suggestion and useful info kempofighter...
kempofighter's solution reminded me of something. I have seen some programmers use:

using namespace std;
cout << "Hello World!" << endl;

and others not use the namespace qualifer:

std::cout << "Hello World!" << std::endl;

Is this just a personal preference in style of programming, or are there conditions where "using namespace std;" would hide some non standard library functions that were needed (couldn't you just say nonstd::cout in that case?).
if you have std::cout and nonstd::cout by using namespace std you will have cout to be std::cout and nonstd::cout should have the nonstd::.
But if you are using both namespaces you would have ambiguities when calling cout as the compiler wouldn't know which you mean
Last edited on
Topic archived. No new replies allowed.