0

here's the problem:

I have student Class:

class Student
{
    public int Mark1 { get; set; }
    public int Mark2 { get; set; }
    public int Mark3 { get; set; }
    public int Mark4 { get; set; }
    public int Mark5 { get; set; }
    public Student(int mark1, int mark2, int mark3, int mark4, int mark5)
    {
        Mark1 = mark1;
        Mark2 = mark2;
        Mark3 = mark3;
        Mark4 = mark4;
        Mark5 = mark5;
    }
}

And I need to put these marks of each student into an int array. Here are my attempts:

class Program
{
    static void Main(string[] args)
    {
    }
    public static void MarksAbove5(College ListOfStudents, out College FilteredStudents)
    {
        FilteredStudents = new College(50);
        int[] Marks = new int[ListOfStudents.Count];
        for (int i = 0; i < ListOfStudents.Count; i++)
        {
         Marks[i] = (ListOfStudents.GetStudent(i).Mark1, ListOfStudents.GetStudent(i).Mark2, ListOfStudents.GetStudent(i).Mark3, ListOfStudents.GetStudent(i).Mark4, ListOfStudents.GetStudent(i).Mark5);
        } 
    }
}

And I get an error stating "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement". How do I put these ints into an int array correctly? Thanks in advance!

Mr Moil
  • 33
  • 6
  • Are you actually wanting an array of arrays or are you trying to flatten the marks of all students into a single array? – npearson Jan 24 '18 at 18:49
  • I want each student to have his/her own array of their own marks. For example, John's marks: Mark1: 5 Mark2: 6 etc; Johns Mark Array: 5,6, etc I will be needing the mark array to check if all of the marks in that mark array are above 5 – Mr Moil Jan 24 '18 at 18:51
  • If that's the case you need a 2-dimensional array. int[][] marks = new int[ListOfStudents.Count][] and see the array initialization link. – npearson Jan 24 '18 at 18:57
  • Or since you know the number of marks int[,] marks = new int[ListOfStudents.Count, 5] – npearson Jan 24 '18 at 18:58
  • this is not a dup of the initialization question. Its simply a question about loading data into an array. – pm100 Jan 24 '18 at 18:59
  • @Servy I agree this should not have been marked a dup – npearson Jan 24 '18 at 19:01
  • @npearson, yes I do know exactly how many marks will be there. There will be 5 marks for each person. So it should look like: int[,] marks = new int[ListOfStudents.Count, 5]? where does the "i" come in? P.S. I am sorry, I am a beginner at c# and tomorrow is the programming exam, and I feel desperate :/ – Mr Moil Jan 24 '18 at 19:03
  • 1
    With the int[,] declaration you would need to add each mark individually. mark[i, 1] = ListOfStudents.GetStudent(i).Mark1; – npearson Jan 24 '18 at 19:11
  • 1
    Using the first declaration, it's exactly what you have but with the correct initialization using curly braces. marks[i] = new int[] {ListOfStudents.GetStudent(i).Mark1, ...} – npearson Jan 24 '18 at 19:16
  • I did what you said in the last comment, but I get another error: "Cannot implicitly convert type 'int[]' to 'int'" – Mr Moil Jan 24 '18 at 19:25

0 Answers0