Recap: Student Data Struct
Part of the Logic & Flow section of Coddy's C journey — lesson 43 of 63.
Challenge
EasyCreate a C program that demonstrates the complete workflow of struct usage with a student management system. Your program should:
- Define a
structnamedStudentwith the following members:- An integer
idto store the student's ID number - A float
gradeto store the student's grade
- An integer
- In the main function, create a
Studentvariable namedstudent1 - Read the student's ID and grade from input using
scanf:- Read an integer for the ID and store it in
student1.id - Read a float for the grade and store it in
student1.grade
- Read an integer for the ID and store it in
- After reading the input, perform the following validation and calculations:
- If the grade is greater than 100.0, set it to 100.0
- If the grade is less than 0.0, set it to 0.0
- Calculate a bonus grade by adding 5.0 to the current grade (after validation)
- If the bonus grade exceeds 100.0, set the bonus grade to 100.0
- Print the student information in this exact format:
Student Information:ID: [id]Original Grade: [grade]Bonus Grade: [bonus_grade]Grade Status: [status]
- The grade status should be determined as follows:
- If the original grade is 90.0 or above:
Excellent - If the original grade is 80.0 to 89.9:
Good - If the original grade is 70.0 to 79.9:
Average - If the original grade is below 70.0:
Needs Improvement
- If the original grade is 90.0 or above:
This challenge tests your understanding of struct definition, variable creation, member access using the dot operator, input handling with scanf, and conditional logic for data validation and processing. You'll practice the complete workflow of working with structs in a practical scenario.
Try it yourself
#include <stdio.h>
// TODO: Define the Student struct here
int main() {
// TODO: Create a Student variable named student1
// Read input
int id;
float grade;
scanf("%d", &id);
scanf("%f", &grade);
// TODO: Store the input values in the struct members
// TODO: Implement grade validation and bonus calculation
// TODO: Determine grade status
// Output the results
printf("Student Information:\n");
printf("ID: %d\n", /* TODO: print student ID */);
printf("Original Grade: %.1f\n", /* TODO: print original grade */);
printf("Bonus Grade: %.1f\n", /* TODO: print bonus grade */);
printf("Grade Status: %s\n", /* TODO: print grade status */);
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters7Structures (structs)
What is a Struct?Declaring a StructCreating Struct VariablesAccessing Struct MembersInitializing StructsRecap: Student Data Struct2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions