Could you check this C language Code... The Output is not complete... it doesnt print the firstclas name n id?
my assignment is Write a complete C program that will meet the following requirement:
• Create student detail must contain each student’s name (as a string), id (as integer) and CGPA (double).
• Create 20 students whose names are student1, studnet1, and so on to student20 whose id and status is assigned randomly.
• Find all ‘first class’ student(s) and print their name and ID.


# include <stdio.h>
int main (void) {

typedef struct
{
char name[20];
double cgpa;
int id;
} Detail;

//This will create a new type for you to use...just like int or char...

//Then you'll need

int student_number = 2; //so we can count
Detail student_records[2]; //holds the students

int i;
int j; //Both for loops

for(i = 0; i < student_number; i++){
printf("\nEnter Student Name: ");
scanf ("%s", &student_records.name);

printf("Enter UserID : ");
scanf ("%d", &student_records.id);

printf("Enter Student CGPA: ");
scanf ( "%f", &student_records.cgpa);

}

//Then you need to loop through to check the data...
for(j = 0; j < student_number; j++){
//Check for the stuff you wanted
if( student_records.cgpa >= 3.6){
printf("The First Class Student Are : %s and his metric id is: %d", &student_records.name, &student_records.id);
}
else{
printf("No one is a first class"); }

}//end for loop


return 0;

}


THANKS IN ADVANCE

Dani AI

Generated

Short summary and concrete fixes (ties to existing replies): is correct that the array must hold 20 students; is right about the scanf/address-of and the loop-variable mixup. On top of those, common-but-subtle bugs to fix here are: using the wrong scanf format for a double, risking string overflow when using %s, printing "No one is a first class" inside the loop (which prints it once per non‑first‑class student), and not initializing/creating the 20 student records programmatically as the assignment asks.

Concrete tips and rules to follow

  • Use a named constant for the count (e.g. #define STUDENTS 20) and for the cutoff (e.g. FIRST_CLASS 3.6).
  • Generate the required names (student1..student20) with snprintf into a fixed-size buffer to avoid overflow.
  • For random IDs and CGPAs use srand(time(NULL)), and produce a double CGPA with (rand()/(double)RAND_MAX)*4.0.
  • When reading into a char name[20] with scanf, either use fgets or scanf("%19s", students[i].name) (no &). For double, use scanf("%lf", &students[i].cgpa).
  • Don't print "no one" from inside the loop; use a found flag and print a single message after the scan loop if none matched.
  • Compile with warnings enabled (e.g. -Wall) and check scanf return values during input.

Minimal example that implements the assignment (creation, random id/cgpa, and printing first-class students):

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

#define STUDENTS 20
#define FIRST_CLASS 3.6

typedef struct { char name[16]; double cgpa; int id; } Detail;

int main(void) {
    Detail s[STUDENTS];
    srand((unsigned)time(NULL));
    for (int i = 0; i < STUDENTS; ++i) {
        snprintf(s[i].name, sizeof s[i].name, "student%d", i+1);
        s[i].id = 1000 + rand() % 9000;
        s[i].cgpa = (rand() / (double)RAND_MAX) * 4.0;
    }
    int found = 0;
    for (int i = 0; i < STUDENTS; ++i)
        if (s[i].cgpa >= FIRST_CLASS) {
            printf("%s (id %d) cgpa %.2f\n", s[i].name, s[i].id, s[i].cgpa);
            found = 1;
        }
    if (!found) puts("No first-class students.");
    return 0;
}

Additional diagnostics: run with -Wall -Wextra, print the generated CGPAs to verify distribution, and if taking user input prefer fgets + strtoumax/strtod parsing to robustly handle bad input.

Recommended Answers

All 2 Replies

Basically your loop only gets the information of 2 students, not of all 20 like you suppose to.
Shouldn't your loop be

int student_number=20;
for(i = 0; i < student_number; i++){

the same thing to check the GPA. You want to check all the 20 students to see which ones have the GPA higher tha 3.6 am I right?

First up, as a newbie, you need to learn how to post code correctly on these forums using code tags. You'll find that people will be more willing to help you out if you're prepared to show some effort in this area. Please read the information at the following link to learn how to use code tags:
http://www.daniweb.com/forums/thread93280.html

Now moving on to your code. In addition to the anomaly that Samyx has pointed out, here are some other things to consider:
1. When using scanf() with strings, the address-of operator (&) is not required. You're doing this when scanning input for the student names.

2. While on the subject of the address-of operator, you're using them in the for loop that prints out student details - these are not required.

3. You have declared two variables (i and j) for use as loop variables. Only one is required (drop j).

4. You have a bug in the code that prints out first-class student details. You use the variable j in your loop statement and then use the variable i to reference your array elements. As in point 3, drop the j variable and change the code here accordingly.

5. A minor point - you can probably lose the else clause - the requirement only asks that you print the details for those students that are "first-class".

6. Try to avoid the use of "magic" numbers - you should probably declare constants for the number of students and the GPA cut off mark.

commented: Very good suggestion. +17
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.