Data Analysis
Part of the Logic & Flow section of Coddy's C# journey — lesson 42 of 66.
Challenge
MediumNow 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:
CalculateStudentAverage(int[][] scoreGrid, int studentIndex): Calculates and returns the average score for a specific student. Return -1 if the studentIndex is invalid.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.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
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling8Data Analysis System
Data Collection SetupData Entry Logic3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety