1. Use Notepad to place the following values in a text file: 86, 97, 144, 26. To
ID: 3557514 • Letter: 1
Question
1. Use Notepad to place the following values in a text file: 86, 97, 144, 26. To simplify the problem, the values can each be placed on separate lines. Write a C# program to retrieve the values from the text file and print the average of the values, formatted with two decimal places. Please complete the assignmet as actual C# applications so yo can compile ad run them to see if you have any errors.
2. Write an application that retrieves a student name and three scores per line from a text file. Process the values by calculating the averae of the scores per student. Write the name and average to a different text file. Test your application with a minimum of eight records in the original file. Please complete the assignment in C# applicaions so you compile and run the to see if you have any errors.
Explanation / Answer
1) how to read line by line
2) Something along the following lines would do it. But you need to create Open, Save, and Quit buttons with the right names (shown below) and you need to create a couple of labels, as well (also named below prefixed with the word "text".) You will also need to include an OpenFileDialog and a SaveFileDialog, too. That's about it, though. There's no error checking to speak of (values that aren't really numbers in the file, for example, may cause an exception.) But what can you expect?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace StudentAvg
{
public partial class StudentAvgs : Form
{
public StudentAvgs()
{
InitializeComponent();
}
private void buttonOpenStudentFile_Click(object sender, EventArgs e)
{
DialogResult r = openStudentFileDialog.ShowDialog();
if (r != DialogResult.OK) return;
textStudentFilename.Text = openStudentFileDialog.FileName;
if (!File.Exists(textStudentFilename.Text))
{
MessageBox.Show("Somehow you selected a file that doesn't exist.");
textStudentFilename.Text = "";
return;
}
}
private void StudentAvgs_Load(object sender, EventArgs e)
{
textStudentFilename.Text = "";
}
private void buttonSaveStudentFile_Click(object sender, EventArgs e)
{
if (textStudentFilename.Text == "")
{
MessageBox.Show("Must first select a valid Student file.");
return;
}
if (!File.Exists(textStudentFilename.Text))
{
MessageBox.Show(
"Student file was deleted or has gone missing since it was selected."
);
textStudentFilename.Text = "";
return;
}
DialogResult r = saveStudentFileDialog.ShowDialog();
if (r != DialogResult.OK) return;
if (File.Exists( saveStudentFileDialog.FileName ))
{
if (MessageBox.Show("Are you sure you want to replace that file?",
"File Already Exists", MessageBoxButtons.OKCancel) ==
DialogResult.Cancel)
return;
File.Delete( saveStudentFileDialog.FileName );
}
string[] lines = File.ReadAllLines( textStudentFilename.Text );
for (int i = 0; i < lines.Length; ++i )
{
string[] words = lines[i].Split(new char[] { ' ', ',', ' ' });
if (words.Length > 1)
{
double sum = 0.0;
for (int j = 1; j < words.Length; ++j)
sum += Convert.ToDouble(words[j]);
double avg = sum / (words.Length - 1);
lines[i] = words[0] + " " + Convert.ToString(avg);
}
}
File.WriteAllLines( saveStudentFileDialog.FileName, lines );
}
private void buttonQuit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.