Thread: Need Help

  1. #1
    Registered User Ghouri's Avatar
    Join Date
    Apr 2016
    Posts
    2

    Exclamation Need Help

    --> what is the purpose of if statement in this code;
    --> what is purpose of (i-1) in average formula;

    Code:
    #include <stdio.h>
    #define LIMIT 130
    
    
    int main()
    {
        float marks[LIMIT];
        float average;
        float sum=0.0;
        int i=0;
        do {
        printf("\nEnter the marks of the student(-1 to finish)");
        scanf("%f", &marks[i]);
        if (marks[i] >= 0)
          sum = sum + marks[i];
      } while(marks[i++] >= 0);
      average = sum/(i-1);
      printf ("\nThe average is %.2f\n", average);
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    What do you think the purpose of the if statement is?

    What do you think the purpose of the (i - 1) in the formula?

    You have to at least try. We aren't an answer factory.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User Ghouri's Avatar
    Join Date
    Apr 2016
    Posts
    2
    oka i have tried it.....yet it gives no error if i remove these but........i need explaination

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Why do you think there should be an error if you remove them? The question is "what is the purpose?"

    What do those lines do? How does the behavior of the program change if you remove them?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    "what is the purpose of if statement in this code"
    Consider the instructions "-1 to finish".

    "what is purpose of (i - 1) in average formula"
    Consider the value of i after the loop. (Maybe print it.)

    --
    See how it sings like a sad heart
    And joyously screams out it's pain
    Sounds that build high like a mountain
    Or notes that fall gently like rain

Popular pages Recent additions subscribe to a feed

Tags for this Thread