C++ Programming Assignment: From Basics to Functions & Data Structures
Learning Objectives
By completing this assignment, students will be able to:
Use variables, data types, and operators effectively in C++ programs
Implement conditional statements and loops for program flow control
Create and call functions with parameters and return values
Work with arrays and vectors for storing and manipulating collections of data
Use strings and perform basic string operations
Read from and write to files
Apply these concepts to solve real-world programming problems
Assignment Overview
In this assignment, you will build a Student Grade Management System that evolves from basic input/output to a more sophisticated program using functions, arrays, and file I/O. Each challenge builds on previous skills while introducing new intermediate concepts.
Real-World Context: Schools need to track student grades, calculate averages, and generate reports. Your program will help teachers manage this data efficiently.
Challenge 1: Grade Input & Basic Calculations (Beginner)
Problem Description
Create a program that allows a teacher to input grades for a single student and calculate basic statistics (average, highest, lowest).
Requirements
Prompt the user to enter the student's name
Ask how many grades the student has
Use a loop to collect each grade
Calculate and display:
Average grade
Highest grade
Lowest grade
Use proper data types (string for name, float for grades)
Example Input/Output
Enter student name: Alice Johnson
How many grades does the student have? 5
Enter grade 1: 85
Enter grade 2: 92
Enter grade 3: 78
Enter grade 4: 88
Enter grade 5: 95
--- Grade Report for Alice Johnson ---
Average Grade: 87.6
Highest Grade: 95
Lowest Grade: 78Requirements Checklist
Use
stringfor student nameUse
intfor number of gradesUse
floatfor grade valuesImplement a loop to collect grades
Calculate average, max, and min correctly
Display results with clear formatting
Starter Code
#include <iostream>
#include <string>
using namespace std;
int main() {
string studentName;
int numGrades;
float grade;
float sum = 0;
float highest = 0;
float lowest = 100;
// TODO: Prompt for student name
// TODO: Prompt for number of grades
// TODO: Loop to collect grades and calculate sum, highest, lowest
// TODO: Calculate and display average, highest, lowest
return 0;
}Hints
Use
getline(cin, studentName)to read a full name with spacesInitialize
lowestto a high value (like 100) so any grade will be lowerUse
sum / numGradesto calculate the averageConsider using
ifstatements inside your loop to track highest and lowest
Challenge 2: Letter Grades & Conditional Logic (Beginner)
Problem Description
Extend Challenge 1 to assign letter grades based on the calculated average.
Requirements
Use the program from Challenge 1
Add a function that converts a numeric grade to a letter grade:
A: 90-100
B: 80-89
C: 70-79
D: 60-69
F: Below 60
Display the letter grade alongside the numeric average
Add a pass/fail indicator (Pass if average ≥ 70, Fail otherwise)
Example Input/Output
--- Grade Report for Alice Johnson ---
Average Grade: 87.6
Letter Grade: B
Status: PASS
Highest Grade: 95
Lowest Grade: 78Requirements Checklist
Create a function
char getLetterGrade(float average)Use if/else statements for grade conversion
Display letter grade in output
Display pass/fail status
Handle edge cases (exactly 90, exactly 70, etc.)
Starter Code Addition
// Function to convert numeric grade to letter grade
char getLetterGrade(float average) {
// TODO: Use if/else to return appropriate letter
// Remember: A (90+), B (80-89), C (70-79), D (60-69), F (<60)
}
// In main(), after calculating average:
char letterGrade = getLetterGrade(average);
string status = (average >= 70) ? "PASS" : "FAIL";
cout << "Letter Grade: " << letterGrade << endl;
cout << "Status: " << status << endl;Hints
Use nested
if/elsestatements or aswitchstatementThe ternary operator
? :is useful for pass/fail logicTest your function with boundary values (90, 89, 70, 69)
Challenge 3: Multiple Students with Arrays/Vectors (Intermediate)
Problem Description
Expand the system to manage grades for multiple students. Use an array or vector to store student data.
Requirements
Create a struct or class to hold student information (name, grades, average)
Allow the teacher to input data for multiple students
Store each student's data in a vector
Calculate class statistics:
Class average
Student with highest average
Student with lowest average
Display a summary report for all students
Example Input/Output
How many students? 3
--- Student 1 ---
Enter student name: Alice Johnson
How many grades? 3
Enter grade 1: 85
Enter grade 2: 92
Enter grade 3: 88
--- Student 2 ---
Enter student name: Bob Smith
How many grades? 3
Enter grade 1: 78
Enter grade 2: 82
Enter grade 3: 80
--- Student 3 ---
Enter student name: Carol White
How many grades? 3
Enter grade 1: 95
Enter grade 2: 98
Enter grade 3: 96
=== CLASS SUMMARY ===
Student Name | Average | Letter Grade
---------------------------------------------------
Alice Johnson | 88.3 | B
Bob Smith | 80.0 | B
Carol White | 96.3 | A
Class Average: 88.2
Top Student: Carol White (96.3)
Bottom Student: Bob Smith (80.0)Requirements Checklist
Create a struct to store student data
Use a vector to store multiple students
Implement a function to calculate individual averages
Implement a function to find top and bottom students
Display formatted summary report
Calculate and display class average
Starter Code
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct Student {
string name;
vector<float> grades;
float average;
};
float calculateAverage(vector<float> grades) {
// TODO: Sum all grades and divide by count
}
void displayReport(vector<Student> students) {
// TODO: Display formatted table with all students
}
int main() {
vector<Student> students;
int numStudents;
cout << "How many students? ";
cin >> numStudents;
cin.ignore(); // Clear input buffer
// TODO: Loop to collect student data
// TODO: Calculate averages for each student
// TODO: Display report
return 0;
}Hints
Use
vector<float>inside the struct to store multiple grades per studentUse
iomaniplibrary withsetw()andleftfor formatted table outputThe
cin.ignore()function clears leftover newlines from input bufferConsider creating helper functions for finding min/max students
Challenge 4: File I/O & Data Persistence (Intermediate)
Problem Description
Save and load student grade data from a file so teachers can work with previously entered data.
Requirements
Implement a function to save all student data to a file (grades.txt)
Implement a function to load student data from a file
Display a menu allowing teachers to:
Enter new student data
Load existing data from file
View current data
Save data to file
Exit
File format should be readable and well-organized
Example File Format (grades.txt)
Alice Johnson
3
85 92 88
Bob Smith
3
78 82 80
Carol White
3
95 98 96Requirements Checklist
Create
void saveToFile(vector<Student> students, string filename)functionCreate
vector<Student> loadFromFile(string filename)functionImplement menu system using switch/case
Handle file I/O errors gracefully
Display appropriate messages for save/load operations
Preserve data between program runs
Starter Code
#include <fstream>
void saveToFile(vector<Student> students, string filename) {
ofstream outFile(filename);
if (!outFile.is_open()) {
cout << "Error: Could not open file for writing!" << endl;
return;
}
// TODO: Write each student's data to file
// Format: name on one line, number of grades, grades on next line
outFile.close();
cout << "Data saved to " << filename << endl;
}
vector<Student> loadFromFile(string filename) {
vector<Student> students;
ifstream inFile(filename);
if (!inFile.is_open()) {
cout << "Error: Could not open file for reading!" << endl;
return students;
}
// TODO: Read student data from file
// TODO: Reconstruct Student structs
inFile.close();
return students;
}
void displayMenu() {
cout << "\n=== Grade Management System ===" << endl;
cout << "1. Enter new student data" << endl;
cout << "2. Load data from file" << endl;
cout << "3. View current data" << endl;
cout << "4. Save data to file" << endl;
cout << "5. Exit" << endl;
cout << "Choose option: ";
}
int main() {
vector<Student> students;
int choice;
while (true) {
displayMenu();
cin >> choice;
cin.ignore();
switch(choice) {
case 1:
// TODO: Call function to enter new student
break;
case 2:
students = loadFromFile("grades.txt");
break;
case 3:
displayReport(students);
break;
case 4:
saveToFile(students, "grades.txt");
break;
case 5:
cout << "Goodbye!" << endl;
return 0;
default:
cout << "Invalid option!" << endl;
}
}
}Hints
Use
ofstreamfor writing andifstreamfor readingAlways check if file opened successfully with
.is_open()Use
getline(inFile, name)to read student namesUse
inFile >> gradeto read numeric valuesClose files after use with
.close()
Submission Guidelines
What to Submit
All four C++ source files (.cpp files) with your solutions
A grades.txt file with sample data demonstrating file I/O works
A README.txt file explaining:
How to compile and run your program
Any challenges you encountered
Features you added beyond requirements
How to Compile & Run
g++ -o gradeManager gradeManager.cpp
./gradeManagerOr in an IDE, simply build and run the project.
Submission Checklist
All code compiles without errors
All four challenges completed
Program runs without crashing
File I/O works correctly
Output is formatted and readable
Code includes comments explaining key sections
README file included
Grading Rubric
Criterion | Points | Description |
|---|---|---|
Challenge 1: Basic Input/Calculations | 20 | Correctly reads input, calculates average/min/max |
Challenge 2: Functions & Conditionals | 20 | Letter grade function works; pass/fail logic correct |
Challenge 3: Vectors & Data Structures | 25 | Struct defined; vector used correctly; summary report complete |
Challenge 4: File I/O | 20 | Save/load functions work; menu system functional |
Code Quality | 10 | Clean code, comments, proper variable names, no crashes |
Documentation | 5 | README file clear and complete |
Total | 100 |
Answer Key & Sample Solutions
Challenge 1: Solution
#include <iostream>
#include <string>
using namespace std;
int main() {
string studentName;
int numGrades;
float grade;
float sum = 0;
float highest = 0;
float lowest = 100;
cout << "Enter student name: ";
getline(cin, studentName);
cout << "How many grades does the student have? ";
cin >> numGrades;
for (int i = 1; i <= numGrades; i++) {
cout << "Enter grade " << i << ": ";
cin >> grade;
sum += grade;
if (grade > highest) {
highest = grade;
}
if (grade < lowest) {
lowest = grade;
}
}
float average = sum / numGrades;
cout << "\n--- Grade Report for " << studentName << " ---" << endl;
cout << "Average Grade: " << average << endl;
cout << "Highest Grade: " << highest << endl;
cout << "Lowest Grade: " << lowest << endl;
return 0;
}Challenge 2: Solution
#include <iostream>
#include <string>
using namespace std;
char getLetterGrade(float average) {
if (average >= 90) {
return 'A';
} else if (average >= 80) {
return 'B';
} else if (average >= 70) {
return 'C';
} else if (average >= 60) {
return 'D';
} else {
return 'F';
}
}
int main() {
string studentName;
int numGrades;
float grade;
float sum = 0;
float highest = 0;
float lowest = 100;
cout << "Enter student name: ";
getline(cin, studentName);
cout << "How many grades does the student have? ";
cin >> numGrades;
for (int i = 1; i <= numGrades; i++) {
cout << "Enter grade " << i << ": ";
cin >> grade;
sum += grade;
if (grade > highest) {
highest = grade;
}
if (grade < lowest) {
lowest = grade;
}
}
float average = sum / numGrades;
char letterGrade = getLetterGrade(average);
string status = (average >= 70) ? "PASS" : "FAIL";
cout << "\n--- Grade Report for " << studentName << " ---" << endl;
cout << "Average Grade: " << average << endl;
cout << "Letter Grade: " << letterGrade << endl;
cout << "Status: " << status << endl;
cout << "Highest Grade: " << highest << endl;
cout << "Lowest Grade: " << lowest << endl;
return 0;
}Challenge 3: Solution
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct Student {
string name;
vector<float> grades;
float average;
};
char getLetterGrade(float average) {
if (average >= 90) return 'A';
else if (average >= 80) return 'B';
else if (average >= 70) return 'C';
else if (average >= 60) return 'D';