Question

Language: c++ Write a program that opens a file to read 20 floating point numbers and...

Language: c++

Write a program that opens a file to read 20 floating point numbers and store the information in a 4 x 5 array. The program should do the following:

a. Compute the average of each set of 5 values

b. Determine the largest value of the 20 values

c. Report the results; print the original matrix and average of each row, and the largest of the values in this function.

d. At the end of the program prompt the user press "Y or y" and any other character to quit.

Each part should be handled by a separate function. For parts a and b you must use pointer notations. Do not use any global arrays or variables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

#include <iostream>
#include <fstream>
using namespace std;
// Function prototypes
void compute_average(float array[4][5],float average[4]);
float find_largest(float array[4][5]);
void print_results(float array[4][5],float average[4],float large);
// Main Function
int main()
{
float arr[4][5],avg[4];
float largest;
int i,j;
char c;
string line;
ifstream myfile;
  
myfile.open("float_numbers.txt"); // Opening the file for reading
  
if(!myfile) // Checking whether the file is opened or not
{
cout << "Unable to open file";
exit(1); // terminate with error
}
  
// Reading floating-point numbers from file and storing them into an array
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
myfile >> arr[i][j];
}
}
// Calling the function to compute the average of each row in array
compute_average(arr,avg);
// Calling the function to find the largest of all numbers in the array
largest = find_largest(arr);
  
do
{
// Calling the function to print the results
print_results(arr,avg,largest);
cout << "\nY/y or any other character to quit\n";
cin >> c;
}while((c == 'N') || (c == 'n'));
  
myfile.close();
}
//Definition of function to compute the average of each row in array
void compute_average(float array[4][5],float average[4])
{
int i,j;
  
for(i=0;i<4;i++)
{
average[i] = 0;
for(j=0;j<5;j++)
{
average[i] += array[i][j];
}
}
  
for(i=0;i<4;i++)
{
average[i] = (average[i])/5;
}
}
// Definition of the function to find the largest of all numbers in the array
float find_largest(float array[4][5])
{
int i,j;
float max=0.0;
  
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
if(array[i][j]>max)
max = array[i][j];
}
}
return max;
}
// Definition of the function to print the results
void print_results(float array[4][5],float average[4],float large)
{
int i,j;
cout << "\nOriginal Matrix: \n";
  
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
cout << array[i][j] << "\t";
}
cout << "\n";
}
  
cout << "\nAverage of each row: \n";
  
for(i=0;i<4;i++)
{
cout << average[i] << "\n";
}
  
cout << "\nLargest value is: " << large;
}

Please refer to the following screenshot of the program for indentation of the code:

Sample Inputs and Outputs:

Input File1:

Output:

Input File2:

Output:

Add a comment
Know the answer?
Add Answer to:
Language: c++ Write a program that opens a file to read 20 floating point numbers and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load...

    Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load them into an array. Add up all the values and store the answer in a variable. Find the largest and smallest number in the array. Put each into its own variable. Print to the screen the sum, largest value, and smallest value. Copy the array to a second array in reverse order. Print out the second array. Read in 20 integer numbers, all in...

  • C++ Write a program with the following elements: in main() -opens the 2 files provided for...

    C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • write a program that will read a file containing floating point numbers, store the numbers into...

    write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. Your program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. Numbers found in file 100.7362 105.2896 66.3493 105.6688 91.6180 64.8770 73.9249 87.3441...

  • Program must be in C language and MUST be able to run on visual studio 2....

    Program must be in C language and MUST be able to run on visual studio 2. Using Pointers and Arrays 2.1 Write a function bubblesort which will accept an array of double precision floating point numbers of length N and sort the array. Ref: https://en.wikipedia.org/wiki/Bubble_sort (I can verify this is accurate) 2.2 Write a program which exercises the bubblesort. For a good test, the program should have more than 20 values.

  • 29. (20 points) Write the complete C++ program that implements the following program, as if you...

    29. (20 points) Write the complete C++ program that implements the following program, as if you were turning this in for homework. Your program should have a commented header (your name, etc), comments within the program and a well-built display for the output. Don't forget the pre-processor commands Define an array called nums with a maximum of 20 integers, and fill the array with numbers, recei the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT