Question : In Bouboun University's Information Systems 3 arrays are used to store student details,
1. Stud_Names (array of strings) holds the name of 100 Students
2. Stud_Map (array of strings) hold the ids of the 100 students (whose details are stored in Stud_Name)
3. Stud_Marks_Mod1 (array of floats) holds the grades of grades of each of the 100 students in Module1
4. Stud_Marks_Mod2 (array of floats) holds the grades of grades of each of the 100 students in Module2
5. Stud_Marks_Mod3 (array of floats)holds the grades of grades of each of the 100 students in Module3

Write a program in C which computes the average marks for each student and displays the highest average.

Problem: when i enter details for the first student it is correct but when i enter the second student details i cannot enter the name it skip it and student ID come in its place ant help how to correct the codes? OPPS I POSTED MY PROBLEM IN THE C++ SECTION SORRY IT WAS IN A HURRY.

My program:

#include <stdio.h>
#include <string.h>

int main()
{
    char *Stud_Names[100];
    char name;
    char Stud_ID[100];
    float Stud_Marks_Mod1[100];
    float Stud_Marks_Mod2[100];
    float Stud_Marks_Mod3[100];

    int b=1,i,sum=0;
    float avg,avg1;
    int max=0;


    for(i=0;i<2;i++)
    {
        printf("Enter student %d name: ", b);
        gets(Stud_Names);
        Stud_Names[i]=name;
        printf("Enter student %d ID: ", b);
        scanf("%s", &Stud_ID[i]);
        printf("Enter student %d marks for module 1: ", b);
        scanf("%f", &Stud_Marks_Mod1[i]);
        printf("Enter student %d marks for module 2: ", b);
        scanf("%f", &Stud_Marks_Mod2[i]);
        printf("Enter student %d marks for module 3: ", b);
        scanf("%f", &Stud_Marks_Mod3[i]);

        sum = Stud_Marks_Mod1[i] + Stud_Marks_Mod2[i] + Stud_Marks_Mod3[i];

        avg1=sum/3;

        printf("Average of student %d mark is %.2f \n",i+1,avg1);

        b++;
    }

if(avg1>max)
{
    printf("Highest average mark is %.2f ", avg1);
}

Edited 2 Years Ago by nitish.mohiputlall: ERROR

Line 22, I thought you would have noticed but the char name is assigned to Stud_Names[i]. name is never initialised.

name is a single character, it is not an array (see line 7). Therefore you can only enter a one-character name. I doubt that is what you want. Also, Stud_Names is an array of pointers, so if you assign names to each pointer then all pointers will contain the same name. For example if you enter two student names, John and Mary, then Stud_Names[0] and Stud_Names[1] will both point to "Mary" because names contains the word "Mary".

In C++ you have a couple options:

1.change Stud_Names to be an array of std::string objects
std::string Stud_Names[100];

2.leave Stud_Names as an array of pointers, then call strdup to duplicate the names: This is the same as malloc and strcpy.

Stud_Names[i] = strdup(name);

Now for Stud_ID: This is a 1d array of 100 characters, yet you are treating it as a 2d array. Line 24 won't work because all you can enter for &Stud_ID[i]) is a single digit and the scanf statement uses "%c" not "%s".

scanf("%c", &Stud_ID[i]);

Edited 2 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.