Thread: HELP with Array Question

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    16

    Smile HELP with Array Question

    Code:
    //Program that reads 10 students marks and display the average, the maximum and the minimum marks.
    
    #include <Stdio.h>
    
    main()
    {
    
    // Local Declarations
      int marks[10];
      int i;                        /*loop counter */
      int avg;
      int sum = 0;
      int max = marks[0];
      int min = marks[0];
    
    // Filling the Array
      printf("Enter the marks of the ten students: \n");
      printf("==\n\n");
      printf("Student No. \t\t Marks Obtained\n");
      for (i = 0; i <= 9; i++) {
        printf("\t%d \t\t\t", i + 1);
        scanf("%d", &marks[i]);
      }
    
    // Calculate the Average
      for (i = 0; i <= 9; i++) {
        sum += marks[i];
        avg = (sum / 9.0);
      }
    
    // Find maximum and minimum element in an array
      for (i = 0; i <= 9; i++) {
        if (max < marks[i]) {
          max = marks[i];
        }
    
        else if (min > marks[i]) {
          min = marks[i];
        }
      }
    
      printf("\n==\n\n");
      printf("Average Marks: %d\n", avg);
      printf("Maximum Marks: %d\n", max);
      printf("Minimum Marks: %d\n", min);
    
      getch();
    }
    In this program i am able to get Average Marks and Minimum Marks But i am not able to get the Maximum Marks Output correct
    Last edited by Salem; 09-29-2013 at 07:46 AM. Reason: Removed train wreck font abuse

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can barely read your program!

    First thing: assign max to the very first value in the array, marks[0]. Do this either just before the for loop, or inside the top line of the for loop:
    Code:
    for(i=0,max=marks[0];i<9;i++) {
       if(max > marks[i])
           max = marks[i];
    }
    You have < instead of > in your code. You can assign max to an impossibly low value to start with, but using marks[0] is always a winner. Reverse this for min, but keep the initial assignment to marks[0].

    [/code]

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Adak View Post
    Code:
    for(i=0,max=marks[0];i<9;i++) {
       if(max > marks[i])
           max = marks[i];
    }
    You have < instead of > in your code.
    Well, Adak with > in the code you will find min value not max value as code with <
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, < is correct, of course.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Quote Originally Posted by Adak View Post
    I can barely read your program!

    First thing: assign max to the very first value in the array, marks[0]. Do this either just before the for loop, or inside the top line of the for loop:
    Code:
    for(i=0,max=marks[0];i<9;i++) {
       if(max > marks[i])
           max = marks[i];
    }
    You have < instead of > in your code. You can assign max to an impossibly low value to start with, but using marks[0] is always a winner. Reverse this for min, but keep the initial assignment to marks[0].

    [/code]
    thanks that works!
    how do i print the error message "Character is Invalid" if a user enter something other then numbers?
    Which method i should use?

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    i want to write a function for this program that will count no of students who are above and below average!
    what will be the formula to get it?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Checking for invalid input in your program, can be done different ways. One way is to use the return from scanf() on line 22. It will return the number of items it has read and stored, or EOF if it fails.

    Study this and run it to see an example.
    Code:
    #include <stdio.h>
    
    int main(void) {
       int ok=0,mynumber=0;
    
    
       while(mynumber != -99) {
          printf("Enter an score or -99 to quit:  ");
          fflush(stdout);
    
          ok = scanf("%d",&mynumber);
    
          if(ok<1) {                        //scanf() failed
             printf("\ninvalid input\n");
             while((ok=getchar()) != '\n'); //clear out the input buffer
          }else {
             printf("My number is: %d\n",mynumber);
          }
       }
    
       printf("\n");
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array question
    By rassul in forum C Programming
    Replies: 11
    Last Post: 01-14-2011, 02:02 AM
  2. Replies: 12
    Last Post: 11-21-2010, 09:46 AM
  3. 1-D array question
    By everyone0 in forum C Programming
    Replies: 5
    Last Post: 11-15-2010, 12:29 PM
  4. question about array's
    By ChrisE in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2002, 11:56 AM
  5. array question
    By KAchE in forum C Programming
    Replies: 4
    Last Post: 02-18-2002, 06:33 PM