Beginning programmer... I need some help.

B

bmlclemson08

Hey if anyone could I need to find out how to write a program that will
read in any number of integers, including none, and determine which is
the largest integer. The part i can't figure out is getting the
program to read any number of integers. Any ideas at all would be
wonderful. here's the code for what i have so far.

#include <stdio.h>

int main ()
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

for (counter = 2; ; counter=counter+1)
{
scanf ("%d", &current_in);

if (largest_so_far<current_in)
largest_so_far = current_in;
else
largest_so_far = largest_so_far;
}
printf ("The largest number is %i.\n", largest_so_far);

return 0;

}


All variables and suuch were determined in my class. I'm guessing my
problem is in the for statement, but I can't figure it out. Thanks
again for any help.
 
O

osmium

bmlclemson08 said:
Hey if anyone could I need to find out how to write a program that will
read in any number of integers, including none, and determine which is
the largest integer. The part i can't figure out is getting the
program to read any number of integers. Any ideas at all would be
wonderful. here's the code for what i have so far.

#include <stdio.h>

int main ()
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

for (counter = 2; ; counter=counter+1)
{
scanf ("%d", &current_in);

if(current_in == 99)
break;
if (largest_so_far<current_in)
largest_so_far = current_in;
else
largest_so_far = largest_so_far;
}
printf ("The largest number is %i.\n", largest_so_far);

return 0;

}


All variables and suuch were determined in my class. I'm guessing my
problem is in the for statement, but I can't figure it out. Thanks
again for any help.

You don't provide any mechanism to determine when the user is done. A
simple way, often used in student programs, is to enter a special value to
indicate "doneness" as in the insert suggested above. Most programmers
frown on the use of scanf but that discussion can be deferred.
 
V

Vladimir S. Oka

bmlclemson08 said:
Hey if anyone could I need to find out how to write a program that
will read in any number of integers, including none, and determine
which is
the largest integer. The part i can't figure out is getting the
program to read any number of integers. Any ideas at all would be
wonderful. here's the code for what i have so far.

#include <stdio.h>

int main ()

int main(void)

is better, as it spells it out for all to see...
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

for (counter = 2; ; counter=counter+1)

This is a never-ending loop. I suspect that's not what you want.
{
scanf ("%d", &current_in);

Using fgets()/sscanf() combination is safer...
if (largest_so_far<current_in)
largest_so_far = current_in;
else
largest_so_far = largest_so_far;

The `else` part is superfluous.
}
printf ("The largest number is %i.\n", largest_so_far);

return 0;

}


All variables and suuch were determined in my class. I'm guessing my
problem is in the for statement, but I can't figure it out. Thanks
again for any help.

Hint: ask user for a number of integers they want to enter; store that
in your `counter`; while `counter` is > 0 read in number, determine if
it's largest so far, decrement `counter`, rinse, repeat; output
`largest_so_far`.

Cheers

Vladimir
 
V

Vladimir S. Oka

osmium wrote:
if(current_in == 99)
break;
You don't provide any mechanism to determine when the user is done. A
simple way, often used in student programs, is to enter a special
value to indicate "doneness" as in the insert suggested above. Most
programmers frown on the use of scanf but that discussion can be
deferred.

This programmer here shudders at the thought, especially as a learning
tool.

To OP: please, please, please don't do such a thing. Your original
problem is a perfect example of why it is a bad idea. There is /no/
special value of an integer!

Cheers

Vladimir
 
M

Mark Odell

bmlclemson08 said:
Hey if anyone could I need to find out how to write a program that will
read in any number of integers, including none, and determine which is
the largest integer. The part i can't figure out is getting the
program to read any number of integers. Any ideas at all would be
wonderful. here's the code for what i have so far.

#include <stdio.h>

int main ()

more typical:

int main(void)
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

for (counter = 2; ; counter=counter+1)

more typical:

for (counter = 0; SOME_EXIT_CRITERIA; ++counter)

where ++counter is shorthand for counter = counter + 1. Note you did not
provide the for loop with an exit criteria. This could be some counter
max., some current_in flag value, or what have you. Frankly, I don't
know why you even use counter since it doesn't do much except increment.
You never print it or make a decision based on it.
{
scanf ("%d", &current_in);

if (largest_so_far<current_in)
largest_so_far = current_in;
else
largest_so_far = largest_so_far;
}
printf ("The largest number is %i.\n", largest_so_far);

printf ("The largest number is %d.\n", largest_so_far);

stick with %d, people don't use %i much anymore.
return 0;

Include stdlib.h and return EXIT_SUCCESS for real pedantic-ness.
}


All variables and suuch were determined in my class. I'm guessing my
problem is in the for statement, but I can't figure it out. Thanks
again for any help.

Note: If you need a "forever" loop, you can exclude 'counter' all
together like this:

for (;;)
{
if (some_exit_criteria_met)
{
break; /* exit nearest encompassing iteration loop */
}

/* normal loop processing */
}
 
C

CBFalconer

bmlclemson08 said:
Hey if anyone could I need to find out how to write a program that
will read in any number of integers, including none, and determine
which is the largest integer. The part i can't figure out is
getting the program to read any number of integers. Any ideas at
all would be wonderful. here's the code for what i have so far.

.... snip try code ...

This seems to work:

#include <stdio.h>

/* ---------------------- */

static int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
} /* flushln */

/* ---------------------- */

/* absorb integer if available, flush remainder of line */
/* return 1 for success, else 0 */
int getint(int *i, FILE *f) {
int rslt;

rslt = fscanf(f, "%d", i);
flushln(f);
if (1 == rslt) return 1;
else return 0;
} /* getint */

/* ---------------------- */

int main(void) {
int maxsofar, newvalue;

printf("Signal EOF to stop\n"
"Enter first value:");
fflush(stdout);
if (getint(&newvalue, stdin)) {
maxsofar = newvalue;
do {
if (newvalue > maxsofar) maxsofar = newvalue;
printf("Max so far = %d, enter next: ", maxsofar);
fflush(stdout);
} while (getint(&newvalue, stdin));
}
return 0;
} /* main */

[1] c:\c\junk>cc junk.c

[1] c:\c\junk>.\a
Signal EOF to stop
Enter first value:23
Max so far = 23, enter next: 33
Max so far = 33, enter next: 21
Max so far = 33, enter next: 2
Max so far = 33, enter next: 4
Max so far = 33, enter next: 18
Max so far = 33, enter next: 65
Max so far = 65, enter next: ^Z

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
B

bmlclemson08

OK the new code i have so far is:
#include <stdio.h>

int main ()
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

for (counter = 0; ; counter = counter + 1)
{
scanf ("%d", &current_in);

if (largest_so_far < current_in)
largest_so_far = current_in;
if ((current_in = getchar()) != EOF)
{
break;
}
}
printf ("The largest number is %d.\n", largest_so_far);

return 0;

}

But it's stopping on the second integer entered both manually and by
..dat files.

I have to turn in .dat files with the program to show it works with
different numbers of integers.
 
V

Vladimir S. Oka

bmlclemson08 said:
OK the new code i have so far is:
#include <stdio.h>

int main ()
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

for (counter = 0; ; counter = counter + 1)
{
scanf ("%d", &current_in);

if (largest_so_far < current_in)
largest_so_far = current_in;
if ((current_in = getchar()) != EOF)
{
break;
}

Here, you break out of `for` loop only if getchar() does /not/ return
EOF, probably not what you wanted. Did you mean to type `==`?

Anyway, read my other post, and try to implement my suggestion. I don't
think you're on the right path here (e.g. your current solution does
not cater for /no/ integers at all).

Cheers

Vladimir

PS
Also keep in mind that your getchar() call "eats" the non-EOF characters
(to pedants: I know EOF is a condition, not a character). You may be
better of using getc() and if it does not return EOF do an ungetc().
 
M

Michael Mair

bmlclemson08 said:
OK the new code i have so far is:
#include <stdio.h>

int main ()

You forgot the void parameter list...
int main (void)
{
int current_in, largest_so_far, counter;

printf ("Enter your numbers:\n");

scanf ("%d", &largest_so_far);

Rather use fgets()/sscanf() or ggets()/sscanf(). (Note:
ggets() is not standard C but you can get a public domain
standard C version; Chuck Falconer advertises his regularly
round here).

More important:
Always check the return value of scanf() family functions
-- it tells you wether the whole thing was successful.
for (counter = 0; ; counter = counter + 1)

You have been warned about leaving out the loop continuation
condition.
{
scanf ("%d", &current_in);

if (largest_so_far < current_in)
largest_so_far = current_in;
if ((current_in = getchar()) != EOF)
{
break;
}

What are you trying to achieve?
This should break rather early.

Emptying an input line after scanf() looks like
while ((current_in = getchar()) != EOF)
if (current_in == '\n')
break;
This may help you
}
printf ("The largest number is %d.\n", largest_so_far);

return 0;

}

But it's stopping on the second integer entered both manually and by
.dat files.

See above.
I have to turn in .dat files with the program to show it works with
different numbers of integers.

It does not.

printf() the return value of the scanf() calls and try to
figure it out. Essentially: Either go for fgets()+sscanf()
or prepare everything so scanf() cannot mess up (use the above
code snippet).

Cheers
Michael
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top