Question
in c++ Im a little confused do i place the numbers in the program or do create a seaprate file and connect them fivenumbers.txt file
in c++
Im a little confused do i place the numbers in the program or do create a seaprate file and connect them
fivenumbers.txt file
-53 -22 -87 -103 -3
numbers.txt file
thenumber file
53 22 87 103 -3 75 220 1 64 543 98 44
my code
#include
const int ARRAY_SIZE = 100;
// Function to read integers from a file into an array and return the number of elements read int readFromFile(const std::string& filename, int numbers[]) { std::ifstream inputFile(filename); int count = 0;
if (inputFile.is_open()) { while (count > numbers[count]) { count++; } inputFile.close(); }
return count; }
// Function to find the lowest number in the array int getLowest(const int numbers[], int count) { int lowest = numbers[0]; for (int i = 1; i
// Function to find the highest number in the array int getHighest(const int numbers[], int count) { int highest = numbers[0]; for (int i = 1; i highest) { highest = numbers[i]; } } return highest; }
// Function to calculate the sum of the numbers in the array int getSum(const int numbers[], int count) { int sum = 0; for (int i = 0; i
// Function to calculate the average of the numbers in the array double getAverage(const int numbers[], int count) { int sum = getSum(numbers, count); return static_cast
int main() { int numbers[ARRAY_SIZE]; std::string filename;
std::cout > filename;
int count = readFromFile(filename, numbers);
if (count > 0) { std::cout
return 0; }
You must write a program that contains several functions (as described below). The "main" function must: - Define an array of 100 integers (do NOT use a vector for this lab exercise): \[ \text { const int ARRAY_SIZE }=100 \text {; } \] int numbers [ARR__SIZE]; - Output a prompt to the screen, asking the user to specify the input file name. (The program must work correctly for any file name that the user chooses.) Open this file for input. The file contains integers (one per line). - Read the contents of the file into the array. - Call four separate functions: getLowest, getHighest, getSum, and getAverage, and display the value that each function returned. Format the output in a manner similar to the sample output on the next page of this document. The getLowest, getHighest, and getSum functions must each contain a loop that scans the data in the array and returns the appropriate result: - The getLowest function returns the lowest number in the array. - The getHighest function returns the highest number in the array. - The getSum function returns the total of the numbers in the array. The getAverage function must call the getSum function, and use its return value to calculate the average of the numbers in the array. The getAverage function returns the average value to its caller. Design Restriction For this lab exercise you must use an array of integers, not a vector of integers. We will use vectors later in the course, but for now we need to use only arrays. Sample Data Files and Sample Program Output The ZIP file, which can be downloaded from Moodle, contains two text files. These should be used to test your program. (They are also shown below.) The sample data and sample program output are provided below. (In the sample program output shown in this document, text typed by the user is shown in BOLD font. In actuality, all text will be displayed in the same font.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started