Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using System; using System.Collections.Generic; using System.Linq; using System.

ID: 3659904 • Letter: U

Question

using System;

usingSystem.Collections.Generic;

using System.Linq;

using System.Text;

namespace CS201_Exam2B

{

/// <summary>

/// Complete the following polling application by adding code in the

/// areas labeled "TODO". Hint: From the Visual Studio Menu, select

/// "View" / "Task List". In the window that appears, change the dropdown from

/// "User Tasks" to "Comments" and you'll see all of the TODOs in one spot. :)

/// </summary>

class PollingApp

{

/// <summary>

/// Customized list of 5 topics.

/// TODO: Customize text from "Issuexx" to

/// real topics such as "open internet", "the economy", "war", "health care", or "education".

/// </summary>

static string[] topics = { "Issue01", "Issue02", "Issue03", "Issue04", "Issue05" };

const int NUM_TOPICS = 5;

const int MAX_RATING = 10;

/// <summary>

/// Create a polling application that asks users the importance of five issues,

/// displays a table of the number of ratings (1 through 10) each recieved and the

/// average score, then outputs the issue with the greatest number of points and

/// the issue with the lowest number of points.

/// </summary>

/// <param name="args"></param>

static void Main(string[] args)

{

// responses is the most important data structure -

// spend some time understanding what this does.

// It's a array - think one row for each of the five topics

// and a column for each rating value (1-10).

// Each element holds the number of times that score (e.g. 7)

// was assigned to that topic.

// Remember that C# array indexes always start at 0

// while the ratings (1-10) start at 1.

int[,] responses = new int[NUM_TOPICS, MAX_RATING];

// sentinel to decide when to exit loop

int choice = 1;

// TODO: add a while loop to allow the following two lines of code to repeat until

// the user types the sentinel value...

Console.WriteLine(); // add extra blank line

PollUser(responses);

// TODO: see if we should stop getting user input and end the while loop

// Call the display results method given the array with the rating values

DisplayResults(responses);

// TODO: Create a list of strings.

// TODO: Add each topic to the list of strings.

// Print the header for the topic list.

Console.WriteLine("Thank you for participating in this survey of the following issues:");

// TODO: Display the topic list

// Keep the console window open

Console.Read();

}

/// <summary>

/// Get ratings on topics from one user.

/// </summary>

/// <param name="responses"></param>

public static void PollUser(int[,] responses)

{

// For each issue....

for (int i = 0; i < NUM_TOPICS; ++i)

{

Console.WriteLine("On a scale of 1-{0}, how important is {1}?", MAX_RATING, topics[i]); // ask question

int rating; // rating user gave for this

do

{

Console.Write("> "); // display prompt

rating = Convert.ToInt32(Console.ReadLine());

}

while (rating < 1 || rating > MAX_RATING);

// increment the correct array element - the one that

// matches this issue, i and this rating value.

++responses[i, rating - 1];

}

}

/// <summary>

/// display polling results in tabular format

/// </summary>

/// <param name="responses"></param>

public static void DisplayResults(int[,] responses)

{

// display table header

Console.Write(" {0, -15}", "Topic");

for (int i = 1; i <= MAX_RATING; ++i)

{

Console.Write("{0, 4}", i);

}

Console.WriteLine("{0, 10}", "Average");

// display rating counts and averages for each topic

for (int i = 0; i < NUM_TOPICS; ++i)

{

Console.Write("{0, -15}", topics[i]); // display topic

// display number of times topic was given this score

for (int j = 0; j < MAX_RATING; ++j)

Console.Write("{0, 4}", responses[i, j]);

// display average rating for this topic

Console.WriteLine("{0, 10:F1}", CalculateAverage(responses, i));

} // end for

Console.WriteLine(); // add blank line

// TODO: Call the method that displays the highest rated issue and its score.

// TODO: Call the method that displays the lowest rated issue and its score.

}

/// <summary>

/// Use a for loop to count how many votes an issue received

/// and return the average (CountPoints for the given row and votes array

/// divided by the count of votes received for the given issue)

/// </summary>

/// <param name="votes"></param>

/// <param name="row"></param>

/// <returns></returns>

public static double CalculateAverage(int[,] votes, int row)

{

// intialize the variables

int count = 0; // total number of votes

double average = 0; //

// TODO: Use a for loop to count up the number of votes received

// TODO: Divide the total points by the count of votes to calculate the average

// be sure you're not using integer division!

// return average

return average;

}

/// <summary>

/// Display the issue with the most points

/// </summary>

/// <param name="responses">the array of reponses</param>

public static void DisplayHighest(int[,] responses)

{

// initialize result variables

int max = CountPoints(responses, 0); // maximum number of points

int maxIndex = 0; // index of issue with maximum number of points

for (int i = 1; i < NUM_TOPICS; ++i)

{

if (CountPoints(responses, i) > max)

{

// larger point count found, update maximum value and index

max = CountPoints(responses, i);

maxIndex = i;

}

}

Console.WriteLine("Highest points: {0} ({1}) ", topics[maxIndex], max);

}

/// <summary>

/// display topic with fewest points

/// </summary>

/// <param name="responses">the array of responses</param>

public static void DisplayLowest(int[,] responses)

{

// initialize result variables

int min = CountPoints(responses, 0); // minimum number of points

int minIndex = 0; // index of issue with minimum number of points

// TODO: iterate through the scores - if a smaller point count is found,

// then update the minimum point value and the index of the row with the

// lowest number of points.

//TODO: Modify the line below so it will show the issue with the

// lowest number of points - and the total number of points the issue received.

// Hint: use topics[minIndex] and the minimum total number of points (min).

Console.WriteLine("Lowest points:");

}

/// <summary>

/// Calculate total number of points for a given topic.

/// A handy resusable method that can be used in the

/// other methods.

/// </summary>

/// <param name="votes">the array of responses</param>

/// <param name="row">the index associated with this issue</param>

/// <returns></returns>

public static int CountPoints(int[,] votes, int row)

{

int sum = 0; // initialize the total number of points to zero

// use a for loop to sum them up

for (int i = 0; i < MAX_RATING; ++i)

{

sum += votes[row, i] * (i + 1); // add the weighted count

}

return sum;

}

}

}

/* Expected output:

*

*

On a scale of 1-10, how important is Issue01?

> 2

On a scale of 1-10, how important is Issue02?

> 3

On a scale of 1-10, how important is Issue03?

> 4

On a scale of 1-10, how important is Issue04?

> 5

On a scale of 1-10, how important is Issue05?

> 6

Enter more data? (1=yes, 0=no): 1

On a scale of 1-10, how important is Issue01?

> 9

On a scale of 1-10, how important is Issue02?

> 8

On a scale of 1-10, how important is Issue03?

> 7

On a scale of 1-10, how important is Issue04?

> 4

On a scale of 1-10, how important is Issue05?

> 1

Enter more data? (1=yes, 0=no): 0

Topic 1 2 3 4 5 6 7 8 9 10 Average

Issue01 0 1 0 0 0 0 0 0 1 0 0.0

Issue02 0 0 1 0 0 0 0 1 0 0 0.0

Issue03 0 0 0 1 0 0 1 0 0 0 0.0

Issue04 0 0 0 1 1 0 0 0 0 0 0.0

Issue05 1 0 0 0 0 1 0 0 0 0 0.0

Highest points: Issue01 (11)

Lowest points: Issue05 (7)

Thank you for participating in this survey of the following issues:

Issue01

Issue02

Issue03

Issue04

Issue05

*/

Explanation / Answer

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CS201_Exam2B { /// <summary> /// Complete the following polling application by adding code in the /// areas labeled "TODO". Hint: From the Visual Studio Menu, select /// "View" / "Task List". In the window that appears, change the dropdown from /// "User Tasks" to "Comments" and you'll see all of the TODOs in one spot. :) /// </summary> class PollingApp { /// <summary> /// Customized list of 5 topics. /// TODO: Customize text from "Issuexx" to /// real topics such as "open internet", "the economy", "war", "health care", or "education". /// </summary> static string[] topics = { "Open Source", "Linux", "Windows", "Apple IOS", "Andriod" }; const int NUM_TOPICS = 5; const int MAX_RATING = 10; /// <summary> /// Create a polling application that asks users the importance of five issues, /// displays a table of the number of ratings (1 through 10) each recieved and the /// average score, then outputs the issue with the greatest number of points and /// the issue with the lowest number of points. /// </summary> /// <param name="args"></param> static void Main(string[] args) { // responses is the most important data structure - // spend some time understanding what this does. // It's a array - think one row for each of the five topics // and a column for each rating value (1-10). // Each element holds the number of times that score (e.g. 7) // was assigned to that topic. // Remember that C# array indexes always start at 0 // while the ratings (1-10) start at 1. int[,] responses = new int[NUM_TOPICS, MAX_RATING]; // sentinel to decide when to exit loop int choice = 1; // TODO: add a while loop to allow the following two lines of code to repeat until // the user types the sentinel value... while (true) { Console.WriteLine(); // add extra blank line PollUser(responses); Console.Write("Enter more data? (0=yes, 1=no):"); if (Console.ReadLine().Equals("" + choice)) { break; } } // TODO: see if we should stop getting user input and end the while loop // Call the display results method given the array with the rating values DisplayResults(responses);    // TODO: Create a list of strings. List<string> _lstString = new List<string>(); // TODO: Add each topic to the list of strings. for (int i = 0; i < NUM_TOPICS; i++) { _lstString.Add(topics[i]); } // Print the header for the topic list. Console.WriteLine("Thank you for participating in this survey of the following issues:"); // TODO: Display the topic list Console.WriteLine("List of Topics "); for (int i = 0; i < NUM_TOPICS; i++) { Console.WriteLine(" " + _lstString[i]); } // Keep the console window open Console.Read(); } /// <summary> /// Get ratings on topics from one user. /// </summary> /// <param name="responses"></param> public static void PollUser(int[,] responses) { // For each issue.... for (int i = 0; i < NUM_TOPICS; ++i) { Console.WriteLine("On a scale of 1-{0}, how important is {1}?", MAX_RATING, topics[i]); // ask question int rating; // rating user gave for this do { Console.Write("> "); // display prompt rating = Convert.ToInt32(Console.ReadLine()); } while (rating < 1 || rating > MAX_RATING); // increment the correct array element - the one that // matches this issue, i and this rating value. ++responses[i, rating - 1]; } } /// <summary> /// display polling results in tabular format /// </summary> /// <param name="responses"></param> public static void DisplayResults(int[,] responses) { // display table header Console.Write(" {0, -15}", "Topic"); for (int i = 1; i <= MAX_RATING; ++i) { Console.Write("{0, 4}", i); } Console.WriteLine("{0, 10}", "Average"); // display rating counts and averages for each topic for (int i = 0; i < NUM_TOPICS; ++i) { Console.Write("{0, -15}", topics[i]); // display topic // display number of times topic was given this score for (int j = 0; j < MAX_RATING; ++j) Console.Write("{0, 4}", responses[i, j]); // display average rating for this topic Console.WriteLine("{0, 10:F1}", CalculateAverage(responses, i)); } // end for Console.WriteLine(); // add blank line // TODO: Call the method that displays the highest rated issue and its score. DisplayHighest(responses); // TODO: Call the method that displays the lowest rated issue and its score. DisplayLowest(responses); } /// <summary> /// Use a for loop to count how many votes an issue received /// and return the average (CountPoints for the given row and votes array /// divided by the count of votes received for the given issue) /// </summary> /// <param name="votes"></param> /// <param name="row"></param> /// <returns></returns> public static double CalculateAverage(int[,] votes, int row) { // intialize the variables int count = 0; // total number of votes double average = 0; // // TODO: Use a for loop to count up the number of votes received int totalpoints = 0; for (int i = 0; i < 10; i++) { count = count + votes[row, i]; totalpoints = totalpoints + (votes[row, i] * (i+1)); } // TODO: Divide the total points by the count of votes to calculate the average average = (double)totalpoints / count; // be sure you're not using integer division! // return average return average; } /// <summary> /// Display the issue with the most points /// </summary> /// <param name="responses">the array of reponses</param> public static void DisplayHighest(int[,] responses) { // initialize result variables int max = CountPoints(responses, 0); // maximum number of points int maxIndex = 0; // index of issue with maximum number of points for (int i = 1; i < NUM_TOPICS; ++i) { if (CountPoints(responses, i) > max) { // larger point count found, update maximum value and index max = CountPoints(responses, i); maxIndex = i; } } Console.WriteLine("Highest points: {0} ({1}) ", topics[maxIndex], max); } /// <summary> /// display topic with fewest points /// </summary> /// <param name="responses">the array of responses</param> public static void DisplayLowest(int[,] responses) { // initialize result variables int min = CountPoints(responses, 0); // minimum number of points int minIndex = 0; // index of issue with minimum number of points // TODO: iterate through the scores - if a smaller point count is found, // then update the minimum point value and the index of the row with the // lowest number of points. for (int i = 1; i < NUM_TOPICS; ++i) { if (CountPoints(responses, i) < min) { // larger point count found, update maximum value and index min = CountPoints(responses, i); minIndex = i; } } //TODO: Modify the line below so it will show the issue with the // lowest number of points - and the total number of points the issue received. // Hint: use topics[minIndex] and the minimum total number of points (min). Console.WriteLine("Lowest points: {0} ({1}) ", topics[minIndex], min); } /// <summary> /// Calculate total number of points for a given topic. /// A handy resusable method that can be used in the /// other methods. /// </summary> /// <param name="votes">the array of responses</param> /// <param name="row">the index associated with this issue</param> /// <returns></returns> public static int CountPoints(int[,] votes, int row) { int sum = 0; // initialize the total number of points to zero // use a for loop to sum them up for (int i = 0; i < MAX_RATING; ++i) { sum += votes[row, i] * (i + 1); // add the weighted count } return sum; } } }