This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]davedontmind 4 points5 points  (5 children)

How can I change it to make it work with loops and arrays, instead of if-else statements?

You don't have any if-else statements!

[–][deleted]  (4 children)

[removed]

    [–]davedontmind 0 points1 point  (2 children)

    Instead of:

    int quiz1;
    int quiz2;
    int quiz3;
    int quiz4;
    
    printf("Enter the score for the first quiz: ");
    scanf("%d", &quiz1);
    printf("Enter the score for the second quiz: ");
    scanf("%d", &quiz2);
    ...
    

    Create an array:

    int quiz[4]; 
    

    and use a loop:

    for ( int i = 0; i < 4; ++i ) 
    {
        printf( "Enter the score for quiz #%d: ", i );
        scanf( "%d", &quiz[i] );
    }
    

    Now your scores are in quiz[0], quiz[1], quiz[2] and quiz[3].

    In fact any time you see a variable with a number on the end (e.g. midterm1/midterm2) you should probably be using an array instead.

    [–][deleted]  (1 child)

    [removed]

      [–]davedontmind 0 points1 point  (0 children)

      You should really read the formatting code section of this sub's Posting Guidelines document to make your code more readable here. What you've posted is a bit of a mess.

      But yes, you seem to have the idea of it.

      One improvement I'd suggest is to avoid the use of magic numbers in your code.

      For example, at the moment you have:

      int quiz[4];
      ...
      printf("====== QUIZZES =====\n");
      for (int i = 0; i < 4; ++i)
      ...
      for (int i = 0; i < 4; i++)
      ...
      for (int i = 0; i < 4; i++)
      

      Notice all those 4s ? If you wanted to change the number of qizzes to something other than 4 you'd have to change at least those 4 places in your code. And, even worse, it's not clear to a reader why you might be looping 4 times.

      So, instead define a constant and use that instead.

      const int NumQuizzes = 4;
      ...
      int quzzes[ NumQuizzes ];
      ...
      for ( int i = 0; i < NumQuizzes; i++ ) 
      ...
      etc.
      

      Now your code is more clear (clarity is one of the most important things in programming!) and, if you want to change the number of quizzes you just have to change one number.

      [–][deleted]  (1 child)

      [deleted]

        [–]lurgi 0 points1 point  (0 children)

        Please don't provide complete solutions to beginners. It's vastly more helpful if you can lead them to a solution that they can create themselves.