0% found this document useful (0 votes)
6 views2 pages

Exercise3

The document outlines a program for managing student information using arrays to store names and grades in a procedural approach. It details the input of data for three students and includes loops for printing student information and calculating average grades. The document also describes a transition to an object-oriented programming (OOP) approach by creating a Student class with properties and methods for better data management.

Uploaded by

jn4304814
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Exercise3

The document outlines a program for managing student information using arrays to store names and grades in a procedural approach. It details the input of data for three students and includes loops for printing student information and calculating average grades. The document also describes a transition to an object-oriented programming (OOP) approach by creating a Student class with properties and methods for better data management.

Uploaded by

jn4304814
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise 03

Scenario: We want to manage student information, specifically their names and grades for
three different subjects (Math, Science, English).

Explanation:

• We use four 1D arrays to store student data: names, mathGrades, scienceGrades,


and englishGrades.

• The index of the arrays corresponds to the student. For example, names[0] is the
name of the first student, mathGrades[0] is their Math grade, and so on.

• We have separate loops to print student information and calculate the average
grade.

Procedural Approach (without OOP)

using System;

public class ProceduralStudent


{
public static void Main(string[] args)
{
// Data for 3 students
string[] names = new string[3];
int[] mathGrades = new int[3];
int[] scienceGrades = new int[3];
int[] englishGrades = new int[3];

// Input data for student 1


names[0] = "Alice";
mathGrades[0] = 85;
scienceGrades[0] = 90;
englishGrades[0] = 78;

// Input data for student 2


names[1] = "Bob";
mathGrades[1] = 70;
scienceGrades[1] = 80;
englishGrades[1] = 85;

// Input data for student 3


names[2] = "Charlie";
mathGrades[2] = 92;
scienceGrades[2] = 88;
englishGrades[2] = 95;
// Print student information
for (int i = 0; i < 3; i++)
{
Console.WriteLine($"Name: {names[i]}, Math: {mathGrades[i]},
Science: {scienceGrades[i]}, English: {englishGrades[i]}");
}

// Calculate and print average grade for each student


for (int i = 0; i < 3; i++)
{
double average = (mathGrades[i] + scienceGrades[i] +
englishGrades[i]) / 3.0;
Console.WriteLine($"{names[i]}'s Average: {average}");
}
}
}

Now you’re turn. Let’s turn this program into OOP by following the instructions below:

• We create a Student class. This is the blueprint for a student object.

• Name: A property to store the student's name.

• Grades: An array to store the grades for the three subjects.

• GetAverageGrade(): A method (function inside the object) to calculate the average


grade.

• We create an array of Student objects: Student[] students = new Student[3];

• Each element of the students array holds a Student object.

• We initialize each student's Name and Grades.

• We loop through the students array, printing the information and using
the GetAverageGrade() method to calculate and print the average.

You might also like