Hi everyone.

My assignment this time requires me to:

1. List down the marks obtained for each 5 Quizzes.

2. Choose the best 2 highest marks out of the 5 Quizzes.

3. To make the "best 2 highest marks" becomes the ultimate Quizzes total mark which has a 10% carrying mark as part of the Coursework mark for the subject.

So my solution answer is down here:

#include <stdio.h>
main()
{
int mark 1, mark 2, mark 3, mark 4, mark 5, The best 2 highest marks;
printf("Enter your Quiz 1 mark:");
scanf("%d", & mark 1);
printf("Enter your Quiz 2 mark:");
scanf("%d", & mark 2);
printf("Enter your Quiz 3 mark:");
scanf("%d", & mark 3);   
printf("Enter your Quiz 4 mark:");
scanf("%d", & mark 4);  
printf("Enter your Quiz 5 mark:");
scanf("%d", & mark 5); 
The best 2 highest marks = mark > 50 + mark > 50
if ( mark > 50 ){
printf (" mark > 50 ");
else {
printf (" the mark is not one of the best 2 highest marks ");
}
return 0;
}

Can anyone tell me if I've done this right, or wrong? Please and thanks in advance (^_^)

Dani AI

Generated

The original attempt from contains several syntactic problems and no real logic to pick the top two marks. correctly called out illegal variable names and uninitialized values, and correctly noted the code never selects the two highest marks. A simple, robust approach is to read the five marks into an array, validate their range, then either sort the array or find the two largest with a single pass.

Algorithm (single pass): initialize max1 and max2 to a sentinel (for example -1). For each mark m: if m > max1 then set max2 = max1 and max1 = m; else if m > max2 then set max2 = m. The quiz contribution as a 10% coursework component depends on the per-quiz maximum N. The formula is: contribution_out_of_10 = (max1 + max2) / (2 N) 10.0. For N=100 that is (sum / 200.0) * 10.0. Cast to double to avoid integer division.

A compact, correct C implementation (assumes per-quiz max = 100):

#include <stdio.h>

int main(void) {
    int marks[5], i;
    for (i = 0; i < 5; ++i) {
        printf("Enter Quiz %d mark: ", i + 1);
        if (scanf("%d", &marks[i]) != 1) return 1;
        if (marks[i] < 0 || marks[i] > 100) {
            fprintf(stderr, "Invalid mark: %d\n", marks[i]);
            return 1;
        }
    }

    int max1 = -1, max2 = -1;
    for (i = 0; i < 5; ++i) {
        int m = marks[i];
        if (m > max1) { max2 = max1; max1 = m; }
        else if (m > max2) { max2 = m; }
    }

    int sum = max1 + max2;
    double weighted = (double)sum / 200.0 * 10.0;
    printf("Best two: %d and %d\nSum = %d, contribution = %.2f / 10\n", max1, max2, sum, weighted);
    return 0;
}

Practical notes: check scanf return values, compile with warnings (e.g. gcc -std=c11 -Wall -Wextra), and adjust the divisor (2 * N) if each quiz is not out of 100. For robust input parsing consider fgets + strtol instead of scanf.

Recommended Answers

All 2 Replies

int mark 1, mark 2, mark 3, mark 4, mark 5, The best 2 highest marks;

space is not used in variable name declaration

The best 2 highest marks = mark > 50 + mark > 50

This is not how you use an operation statement

if ( mark > 50 ){
printf (" mark > 50 ");
else {
printf (" the mark is not one of the best 2 highest marks ");
}

mark is not initialized

Did you compile your work cause you can see most of your errors there

Your C syntax is completely wrong.
Nowhere in your code do you even attempt to find the 2 highest marks.

So, no, you have not done it right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.