I am trying to create a program can read marks of 10 student for different marks using C program but when I debug the output that shown was not the value that I input before, it keeps showing 7 digit value. Need someone advise, what is the misstake of my code. Below is my code.

#include <stdio.h> 
int main() 
{ 
int i, j;  
int marks[2][10];
printf("Enter the marks of the ten students :\n");

for(i=0;i<10;i++)
{
  printf("\nEnter the marks of student :%d",i);

  printf("\nAssignment Mark :");
  scanf("%d",marks[0]);  

  printf("Exam Mark :");
  scanf("%d",marks[1]);
}
printf("****************************************************************\n");
for(j=0;j<10;j++)
{
 printf("Student No :%d\n",j);
 printf("Assignment Marks :%d\n",&marks[0]);
 printf("Exam Marks :%d\n",&marks[1]);            
}                 
getch();
return 0;
}

Recommended Answers

It's a combination of syntax and addressing. Try

const int NUMSTUDENTS = 10;

int main()
{
    int marks[2][NUMSTUDENTS];
    printf("Enter the marks of the %d students :\n",NUMSTUDENTS);

    for (int i = 0; i < NUMSTUDENTS; i++)
    {
        printf("\nEnter the marks of student :%d", i+1);
        printf("\nAssignment Mark :");
        scanf("%d", &marks[0][i]);
        printf("Exam Mark …
Jump to Post

All 2 Replies

It's a combination of syntax and addressing. Try

const int NUMSTUDENTS = 10;

int main()
{
    int marks[2][NUMSTUDENTS];
    printf("Enter the marks of the %d students :\n",NUMSTUDENTS);

    for (int i = 0; i < NUMSTUDENTS; i++)
    {
        printf("\nEnter the marks of student :%d", i+1);
        printf("\nAssignment Mark :");
        scanf("%d", &marks[0][i]);
        printf("Exam Mark :");
        scanf("%d", &marks[1][i]);
    }
    printf("****************************************************************\n");
    for (int i = 0; i < NUMSTUDENTS; i++)
    {
        printf("Student No :%d\n", i+1);
        printf("Assignment Marks :%d\n", marks[0][i]);
        printf("Exam Marks :%d\n", marks[1][i]);
    }
    getchar();
    return 0;
}

Please note that I replaced the literal 10 with NUMSTUDENTS. You should really break the habit of hard coding literals. If you had a longer program with a 10 sprinkled throughout you could easily bury yourself in bugs if you later had to change the 10 to some other value. And you might end up changing a non-related 10 by mistake. The const NUMSTUDENTS is clear and unambiguous.

Malpractice makes malperfect.

Yes, got it. Look like I mess up the code and get confusing in the bugs. I agree with you, using the CONST, it's unambiguous. It's good to learn something new. Thank you =)