Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to change foreach loop to for loop, two codes are in bold using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using

I want to change foreach loop to for loop, two codes are in bold

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO;

namespace Test_Score_List { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // The ReadScores method reads the scores from the // TestScores.txt file into the scoresList parameter. private void ReadScores(List scoresList) { try { // Open the TestScores.txt file. StreamReader inputFile = File.OpenText("TestScores.txt"); // Read the scores into the list.

while (!inputFile.EndOfStream) { scoresList.Add(int.Parse(inputFile.ReadLine())); } // Close the file. inputFile.Close(); } catch (Exception ex) { // Display an error message. MessageBox.Show(ex.Message); } }

// The DisplayScores method displays the contents of the // scoresList parameter in the ListBox control. private void DisplayScores(List scoresList) {

foreach (int score in scoresList) {

testScoresListBox.Items.Add(score); }

} // The Average method returns the average of the values // in the scoresList parameter. private double Average(List scoresList) { int total = 0; // Accumulator double average; // To hold the average // Calculate the total of the scores. foreach (int score in scoresList) { total += score; } // Calculate the average of the scores. average = (double)total / scoresList.Count; // Return the average. return average; } // The AboveAverage method returns the number of // above average scores in scoresList private int AboveAverage(List scoresList) { int numAbove = 0; // Accumulator // Get the average score. double avg = Average(scoresList); // Count the number of above average scores. foreach (int score in scoresList) { if (score > avg) { numAbove++;

} } // Return the number of aboveaverage scores. return numAbove; } // The BelowAverage method returns the number of // below average scores in scoresList private int BelowAverage(List scoresList) { int numBelow = 0; //Accumulator // Get the average score. double avg = Average(scoresList); // Count the number of below average scores. foreach (int score in scoresList) { if (score < avg) { numBelow++; } } // Return the number of below average scores. return numBelow; }

private void getScoresButton_Click(object sender, EventArgs e) { double averageScore; // To hold the average score int numAboveAverage; // Number of above average scores int numBelowAverage; // Number of below average scores // Create a List to hold the scores. List scoresList = new List(); // Read the scores from the file into the List. ReadScores(scoresList); // Display the scores. DisplayScores(scoresList); // Display the average score. averageScore = Average(scoresList); averageLabel.Text = averageScore.ToString("n1"); // Display the number of above average scores. numAboveAverage = AboveAverage(scoresList); aboveAverageLabel.Text = numAboveAverage.ToString(); // Display the number of below average scores. numBelowAverage = BelowAverage(scoresList); belowAverageLabel.Text = numBelowAverage.ToString(); }

private void exitButton_Click(object sender, EventArgs e) { // Close the form. this.Close(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions