473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need for help again!!

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;
}

Nov 19 '06 #1
3 1062
<br*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com
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.
>

counter=counter+1;
}
cout<<"The largest is "<<largest<<endl;
return 0;
}

--
John Carson
Nov 19 '06 #2
"John Carson" <jc****************@netspace.net.auwrote in message
news:45**********************@un-2park-reader-01.sydney.pipenetworks.com.au
<br*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com
>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.
I meant to say:so initialising largest with it is as bad as not initialising
largest.
--
John Carson

Nov 19 '06 #3

<br*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>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 )
{
}
Nov 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jerry Polyak | last post by:
My code is as follows: <? $connection = @mysql_connect("localhost", "jerry", "") or die(mysql_error()); $dbs = @mysql_list_dbs($connection) or die(mysql_error()); $db_list = "<ul>";
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
8
by: Elliot M. Rodriguez | last post by:
I am having a heckuva time debugging this, or determining why my page is behaving this way. I have a search form, that when completed, returns a datagrid. When the user selects a row (normal...
21
by: Marina | last post by:
I've spent 2 months working on this Access 2003 db, get something done and then the 'boss' wants something different that what was originally stated, or wants to add this or that. It's been nuts,...
1
by: al mwaj3 | last post by:
please I need help in this problem, I must send it tomorrow the problem is (1) Write a Java class called: Binary that has two data fields: an integer array, and an integer size as well as the...
1
by: Benton2862 | last post by:
I need help with and array for a program that acts like a cash register. The user will input an items price and then there payment. It will tell you the change and then tell you what kinda of change...
3
by: Rich Squid | last post by:
Hello Here's my basic problem: On my asp.net form page I have a DetailsView (default mode=edit) bound to a AccessDataSource control. Users can successfuly update a databound template field,...
5
by: runsun | last post by:
Thanks in advance. This program is written in C. It needs to read all characters from a file; then write them into a 3D array (yes, 3D!). The file is a .prn file (one of the Excel types), which...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
2
by: CodeTilYaDrop | last post by:
I need help again. This is probably an easy fix, but I can not figure this out. It is late, and this probably explains why I can't figure it out. This is my error 'positive' undeclared (first...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.