0

I'm currently using ASP.NET Core MVC for my application and I'm not sure how to approach the problem.

Say I have two array of double type

double[] questionOne = {1,4,5,2,4};
double[] questionTwo = {3,2,4,5,2};

I want to use a method to concatenate them together and store them in possibly a dictionary such that the stored value is something like

stud1 | 1,3
stud2 | 4,2
stud3 | 5,4
stud4 | 2,5
stud5 | 4,2

so I can retrieve the values and calculate the total value for each student.


I do not know how many questions there will be.
Neither do I know how many students there will be.
I'll be able to loop for these values later on but for now, its a fixed value.

Should I be storing the values in a dictionary, list or a tuple?

Thereafter, how can I call the method such that I can return the value and display in my "View"?
I don't need the values to be in a table, a simple raw output to check the algorithm idea will do if possible.

2
  • 1
    Maybe a dictionary of names and tuples? Commented Oct 12, 2017 at 11:30
  • Something like a Dictionary<string, Tuple<int, int>> ? Commented Oct 12, 2017 at 11:30

3 Answers 3

1

Since .Net 4.7 You can use this code:

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        double[] questionOne = {1, 4, 5, 2, 4};
        double[] questionTwo = {3, 2, 4, 5, 2};
        var combined = questionOne.Zip(questionTwo, (q1, q2) => (q1, q2)).ToList();
        Console.WriteLine(combined);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for replying! I'm not using console for my application. Do you happen to know how do I pass it to my view? I've tried using ViewData but its not working
1

You can use LINQ:

List<Tuple<double, double>> tuples =
    questionOne.Zip(questionTwo, (one, two) => Tuple.Create(one, two)).ToList();

That combines the number arrays. You can do the same for the students:

string[] students = new string[] {"stud1", "stud2", "stud3", "stud4", "stud5"};
Dictionary<string, Tuple<double, double>> result = students
    .Zip(tuples, (student, tuple) => new { student, tuple })
    .ToDictionary(entry => entry.student, entry => entry.tuple);

You can check out the result here.

1 Comment

Thanks for replying! This method would be great if I knew the number of students and tests I have to loop through
0

You could use this structure:

Dictionary<string, string[]> myDictionary= new Dictionary<string, string[]>();

then you just need a algorithm which is adding the content like:

for(int i=0; i<array1.Length; i++) {
    String[] data = new String[2];
    data[0] = array1[i];
    data[1] = array1[i];
    myDictionary.Add("student"+i, data);
}

Comments

Your Answer

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.