Array problem in C Programming.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KhzQas
    New Member
    • Oct 2006
    • 7

    Array problem in C Programming.

    1) Write a C program that declares an integer array with 5 elements.
    2) Inside a for() loop ask the user for a number and store it in the first element of the array (numerically the array is indexed from zero).
    3) Your loop will iterate 5 times and each time you get a number you will fill up the next spot in the list. After 5 pieces of data have been entered your array will be full.
    4) Write a second for() that looks at what is in the list. Find the largest number in the array. Print that number when you find it.

    -------------------------------------------------------------------------------------------------------------------

    Sample Output

    Enter a number: 1234
    Enter a number: -444
    Enter a number: 900
    Enter a number: 3
    Enter a number: 2
    The largest number in the list is 1234

    Enter a number: -100
    Enter a number: -3
    Enter a number: -4
    Enter a number: -2
    Enter a number: -44
    The largest number in the list is -2

    -------------------------------------------------------------------------------------------------------------------
  • KhzQas
    New Member
    • Oct 2006
    • 7

    #2
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     int list[5];
     int i;
     int number;
     
     for (i = 0; i < 5; i++)
     {
      printf("Enter a number: ");
      scanf("%d", &number);
    
      list[i] = number;
      
     }
    return 0;
    }
    The above is the programming code that I have written to so far. I can not get to the 4th point for some reason, the one where I have to create another for() loop to find the largest value in the array list. Any help would greatly be appreciated.

    Comment

    • nswathi2006
      New Member
      • Jan 2008
      • 2

      #3
      HI ,
      This is the code for to find the largest number in the array .This code is executed.Hope this will help others .

      //code goes here

      //code has been removed
      //MODERATOR
      Last edited by Ganon11; Jan 12 '08, 07:15 PM. Reason: Removing 'spoonfed' code

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        @nswathi: Please don't spoonfeed code.

        @OP: If you haven't already seen the code above, how would you do it if you were the computer? What steps would you take to determine the largest element?

        Comment

        • nswathi2006
          New Member
          • Jan 2008
          • 2

          #5
          Originally posted by Laharl
          @nswathi: Please don't spoonfeed code.

          @OP: If you haven't already seen the code above, how would you do it if you were the computer? What steps would you take to determine the largest element?
          may i know the reason

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Originally posted by nswathi2006
            may i know the reason
            To why we don't spoonfeed code here? So that people learn why the code works, as opposed to blindly feeding someone else's code into their work. Read the Posting Guidelines and the Coursework Posting Guidelines for more information.

            Comment

            Working...