package Arrays2D;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/*
==========================2026 (c) Basit Qureshi ===========================

CS102 Programming II
Dept. of Computer Sc, Prince Sultan University
August 8, 2026
https://www.ieeepsu.org/basit/cs102/

//Exercise 3
Write a program that grades the scores of a multiple choice exam.
There are 10 questions in the exam. The possible answer for each question is
A, B, C, D. 

Ask the user to type 10 characters. Each character must be A, B, C or D. This 
is the correct answer KEY.

Read a text file "E3.txt" that gives the answers for students. Show
the total score for each student. Also show the average score, highest and
the lowest scores.

*/


public class EXArray2D3 {
    public static void main(String [] s)throws FileNotFoundException
    {
        //TO DO: Make a 2D array of type char
        char [][] Scores = new char [10][10];
        
        //Ask user to input the correct answer and store it in KEY.
        char [] KEY = {'A', 'B', 'C', 'D', 'A', 
                       'B', 'C', 'D', 'A', 'D'};
        
        //Open the file "E3.txt" and read the contents in the array
        File file = new File("src//Arrays2D//E3.txt");
        Scanner FIn = new Scanner(file);
        
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                Scores[i][j] = FIn.next().charAt(0);
            }
        }
        
        //call getScore method. Compute the score for each row in Scores.
        int [] grades = new int[10];
        for(int i=0; i<10; i++)
        {
            int score =0;
            grades[i] = getScore(Scores, KEY, i);
            System.out.println("student#"+ i+": "+grades[i]);
        }
        //display the average, max and min scores.
        System.out.println("Average: "+ avgGrade(grades));
        System.out.println("Max: "+ maxGrade(grades));
        System.out.println("Min: "+ minGrade(grades));
    }
    
    
    public static int getScore(char [][] C, char [] KEY, int row)
    {
        //grade the score for the student in row
        int sum = 0;
        //match each column value in C[row][col] with KEY[col]. If it is
        //identical, student eachs 1 pt.
        
        for(int i=0;i<10;i++)
        {
            if(C[row][i] == KEY[i])
                sum++;
        }
        //return the sum
        return sum;
    }    
    
    public static int maxGrade(int [] G){
        int max = 0;
        for(int i=0;i<10;i++)
        {
            if(max < G[i])
                max = G[i];
        }
        return max;
    }

    public static int minGrade(int [] G){
        int min = 0;
        for(int i=0;i<10;i++)
        {
            if(min > G[i])
                min = G[i];
        }
        return min;
    }  
    
    public static double avgGrade(int [] G)
    {
        int sum = 0;
        for(int i=0;i<10;i++)
        {
            sum = sum + G[i];
        }
        return sum/10;
    }    
}
