473,386 Members | 2,042 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,386 software developers and data experts.

Beginning programmer... I need some help.

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.

Feb 5 '06 #1
8 1442
"bmlclemson08" writes:
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.
Feb 5 '06 #2
bmlclemson08 wrote:
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
--
You know it's going to be a bad day when you want to put on the clothes
you wore home from the party and there aren't any.

Feb 5 '06 #3
bmlclemson08 wrote:
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 */
}

--
- Mark
Feb 5 '06 #4
osmium wrote:
<snip>
if(current_in == 99)
break; <snip> 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

--
Census Taker to Housewife: Did you ever have the measles, and, if so,
how many?

Feb 5 '06 #5
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.

Feb 5 '06 #6
bmlclemson08 wrote:
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().

--
Very few profundities can be expressed in less than 80 characters.

Feb 5 '06 #7
bmlclemson08 wrote:

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/>
Feb 5 '06 #8
bmlclemson08 wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 5 '06 #9

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

Similar topics

3
by: Christian McArdle | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.os.ms-windows.programmer.64bit This is a formal Request For Discussion (RFD) to create comp.os.ms-windows.programmer.64bit as an unmoderated...
5
by: anas.hashmi | last post by:
I am trying to write to the beginning of a file. The reason: I want to make a form where board webmasters can use it to insert in updates to a webpage without having to go directly into the web...
23
by: Steve Jorgensen | last post by:
Hi all, I'm working on a project through a consulting company, and I'm writing some database code for use in another programmer's project in Excel/VBA. The other programmer is working through...
11
by: Wilsoch | last post by:
Long story short: My Access developer is letting me down. He doesn't really know VB and he can't figure out how to do what I need. Situation: Access database that will be used locally on...
3
by: Rajorshi Biswas | last post by:
Hi all, I'm aware that this might not be a "C-specific" question, if so, please let me know which group is the most appropriate for this kind of question. This is a question on unix...
11
by: Lint Radley | last post by:
Hi Everyone, I've done several C# console applications now, and feel comfortable with the language. Can anyone recommend a web site, book, etc which has information on starting to develop full...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
10
by: darQueskye | last post by:
I'm brand new to the programming scene and I just got into Perl by a friend's recommendation. (I just wrote my first "Hello, world." code last night. :-D) I've always been fascinated by the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.