In: Computer Science
Use Visual Basic Language
In this assignment,you will create the following data file in Notepad:
25
5
1
7
10
21
34
67
29
30
You will call it assignment4.dat and save it in c:\data\lastname\assignment4\. Where lastname is your last name.
The assignment will be a console application that will prompt the user for a number.
Ex. Console.Write(“Enter a number: ”)
And then read in the number
Ex. Number = Console.ReadLine()
You will then read the data file using a while statement. For each time you read a value from the data file you will compare it to the user input value.
Ex. User inputs the value “10”. The first value from the data file is “25”. 25 is greater than 10, so the output would look like this, “25 is greater than10”. The next value from the data file is “5”, so the output would be “5 is less than 10”.Also check for equal values as well.
You will also total the values from your data file and then average them at the end. Your total will be 229 and the average will be 22.9. The total variable should be a double and rounded to two decimal places. The results should be printed to the screen at the end of the program.
Required elements:
* Comments and documentation
* Data file
* While statement(read the data file)
* Totaling the contents of the data file
* Averaging the total
Variables needed:
* Number (input from the screen): Type Double
* Datanumber (input from the data file): Type Double
* Total (values totaled from data file): Type Double
* AverageData (Average from the total): Type Double
* Const variables of string type: For example:
Const author As String = "Jack"
Const doublelines As String = "=============="
//IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE COMMENT TO CLEAR DOUBTS
CODE:
using System;
using System.IO;
class MainClass {
public static void Main (string[] args) {
double totalSum = 0; // to get the total sum of the .dat file
contents
string line; // used to read the .dat file line by line
Console.WriteLine("\nEnter a number: "); //prompting to input
file
int number; // used to input the user value
int readValue; //used to convert the "line" into corresponding
integer
int counter = 0; //count the number of values read
number = Convert.ToInt32(Console.ReadLine()); //taking input
// Read the file and display it line by line.
// according to the question file must be saved in the directory
specified
// path in the string would be
// "C:\data\lastname\assignment4\assignment4.dat"
System.IO.StreamReader file = new
System.IO.StreamReader("assignment4.dat"); //opening file
reader
while((line = file.ReadLine()) != null) //while loop used
{
readValue = Convert.ToInt32(line); //converting string to int
if(readValue > number){ //if greater
Console.WriteLine(readValue+" is greater than "+number);
}
else if(readValue < number){ //if lesser
Console.WriteLine(readValue+" is less than "+number);
}
else{ //if equals
Console.WriteLine(readValue+" is equal to "+number);
}
totalSum += readValue; //adding the value
counter++; //counts the number of lines
}
file.Close(); //closing the StreamReader
double average = totalSum/counter; //getting average
Console.WriteLine("\n\nTotal is "+totalSum);
Console.WriteLine("Average is "+average+"\n");
}
}
CODE PREVIEW: