zoey
zoey

Reputation: 1

C programming language

Codeblocks is asking me for an extra input than required after my first input it prompts for another input without the print statement that gets printed for every input prompt.

#include <stdio.h>
//Computing marks and average of students using 2D arrays
void main()
{
    int i,j,sum,marks[3][5];
    float avg;
    printf("Program to compute average marks of 3 students.\n");
    for(i=0;i<3;i++)
    {   for(j=0;j<5;j++)
    {
        printf("Enter marks for student %d in subject %d:\t",i+1,j+1);
        scanf("%d ",&marks[i][j]);
    }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<5;j++)
        {
            sum=sum+marks[i][j];
        }
        avg= sum/5.0;
        printf("The average marks of student %d is %f:\n",i+1,avg);
         }
    getch();
}

Upvotes: 0

Views: 102

Answers (3)

August Karlstrom
August Karlstrom

Reputation: 11377

The format string in scanf should be "%d" (without a space). You have also forgotten to initialize the sum variable. Below is a corrected version of your code including a convenient array length macro. Hope this helps.

#include <stdio.h>
#include <stdlib.h>

#define LEN(arr) (sizeof (arr) / sizeof (arr)[0])

/*Computing marks and average of students using 2D arrays*/

int main()
{
    int i, j, sum, marks[3][5], count;
    float avg;

    printf("Program to compute average marks of 3 students.\n");
    for (i = 0; i < LEN(marks); i++) {
        for (j = 0; j < LEN(marks[0]); j++) {
            printf("Enter marks for student %d in subject %d:\t", i + 1, j + 1);
            count = scanf("%d", &marks[i][j]);
            if (count != 1) {
                fprintf(stderr, "invalid input\n");
                exit(EXIT_FAILURE);
            }
        }
    }
    for (i = 0; i < LEN(marks); i++) {
        sum = 0;
        for (j = 0; j < LEN(marks[0]); j++) {
            sum = sum + marks[i][j];
        }
        avg = ((float) sum) / ((float) LEN(marks[0]));
        printf("The average marks of student %d is %f:\n", i + 1, avg);
    }

    return 0;
}

Upvotes: 2

Serge Ballesta
Serge Ballesta

Reputation: 148890

As said in comments, void main() is bad: you should use int main() and end your program with return 0;

But the problem is simply caused by an unnecessary space in your input format at "%d ". This removes the problem:

    scanf("%d",&marks[i][j]);

Upvotes: 0

BigO
BigO

Reputation: 98

Problem in code : Your format string like scanf("%d ",&marks[i][j]); requires whitespace after input which causes anomaly.

Corrected Code :

#include <stdio.h>
//Computing marks and average of students using 2D arrays
int main()
{
    int i,j,sum = 0,marks[3][5];
    float avg;

    printf("Program to compute average marks of 3 students.\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<5;j++)
        {
            printf("Enter marks for student %d in subject %d:\t",i+1,j+1);
            scanf("%d",&marks[i][j]);
        }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<5;j++)
        {
            sum=sum+marks[i][j];
        }
        avg= sum/5.0;
        printf("The average marks of student %d is %f:\n",i+1,avg);
    }
    return 0;
}

as specification says

Number,order and type of conversion specifications must match the number,order and type of parameters in the list

. Otherwise, the result will be unpredictable and may terminate input/output function.

Upvotes: 2

Related Questions