loop problem

This is a discussion on loop problem within the C Programming forums, part of the General Programming Boards category; hello every one I'm trying to solve this question but i solve it wrong Write a program to prompt 5 ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    11

    loop problem

    hello every one
    I'm trying to solve this question but i solve it wrong

    Write a program to prompt 5 integers from the operator. Find the largest of those 5 integers and display it to the screen.

    Sample input-output
    Number 1: 8
    Number 2: 12
    Number 3: 5
    Number 4: 22
    Number 5: 17
    The largest is: 22

    this is my solution which is totally wrong


    Code:
    #include <stdio.h>
    
    int main(void) {
    	int in, high, i;
    
    	for (i=0;i<5;i++) {
    		printf("Enter an integer: ");
    		scanf("%d",&in);
    		if (!i) high = in;	// the first loop
    		else if (in > high) high = in;
    	}
    
    	printf("High: %d\n",high);
    
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    1) Initialize high to 0 before your loop.
    2) In your IF, compare high to in. if lower, set high to in.

    that's it.

    For extra credit, check the return value of scanf() to see if you actually got data from the user.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    11
    sorry brother but i didn't understand what u said
    could u write it using code?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Did you write the code posted yourself?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    a better way is to store all numbers in an array and then compare.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by eagle0eyes View Post
    sorry brother but i didn't understand what u said
    could u write it using code?
    A better question would be could you write it using code? Nobody here will do your homework for you.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    That code works.. at least for me. What is the problem you're getting?

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    457
    Did you write the code posted yourself?
    I seem to recall someone giving someone else that exact code about 2 days ago. I'll see if I can find it.

    problem with if statement


    Yup, see MK27's last post in the subject
    Last edited by KBriggs; 05-29-2010 at 03:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 07:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 02:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 07:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 05:37 PM

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