Reputation: 336
I am working on a problem involving arrays. I am recording the marks of students entered into the system, two marks per line, and then storing them into two seperate arrays (Test One and test two). If the user enters e, the system stops asking for marks and prints out the values of the marks for each student.
using System;
class Marks
{
static void Main()
{
string[] input;
string[] testOne = new string[40];
string[] testTwo = new string[40];
int count = 0;
Console.WriteLine("You are to enter two test marks per student.");
Console.WriteLine("Enter the letter e to end");
for (int i = 0; i <= 40; i++)
{
Console.Write("Enter marks for student " + (i+1) + ": ");
input = Console.ReadLine().Split(',');
if (input[0] == "e") break;
else if (input[0] != "e")
{
testOne[i] = input[0];
testTwo[i] = input[1];
}
count++;
}
Console.WriteLine("\nStudent Test 1 Test 2 Total");
Console.WriteLine("======= ====== ====== =====");
for (int i = 1; i <= count; i++)
{
Console.WriteLine(i + " " + testOne[i] + " " + testTwo[i]);
}
}
}
For the last block of code, where I am outputting the values of the array, I cannot seem to get it right. I'm trying to output the value stored in the array for each student.
Any help would be appreciated!
UPDATE:
Thanks for the help!
The sample output is as follows,
You are to enter two test marks per student.
Enter the letter e to end.
Enter marks for student 1: 10, 14
Enter marks for student 2: 13, 9
Enter marks for student 3: 11, 7
Enter marks for student 4: 0, 18
Enter marks for student 5: e
Student Test 1 Test 2 Total
======= ====== ====== =====
1 10.0 14.0 26.0
2 13.0 9.0 24.0
3 11.0 7.0 20.0
4 0.0 18.0 27.0
As you can see, I can enter multiple marks for each student, up till 40 students or until the user enters e. Don't worry about total values as I can sort that out myself, but for now I simply want to get the values outputted.
Upvotes: 0
Views: 3874
Reputation: 4727
I don't know exactly what the problem is. What I see is the following:
You create an array with 40 entries. Your first loop loops 41 times, so at the last position, you'll get an execption. The second loop starts by 1 and so, you are ignoring the first student. You should have the following code:
For the first loop:
for (int i = 0; i < testOne.Length; i++)
{
}
And for your second loop:
for (int i = 0; i < count; i++)
{
Console.WriteLine((i+1) + " " + testOne[i] + " " + testTwo[i]);
}
EDIT:
To format it somehow like in your example, you can work with number formats and with \t (tab). Please be aware that you should check if the input is an int before parsing it (I don't do this in the example here, for an easier explanation). If you would like to right align it, you need to check if the value is < 10, and the add spaces etc. But remember, it's a console app, so the output don't need to be perfect in each case :)
Console.WriteLine("\nStudent\tTest 1\tTest 2\tTotal");
Console.WriteLine("=======\t======\t======\t=====");
for (int i = 0; i < count; i++)
{
int one = int.Parse(testOne[i]);
int two = int.Parse(testTwo[i]);
Console.WriteLine((i + 1) + "\t" + one.ToString("0.0") + "\t" + two.ToString("0.0") + "\t" + (one + two).ToString("0.0"));
}
Or try what MAV mentioned in the comment :)
Upvotes: 2
Reputation: 2746
Try:
for (int i = 0; i <= count; i++)
{
Console.WriteLine(i+1 + " " + testOne[i] + " " + testTwo[i]);
}
Also fix first loop so you don't overrun the array. Change:
for (int i = 0; i <= 40; i++)
To:
for (int i = 0; i < 40; i++)
Upvotes: 0