Menu
Coddy logo textTech

Data Analysis

Part of the Logic & Flow section of Coddy's C# journey — lesson 42 of 66.

challenge icon

Challenge

Medium

Now that we can collect and update student data, let's implement the analysis functionality to gain insights from the scores. Create a class called DataAnalyzer with these methods:

  1. CalculateStudentAverage(int[][] scoreGrid, int studentIndex): Calculates and returns the average score for a specific student. Return -1 if the studentIndex is invalid.
  2. CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex): Calculates and returns the average score for a specific assignment across all students. Return -1 if the assignmentIndex is invalid.
  3. FindHighestScore(int[][] scoreGrid): Returns an array of three integers representing the highest score and its location: [studentIndex, assignmentIndex, score]. If multiple entries have the same highest score, return the first one found.

Note: When calculating averages, consider only valid scores (0-100). Ignore ungraded assignments (-1).

Try it yourself

using System; // Don't delete this line
public class DataAnalyzer
{
    public static double CalculateStudentAverage(int[][] scoreGrid, int studentIndex)
    {
        // Write your code here
        
    }
    
    public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
    {
        // Write your code here
        
    }
    
    public static int[] FindHighestScore(int[][] scoreGrid)
    {
        // Write your code here
        
    }
}

// Required for testing - do not modify
public class DataCollector
{
    public static int[][] CreateScoreGrid(int students, int assignments)
    {
        int[][] scoreGrid = new int[students][];
        for (int i = 0; i < students; i++)
        {
            scoreGrid[i] = new int[assignments];
        }
        return scoreGrid;
    }
    
    public static bool ValidateScore(int score)
    {
        return score >= 0 && score <= 100;
    }
    
    public static int[][] PopulateWithDefaultValues(int[][] scoreGrid)
    {
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            for (int j = 0; j < scoreGrid[i].Length; j++)
            {
                scoreGrid[i][j] = -1;
            }
        }
        return scoreGrid;
    }
}

public class DataEntry
{
    public static int SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score)
    {
        // Check for out of bounds indices
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length || 
            assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
        {
            return -1;
        }
        
        // Validate the score
        if (!DataCollector.ValidateScore(score))
        {
            return -2;
        }
        
        // Set the score
        scoreGrid[studentIndex][assignmentIndex] = score;
        return 0;
    }
    
    public static int UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score)
    {
        int successCount = 0;
        
        for (int i = 0; i < studentIndices.Length; i++)
        {
            int result = SetStudentScore(scoreGrid, studentIndices[i], assignmentIndex, score);
            if (result == 0)
            {
                successCount++;
            }
        }
        
        return successCount;
    }
}

All lessons in Logic & Flow