Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2005, 11:31 PM   #1
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
One more question infinite!

Accept 10 numbers, and find the largest and second largest numbers. You MUST use a while loop

Here's my code thus far:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int number = 0, MAX = 0, MAX1 = 0 count = 1;

while (count <= 10)
{
cout << "Enter 10 numbers to find the maximum: ";
cin >> number;


if (number > MAX)
{
MAX = number;
}

if (number > MAX1)
{
MAX1 = number;
}

count++;
}
cout << MAX << " is the maximum" << " and " << MAX1 << " is the second maximum!" << endl;
return 0;
}


and I have no idea where to go from there,



There's nothing wrong with th at code, except it prints the max for both MAX and MAX1. I don't know how to get the second largest number...
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 11:34 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Change your second if-statement:

if(number > max1 && number < max)
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 10th, 2005, 11:35 PM   #3
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
// Exercise 4.19 Two Largest Numbers.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int number = 0, MAX = 0, MAX1 = 0, count = 1;

while (count <= 10)
{
cout << "Enter 10 numbers to find the maximum: ";
cin >> number;


if (number > MAX)
{
MAX = number;
}

else if (number > MAX && number > MAX1)
{
MAX1 = MAX;
MAX = number;
}

count++;
}
cout << MAX << " is the maximum" << " and " << MAX1 << " is the second maximum!" << endl;
return 0;
}



NEvermind, I figured it out... guess I'm just tired... too bad it took me so long, so thanks anyways
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 11:37 PM   #4
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
damn, nevermind, it's not working now, i keep geting 0 for the second maximum

god dang, stuff like this is hard, but directx buffering and blitting is easy... what a world
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 11:40 PM   #5
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
dang, it still gives me 0 for the second value:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int number = 0, MAX = 0, MAX1 = 0, count = 1;

while (count <= 10)
{
cout << "Enter 10 numbers to find the maximum: ";
cin >> number;


if (number > MAX)
{
MAX = number;
}

if (number > MAX1 && number < MAX)
{
MAX1 = number;
}

count++;
}
cout << MAX << " is the maximum" << " and " << MAX1 << " is the second maximum!" << endl;
return 0;
}
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 11:44 PM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
In your first if-statement, have

max1=max;

beofre max=number;
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 10th, 2005, 11:49 PM   #7
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
oh duh, ok I see it now.

Thanks MJordan2nd
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 11:50 PM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
beat me to it.... gw
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 11th, 2005, 12:11 AM   #9
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road


No problem!
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC