[C# Visual Studio] Jagged Array of Exam Scores Dr. Hunter teaches three sections
ID: 3774487 • Letter: #
Question
[C# Visual Studio]
Jagged Array of Exam Scores
Dr. Hunter teaches three sections of her Intro to Computer Science class. She has
12 students in section 1, 8 students in section 2, and 10 students in section 3. In the
Chap07 folder of the Student Sample Programs, you will find the following files:
• Section1.txt—This file contains the final exam scores for each student in section 1.
(There are 12 integer scores in the file.)
• Section2.txt—This file contains the final exam scores for each student in section 2.
(There are 8 integer scores in the file.)
• Section3.txt—This file contains the final exam scores for each student in section 3.
(There are 10 integer scores in the file.)
Create an application that reads these three files and stores their contents in a jagged
array. The array’s first row should hold the exam scores for the students in section
1, the second row should hold the exam scores for the students in section 2, and the
third row should hold the exam scores for the students in section 3.
The application should display each section’s exam scores in a separate ListBox
control and then use the jagged array to determine the following:
• The average exam score for each individual section
• The average exam score for all the students in the three sections
• The highest exam score among all three sections and the section number in
which that score was found
• The lowest exam score among all three sections and the section number in
which that score was found
Explanation / Answer
/**
C sharp that application that reads three text files of scores
* and finds the average score for three sections.
* finds the highest and lowest scores in all three sections.
*/
//jaggedarray.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace JaggedConsoleApplication
{
class Program
{
static void Main(string[] args)
{
// Declare local jagged array with 3 rows.
int[][] sections = new int[3][];
// Create a new array in the jagged array of size 12
sections[0] = new int[12];
// Create a new array in the jagged array of size 8
sections[1] = new int[8];
// Create a new array in the jagged array of size 10
sections[2] = new int[10];
string fileContent = File.ReadAllText("Section1.txt");
string[] integerStrings =
fileContent.Split(new char[] { ' ', ' ', ' ', ' ' }, StringSplitOptions.RemoveEmptyEntries);
double section1total = 0;
double section2total = 0;
double section3total = 0;
for (int n = 0; n < sections[0].Length; n++)
{
sections[0][n] = int.Parse(integerStrings[n]);
section1total += sections[0][n];
}
fileContent = File.ReadAllText("section2.txt");
integerStrings =
fileContent.Split(new char[] { ' ', ' ', ' ', ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int n = 0; n < sections[1].Length; n++)
{
sections[1][n] = int.Parse(integerStrings[n]);
section2total += sections[1][n];
}
fileContent = File.ReadAllText("Section3.txt");
integerStrings =
fileContent.Split(new char[] { ' ', ' ', ' ', ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int n = 0; n < sections[2].Length; n++)
{
sections[2][n] = int.Parse(integerStrings[n]);
section3total += sections[2][n];
}
Console.WriteLine("Average exam score for section1 {0}", section1total / sections[0].Length);
Console.WriteLine("Average exam score for section1 {0}", section2total / sections[1].Length);
Console.WriteLine("Average exam score for section1 {0}", section3total / sections[2].Length);
double total = (section1total / sections[0].Length
+ section2total / sections[1].Length
+ section2total / sections[1].Length) / 3.0;
Console.WriteLine("Average exam score for 3 section1 {0}", total);
int highest = Int32.MinValue;
int highesSection = -1;
// find highest element and its section in the jagged array.
for (int i = 0; i < sections.Length; i++)
{
int[] innerArray = sections[i];
highesSection = i;
for (int a = 0; a < innerArray.Length; a++)
{
if (highest < innerArray[a])
highest = innerArray[a];
}
}
Console.WriteLine("Highest score {0} in section {1}", highest, highesSection);
int lowest = Int32.MaxValue;
int lowestSection = -1;
// find lowest element and its section in the jagged array.
for (int i = 0; i < sections.Length; i++)
{
int[] innerArray = sections[i];
lowestSection = i;
for (int a = 0; a < innerArray.Length; a++)
{
if (innerArray[a]<lowest)
lowest = innerArray[a];
}
}
Console.WriteLine("Lowest score {0} in section {1}", lowest, lowestSection);
Console.ReadKey();
}
}
}
-------------------------------
Section1.txt
87 93 72 98 65 70 89 78 77 66 92 72
section2.txt
71 98 93 79 84 90 88 91
section3.txt
71 98 93 79 84 90 88 91
-------------------------------------------------------------------------
Average exam score for section1 79.9166666666667
Average exam score for section1 86.75
Average exam score for section1 73
Average exam score for 3 section1 84.4722222222222
Highest score 98 in section 2
Lowest score 56 in section 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.