in this code i am not able to print the output i.e after for loop execution i want the total details which I entered

+1 vote
asked Nov 9 by GADDAM SUSMITHA (130 points)
#include <stdio.h>
int main()
{
    char name[10][100];
    int phy[10][10];
    int maths[10][10];
    int chem[10][10];
    int no;
    printf("how many member of student details u want to enter:");
    scanf("%d",&no);
    for(int i=0;i<=no-1;i++)
    {
        
        printf("enter student %d info\n",i+1);
        printf("name:");
        scanf("%s",name[i]);
        printf("phy:");
        scanf("%d",phy[i]);
        printf("maths:");
        scanf("%d",maths[i]);
        printf("chem:");
        scanf("%d",chem[i]);
        
    }
    printf("name\tphy\tmaths\tchem\t\n");
    for(int j=0;j<=no-1;j++)
    {
        printf("%s\t%d\t%d\t%d\t\n",name[j],phy[j],maths[j],chem[j]);;
     
        
        
    }
    
}

1 Answer

+1 vote
answered Nov 12 by Peter Minarik (12,220 points)

I've fixed your code and you can find it below.

The point is that you misused your arrays (pointers).

You want to store maximum 10 names, physics, maths and chemistry grades. So the name can be a character array (string) up to 100 characters. Physics/Maths/Chemistry grades can be numbers. And you want an array of 10 of these. Hence the correct array usage is below.

#include <stdio.h>

int main()
{
    char name[10][100];
    int phy[10];
    int maths[10];
    int chem[10];
    int no;
    printf("How many member of student details would you like to enter: ");
    scanf("%d", &no);

    for (int i = 0; i < no; i++)
    {
        printf("enter student %d info\n", i + 1);
        printf("name: ");
        scanf("%s", name[i]);
        printf("phy: ");
        scanf("%d", &phy[i]);
        printf("maths: ");
        scanf("%d", &maths[i]);
        printf("chem: ");
        scanf("%d", &chem[i]);
    }

    printf("name\tphy\tmaths\tchem\t\n");
    for (int j = 0; j < no; j++)
    {
        printf("%s\t%d\t%d\t%d\t\n", name[j], phy[j], maths[j], chem[j]);
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...