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";
}
}
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.
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
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();
}
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.
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 usingnamespace 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