0

New c# learner, this program is to input grades and then figure the average. However, I want to use specific student ID numbers (ie 3345, 6654, etc.) rather than the looped numbers as IDs. How can I edit this code to reflect this? Thanks in advance.

 class ClassScores
 {
 private Score[] scores;
 private int index;
 public ClassScores(int n)
 {
 scores = new Score[n];
 index = 0;
 }
 public void addScore(Score obj)
 {
 scores[index] = obj;
 index++;
 }
 public Score highScore()
 {
 Score obj = scores[0];
 for (int i = 0; i < index; i++)
 {
 if (scores[i].getGrade() > obj.getGrade())
 {
 obj = scores[i];
 }
 }
 return obj;
 }
 public double average()
 {
 int sum = 0;
 for (int i = 0; i < index; i++)
 {
 sum += scores[i].getGrade();
 }
 return (sum / (double)(scores.Length));
 }
 }
}
 class Score
 {
 private int id;
 private int grade;
 public Score(int studentID, int studentGrade)
 {
 id = studentID;
 grade = studentGrade;
 }
 public void setId(int studentID)
 {
 id = studentID;
 }
 public void setGrade(int score)
 {
 grade = score;
 }
 public int getGrade()
 {
 return grade;
 }
 public int getId()
 {
 return id;
 }
 }
 class Program
 {
 static void Main(string[] args)
 {
 ClassScores classOne = new ClassScores(6);
 for (int i = 1; i <= 6; i++)
 {
 Console.Write("Enter Score for Student " + i + ": ");
 int grade = Convert.ToInt32(Console.ReadLine());
 Score classTwo = new Score(i, grade);
 classOne.addScore(classTwo);
 }
 Console.WriteLine("\n\n Average Score: " + classOne.average());
 Score high = classOne.highScore();
 Console.WriteLine("\n\n Student with id: " + high.getId() + " has a Highest Score: " + high.getGrade() + "\n\n");
 Console.Read();
 }
 }

It will need an array of six different student ID numbers.

asked Dec 5, 2019 at 1:01
3
  • 1
    You have several problems here, try reducing your code and question to one problem at a time. Commented Dec 5, 2019 at 1:27
  • Okay, now there is a single question. I wouldn't know how to cut down the code without compromising the question. Commented Dec 5, 2019 at 1:32
  • Side note: learning C# using Java book? Look into learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… (or in properties in general) Commented Dec 5, 2019 at 2:15

1 Answer 1

1

You can change your for loop to prompt the user to enter the Student ID. You can then pass that to the Score class constructor:

for (int i = 1; i <= 6; i++)
{
 Console.Write("Enter Student ID:");
 int id = Convert.ToInt32(Console.ReadLine());
 Console.Write("Enter Score for Student " + id + ": ");
 int grade = Convert.ToInt32(Console.ReadLine());
 Score classTwo = new Score(id, grade);
 classOne.addScore(classTwo);
}

EDIT

If you already have your Student ID's in an array you can loop over that array:

int[] studentIds = new[] { 3524, 4321, 546, 7896, 4327, 123 };
ClassScores classOne = new ClassScores(studentIds.Length);
foreach(int id in studentIds)
{
 Console.Write("Enter Score for Student " + id + ": ");
 int grade = Convert.ToInt32(Console.ReadLine());
 Score classTwo = new Score(id, grade);
 classOne.addScore(classTwo);
}

We have changed the for loop to a foreach which will loop over each item in the array and retrieve each value

If you still want to use a for loop instead then you can use the loop variable to get the ID from the array - remember arrays are zero based so you need to loop from zero to the length of the array:

for (int i = 0; i < studentIds.LEngth; i++)
{
 int id = studentIds[i];
 ...
}
answered Dec 5, 2019 at 2:06

2 Comments

This is helpful! I do need it to already have the ID's stored in an array, though.
@teestokes12 I've updated my answer to include an array of student ids - you didn't put that in your original question so I've had to guess the numbers

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.