Question

In: Computer Science

Objectives To learn to code, compile, and run a program using file input and an output...

Objectives

To learn to code, compile, and run a program using file input and an output file.

Assignment

Plan and code a program utilizing one file for input and one file for output to solve the following problem:

Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown.

Note

Label all output clearly. Be sure your output file contains user prompts and what was entered by the user in addition to the results of your program processing. Be sure your output file contains data that was input in addition to the results of your program processing. Create the data file below using your text editor or Notepad. Copying and pasting the numbers might create problems with reading from the files. Type in the numbers. Separate numbers by using either the tab key or blank spaces.

Data File Sample

346       130       982       90         656       117       595

415       948       126       4            558       571       87

42         360       412       721       463       47         119

441       190       985       214       509       2            571

77         81         681       651       995       93         74

310       9            995       561       92         14         288

466       664       892       8            766       34         639

151       64         98         813       67         834       369

Solutions

Expert Solution

Here is the code for the given problem :-

#include<iostream.h>
#include<fstream.h>
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    std::ifstream fin("input.txt");
    int val=0, rows=0, cols=0, numItems=0;
    ofstream myfile;
    myfile.open ("output.txt");
    
    while( fin.peek() != '\n' && fin >> val )
    {
        myfile << val << ' ';
        ++numItems;
    }
    cols = numItems;// # no of columns =7

    myfile << '\n';
    while( fin >> val )
    {
        ++numItems;
        myfile << val << ' ';
        if( numItems%cols == 0 ) cout << '\n';
    }

    if( numItems > 0 )// got data!
    {
        rows = numItems/cols;
        myfile << "rows = " << rows << ", cols = " << cols << '\n';
    }
    else// didn't get any data
        myfile << "data reading failed\n";

    vector<vector<<int>> matrix;
    while(row!=0)
    {
        vector<int> a;
        int sum=0
        while( fin.peek() != '\n' && fin >> val )
        {
            sum+=val;
            a.push_back(val);
        }
        myfile << "\nMax Element of this line = "
         << *max_element(a.begin(), a.end()); 
        myfile << "\nMin Element of this line = "
         << *min_element(a.begin(), a.end());
        myfile<<"\nAverage of the elements of this line = "
         << sum/7;
        row--;
    }
    myfile.close();
    return 0;
    
}

Here we read the file "input.txt" using ifstream fin, and writes the data present in it along with max element, min element, and the average of each line in another file "output.txt" and then close the file.

Hope this helps :) Feel free to ask any doubt in the comment section.


Related Solutions

Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or...
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or DecimalFormat Practice programming simple mathematical calculations Instructions: Samwise Gamgee has finally decided to submit his expense report for all of his adventures regarding his travels to Mordor. Part of those expenses are his stay at the Prancing Pony Inn located in Bree. You are to write a simple Java program which will generate an Invoice for his stay at the Inn. Your program should...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
This program does compile. But it does not give the expected output. It is supposed to...
This program does compile. But it does not give the expected output. It is supposed to raise the value of the input to its power all the way from 0 to 10. The only output seen is the number raised to the power 0 over and over again. what did I do wrong? help please. // This program raises the user's number to the powers // of 0 through 10. #include <iostream> #include <cmath> using namespace std; int main() {...
Using the code below from “LStack.h” file, write the code for a main program that takes...
Using the code below from “LStack.h” file, write the code for a main program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions { 25 + ( 3 – 6 ) * 8 } and 7 + 8 * 2 contains matching grouping symbols. However, the expression 5 + { ( 13 + 7 ) / 8 - 2 * 9 does not contain matching grouping symbols....
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
Write a program with total change amount as an integer input, and output the change using...
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes c++ please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT