Need for help again!!

B

bryant058

i post an article for help before and people help me(thx).And i try
another way to modify my source code and succedd, but i don't realize
the logic of computer = =!Can't any body explain my new source?
my ex-source code didn't contain line 7(largest=number).As a
result,when i type every number=0,the
largest is 4.
-----<my new source code>----------------
#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;
largest=number;
while(counter<10)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
else
largest=largest;


counter=counter+1;


}
cout<<"The largest is "<<largest<<endl;


return 0;
}
 
J

John Carson

i post an article for help before and people help me(thx).And i try
another way to modify my source code and succedd, but i don't realize
the logic of computer = =!Can't any body explain my new source?
my ex-source code didn't contain line 7(largest=number).As a
result,when i type every number=0,the
largest is 4.
-----<my new source code>----------------
#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;
largest=number;

number is not initialised, so initialising largest with it is as bad as not
initialising number.

What you need to do is initialise number with 0 (assuming that the numbers
you type in are non-negative). Do this:

int largest = 0, number;
while(counter<10)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
else
largest=largest;

The two lines above do nothing useful and should be omitted.
 
J

John Carson

John Carson said:
number is not initialised, so initialising largest with it is as bad
as not initialising number.

I meant to say:so initialising largest with it is as bad as not initialising
largest.
 
J

Jim Langston

i post an article for help before and people help me(thx).And i try
another way to modify my source code and succedd, but i don't realize
the logic of computer = =!Can't any body explain my new source?
my ex-source code didn't contain line 7(largest=number).As a
result,when i type every number=0,the
largest is 4.
-----<my new source code>----------------
#include<iostream>
using namespace std;
int main()
{
int counter=0;
int largest,number;
largest=number;

number is not initialized. I can contain anything. 0, 12, 234987687,
anything. Better to make this line:
int largest = 0, number = 0;
You don't have to initialize number, but if they put in bogus data (x) for
the cin it remains unchanged.
while(counter<10)
{
cout<<"Enter a Number:";
cin>>number;
if (number>largest)
largest=number;
else
largest=largest;

This is doing nothing. No reason to have it. A smart compiler will
optimize this away too.
counter=counter+1;

++counter; does the same thing and is easier to follow (once you're used to
it).
You could also have written this as:
counter += 1;
}
cout<<"The largest is "<<largest<<endl;

return 0;
}

That being said, if you are doing this 10 times, why not just make it a for
statement?

for ( counter = 1; counter <= 10; ++counter )
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top