Click here to Skip to main content
15,884,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am currently stuck on an assignment. I have just learnt array and I am using Visual Basic console mode. In the assignment, I have declared 5 one-dimension arrays and they all have 30 variables: names, test1, test2, test3, total. They are names of students and their scores for tests and the total score that they have achieved. Names, test1, test2 and test3 can be input. The problem is that I have to calculate the total score and then output it with the corresponding student name. How do I add values from different arrays accordingly so that it corresponds with the student name and output the student name?

Example:
John is one of the students and his score is stored in 3 different arrays. Now I need to add 3 variables in different arrays to get his total score, but not just for John, but for 30 students in total. You can assume that the variable in every array corresponds to the "names" array. So if John is the first variable of the "names" array, then the first variable of the "test1", "test2", "test3" are his score and need to be added together. The point is I have to do this for 30 students

What I have tried:

I am not sure what I can do with arrays, so I have been searching for examples or things that I could do with arrays. I think I am doing things similar to a parallel array.
Posted
Updated 2-Sep-17 23:47pm
v3

If I understand correctly, you want to have the sum of all elements in the array. Use LINQ's extension method Sum[^] :
VB.NET
Dim sum As Integer = yourArray.Sum()
 
Share this answer
 
Comments
Member 13390968 3-Sep-17 5:38am    
Actually, no. Maybe my explanation isn't clear enough. So first, let me make an example.
John is one of the students and his score is stored in 3 different arrays. Now I need to add 3 variables in different arrays to get his total score, but not just for John, but for 30 students in total. You can assume that the first variable in every array corresponds to the "names" array. So if John is the first variable of the "names" array, then the first variable of the "test1", "test2", "test3" are his score and need to be added together. The point is I have to do this for 30 students.
Thomas Daniels 3-Sep-17 5:50am    
Ah, I see now, I missed that part when originally reading the question. I believe Graeme_Grant's answer is what you're looking for then.
Graeme_Grant 3-Sep-17 6:06am    
I almost missed it too...
Quote:
have to calculate the total score and then output it with the corresponding student name

If I read and understood your question correctly, This is what you're looking for:
C#
class Student
{
    public string Name { get; set; }
    public float Score { get; set; }
}
private static void Main()
{
    var List1 = new List<Student>
    {
        new Student { Name="Student 1", Score = 78.1f },
        new Student { Name="Student 2", Score = 88f }
    };

    var List2 = new List<Student>
    {
        new Student { Name="Student 3", Score = 52.7f },
        new Student { Name="Student 1", Score = 48f }
    };

    var results = List1.Concat(List2)
                       .GroupBy(x => x.Name)
                       .Select(x => new Student 
                                    {
                                        Name = x.Key,
                                        Score = x.Sum(y => y.Score)
                                    });

    foreach (var student in results)
    {
        Console.WriteLine($"Student: {student.Name}  > Score: {student.Score:N2}");
    }
}

Which outputs:
Student: Student 1  > Score: 126.10
Student: Student 2  > Score: 88.00
Student: Student 3  > Score: 52.70

UPDATE: My bad, Here is the VB.NET version:
VB
Class Student
    Public Property Name() As String
    Public Property Score() As Single
End Class

Module Module1

    Sub Main()

        Dim List1 = New List(Of Student)() From {
            New Student() With {
                .Name = "Student 1",
                .Score = 78.1F
            },
            New Student() With {
                .Name = "Student 2",
                .Score = 88.0F
            }
        }

        Dim List2 = New List(Of Student)() From {
            New Student() With {
                .Name = "Student 3",
                .Score = 52.7F
            },
            New Student() With {
                .Name = "Student 1",
                .Score = 48.0F
            }
        }

        Dim results = List1.Concat(List2) _
                           .GroupBy(Function(x) x.Name) _
                           .Select(Function(x) New Student() With {
            .Name = x.Key,
            .Score = x.Sum(Function(y) y.Score)
        })

        For Each student In results
            Console.WriteLine("Student: {student.Name}  > Score: {student.Score:N2}")
        Next

    End Sub

End Module
 
Share this answer
 
v3
Comments
Member 13390968 3-Sep-17 5:46am    
I'm sorry. I am not familiar with any other programming language other than Visual Basic console application/mode.
Graeme_Grant 3-Sep-17 5:48am    
VB.NET version added. :)

[edit] Have not heard anything. Working now?
Thomas Daniels 3-Sep-17 5:50am    
+5
Graeme_Grant 3-Sep-17 5:58am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900