C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-30-2009, 10:16 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 6
max and min of a list with sentenial at 0

Im having trouble with the smoothness of running this program. If you run it you will see the program right away. The program completes the task but uses a GetInteger function unnecessarily in the third line. Any help would be appreciated.

Here is the program
-------------------------------------
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

int main()
{
int n, min, max;

printf("Please enter a list of integers.\n");
printf("Signal the end of your list with 0.\n");
printf("Enter first number: ");
n=GetInteger();
{
min=n;
max=n;
while ((n=GetInteger())!=0)
{
printf("Enter next number: ");
if (n<=min)
min=n;
if (n>=max)
max=n;
}
printf("The maximum number in the list is %d.\n", max);
printf("The minimum number in the list is %d.\n", min);
}
}
Attached Files
File Type: cpp MinMax2.cpp (531 Bytes, 10 views)
kbikkasani is offline   Reply With Quote
Old 07-30-2009, 10:25 AM   #2
Super Moderator
 
Join Date: Sep 2001
Posts: 4,680
I don't see any problem (although I don't know why you have a curly brace after your first call to GetInteger). Can you be more specific?


Also, we don't have simpio.h and genlib.h, so we can't run your program and help you debug those portions.

You posted this in the C forum, but your files are .cpp (C++) and they look like C. Are you doing C or C++?

And always use code tags. Surround your code in [ code ] and [ /code ] (without the spaces) and it will format your code nicely (different font, preserve spacing, etc).
sean is offline   Reply With Quote
Old 07-30-2009, 10:45 AM   #3
Registered User
 
Join Date: Jul 2009
Posts: 6
Thanks for the tips. The problem is that GetInteger occurs when running the function unnecessarily at one point right after running the program. I believe it is a problem with the while statement but when i do

while (n!=0)

It fixes the first error but I get the minimum = 0 although that is just supposed to be the sentinel,

i will try to post in the c+ forums for help. Thanks again for the advice
kbikkasani is offline   Reply With Quote
Old 07-30-2009, 11:10 AM   #4
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
Sounds very similar to this thread:

Finding smallest and second smallest integer and largest and second largest integer
slingerland3g is offline   Reply With Quote
Old 07-30-2009, 02:09 PM   #5
Webhead
 
Spidey's Avatar
 
Join Date: Jul 2009
Posts: 278
Quote:
Originally Posted by kbikkasani View Post
Thanks for the tips. The problem is that GetInteger occurs when running the function unnecessarily at one point right after running the program. I believe it is a problem with the while statement but when i do

while (n!=0)

It fixes the first error but I get the minimum = 0 although that is just supposed to be the sentinel,

i will try to post in the c+ forums for help. Thanks again for the advice
The problem is that you are calling GetInteger twice the first time.
Quote:
printf("Enter first number: ");
n=GetInteger();
{
min=n;
max=n;
while ((n=GetInteger())!=0)
{
You can fix this by removing the first getInteger altogether, as the while loop will not enter if you enter 0. But, you must first initialize n to 0 as your setting min and max to 0. Also, the curly brace after the first getInteger is not required.

Code:
int n= 0;
printf("Enter first number: ");
min=n;
max=n;
while ((n=GetInteger())!=0)
{
__________________
Spidey out!
Spidey is offline   Reply With Quote
Old 07-31-2009, 07:08 AM   #6
Registered User
 
Join Date: Jul 2009
Posts: 6
Thanks so much. Setting n=0 works perfectly. Thank you so much.
kbikkasani is offline   Reply With Quote
Reply

Tags
error, loop, max, min

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help finding the max and min of a 2d array finalreign C Programming 6 04-29-2009 11:39 AM
Max Min Problem smithc2005 C Programming 7 10-22-2008 10:38 AM
Ranged numbers Desolation Game Programming 8 07-25-2006 10:02 PM
help finding max and min values from list ericp023 C Programming 7 02-22-2003 06:51 PM
Double output! Spurnout C++ Programming 3 11-02-2002 03:35 AM


All times are GMT -6. The time now is 03:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22