C# Cognizant Handson Solutions

Quiz Competition Report Handson Solution Cognizant

IQA, an International Quizzing Association conducted a quiz competition where several teams participated.

The competition consists of multiple rounds and each round carries a maximum point of 500 and minimum of 0.

IQA wants to take a report on the number of rounds participated by each team and their scores in each round.

 The report should contain number of times each team participated and their scores in each attempt.  Finally, it should display all the teams with their scores and its total.

 How can you help them to generate the report using jagged arrays in C#. (Hint: The number of attempts taken by each team varies.)

From the Main method get input from the user and call the below method to return the results and display the result.public static String GetTotalScore(int[][] array)   //Method should accept a jagged array and return the result as given in the sample output.

Sample Input:

Enter the number of teams:

2

No.of attempts for team 1:

2

No.of attempts for team 2:

3

Enter the score for team 1:

100

120

Enter the score for team 2:

200

150

150

Sample Output:

Team 1 Total Score is 220 . Team 2 Total Score is 500

JaggedArray.cs

using System;

namespace JaggedArray
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter the number of teams:");
            int teams = Convert.ToInt32(Console.ReadLine());

            int[][] teamsArray = new int[teams][];

            for (int i = 0; i < teams; i++)
            {
                Console.WriteLine("No.of attempts for team {0}", (i + 1));
                int attempt = Convert.ToInt32(Console.ReadLine());
                teamsArray[i] = new int[attempt];
            }

            for (int i = 0; i < teams; i++)
            {
                Console.WriteLine("Enter the score for team {0}", (i + 1));
                for (int j = 0; j < teamsArray[i].Length; j++)
                    teamsArray[i][j] = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine(GetTotalScore(teamsArray));
        }

        public static String GetTotalScore(int[][] array)
        {
            String res = "";

            for (int i = 0; i < array.Length; i++)
            {
                int sum = 0;
                for (int j = 0; j < array[i].Length; j++)
                {
                    sum += array[i][j];
                    res += "Team " + (i + 1) + " Total Score is " + sum + ". ";
                }
            }
            return res;
        }
    }
}

Similar Posts