Reputation:
My inner most nested for loop is not counting correctly. It turns into an infinite loop and I'm not seeing why. Does it have something to do with
studentScores.Add(intScore);
being inside the nested for loop?
class Program
{
static void Main(string[] args)
{
string studentCount = string.Empty;
string examCount = string.Empty;
Console.WriteLine("Please enter the number of students you will enter.");
studentCount = Console.ReadLine();
int totalStudents = Convert.ToInt32(studentCount);
Console.WriteLine(string.Empty);
Console.WriteLine("Please enter the number of exams to be entered for each student.");
examCount = Console.ReadLine();
int totalExams = Convert.ToInt32(examCount);
Console.WriteLine(string.Empty);
Dictionary<int, List<int>> studentMap = new Dictionary<int, List<int>>();
List<int> studentScores = new List<int>();
for (int students = 0; students < totalStudents; students++)
{
for (int scores = 0; scores < totalExams; scores++)
{
string score = string.Empty;
Console.WriteLine("Enter exam");
score = Console.ReadLine();
int intScore = Convert.ToInt32(score);
studentScores.Add(intScore);
}
}
}
}
Upvotes: 0
Views: 97
Reputation: 4104
You can use following code snippet. See here for full working code:
Dictionary<int, List<int>> studentMap = new Dictionary<int, List<int>>();
for (int students = 0; students < totalStudents; students++)
{
//Create List<int> here for each student
List<int> studentScores = new List<int>();
for (int scores = 0; scores < totalExams; scores++)
{
//Read and save scores of student in each subject
string score = string.Empty;
Console.WriteLine("Enter exam");
score = Console.ReadLine();
int intScore = Convert.ToInt32(score);
studentScores.Add(intScore);
}
//Add this in dictonary. Key as the `index` and
//value as the scores saved in `studentScores`
studentMap.Add(students, studentScores);
}
Upvotes: 1
Reputation: 29026
As per the clarifications that you provided in the comment, you should try something like this:
Dictionary<int, List<int>> studentMap = new Dictionary<int, List<int>>();
for (int student = 0; student < totalStudents; student++)
{
studentMap.Add(student, new List<int>());
for (int scores = 0; scores < totalExams; scores++)
{
string score = string.Empty;
Console.WriteLine("Enter exam");
score = Console.ReadLine();
int intScore = Convert.ToInt32(score);
studentMap[student].Add(intScore);
}
}
Upvotes: 0
Reputation: 23898
List<int> studentScores = new List<int>();
for (int students = 0; students < totalStudents; students++)
{
for (int scores = 0; scores < totalExams; scores++)
{
string score = string.Empty;
Console.WriteLine("Enter exam");
score = Console.ReadLine();
int intScore = Convert.ToInt32(score);
studentScores.Add(intScore);
}
}
should likely be:
for (int students = 0; students < totalStudents; students++)
{
List<int> studentScores = new List<int>();
for (int scores = 0; scores < totalExams; scores++)
{
string score = string.Empty;
Console.WriteLine("Enter exam");
score = Console.ReadLine();
int intScore = Convert.ToInt32(score);
studentScores.Add(intScore);
}
studentMap[students] = studentScores;
}
This will mean each entry in the Dictionary
will have a subset of the studentScores
data. This is different to your existing code, which lumps all of the studentScores
data in a single List
.
Upvotes: 1