Question

C++: Make sure to identify codes with comments and no other programming other than C++ (please...

C++: Make sure to identify codes with comments and no other programming other than C++ (please make it simple as possible)

DON'T COPY CODES FROM OTHER ANSWERS

Create a program that:

          - in main

                   - opens the file provided for input

                   - calls a function to determine how many lines are in the file

                   - creates an array of the proper size

                   - calls a function to read the file and populate the array

                   - calls a function to write out the contents of the array in reverse order

                      output file should be named 'reverse_output.txt'

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

Screenshot

-------------------------------------------------------------------

Program

//Header files
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
//Function prototypes
void findLineCount(ifstream& in, int &count);
void readfile(ifstream& in, int count,int* arr);
int main()
{
   //Input file name object
   ifstream in("C:/Users/deept/Desktop/input.txt");
   //Ariable for lines of file
   int lineCount=0;
    //Array for store data
   int *intArray=NULL;
   //Error check in opening file
   if (!in) {
       cout << "File not open!!!" << endl;
       exit(0);
   }
   //Function call to get lines in the file
   findLineCount(in,lineCount);
   //Close the file
   in.close();
   //Create file open
   in.open("C:/Users/deept/Desktop/input.txt");
   //Error check
   if (!in) {
       cout << "File not open!!!" << endl;
       exit(0);
   }
   //allocate array size
   intArray=new int[lineCount];
   //Function call to read file data and store into array
   readfile(in, lineCount,intArray);
   //Close file
   in.close();
   //Output file object
   ofstream out("C:/Users/deept/Desktop/reverse_output.txt");
   //Error check
   if (!out) {
       cout << "File not open!!!" << endl;
       exit(0);
   }
   //Write into file
   for (int i = lineCount - 1; i >= 0; i--) {
       out << intArray[i] << endl;
   }
   //close file
   out.close();
   cout << "Reverse write completed" << endl;
   //Free allocated memory
   delete[] intArray;  
}
//Method to find lines count
void findLineCount(ifstream& in, int &count) {
   int data;
   while (in>>data) {
       count++;
   }
}
//Method to read file
void readfile(ifstream& in, int count, int* arr) {
   for (int i = 0; i < count; i++) {
       in >>*(arr+i);
   }
}

----------------------------------------------------------

input file

18
9
27
5
48
16
2
53
64
98
49
82
7
17
53
38
65
71
24
31

output file

31
24
71
65
38
53
17
7
82
49
98
64
53
2
16
48
5
27
9
18

Add a comment
Know the answer?
Add Answer to:
C++: Make sure to identify codes with comments and no other programming other than C++ (please...
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++ 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...

  • ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...

    ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability to create an array on the heap allowing user to choose the number of values to store. Demonstrate the ability to store an array of Struct values on both the stack and the heap. Program Specifications: 1. Create a struct with at least 3 fields - any struct you want but explain it in your comments in the code. Populate at least 10 elements...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...

  • Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the...

    Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the main method creates two arrays, each with 10 int slots. main initializes the first array to randomly generated integers. It then calls a method named copy, passing it the two arrays. Your copy method copies the contents of the first array to the second array. It then returns to main. main then displays the contents of both arrays. b) Write a program in which...

  • C programming. please include comments In this lab, you will learn to read data from and...

    C programming. please include comments In this lab, you will learn to read data from and write data to a file - another use for pointers. SAMPLE PROGRAM There are two sample programs this week - WriteFile.c and ReadFile.c. The first, WriteFile.c, allows the user to enter customer names (first, middle, and last), and writes each name to an output text file, customerNames.txt. formatted as: firstName middlelnitial lastName" After you've run it once, what happens to the names you added...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

  • Please write comments with the program. Python programming!! Part III - write student data In this...

    Please write comments with the program. Python programming!! Part III - write student data In this part the program should create a .csv file and fill it with generated student information. The program should: Ask the user for a name for the .csv file (use your own name for the exercise) Create the .csv file with columns named - ID, first-name, GPA Based on the information received in part I, generate and fill students information in the CSV file Tell...

  • Programming in C with comments/steps 1. Overview The purpose of this assignment is to give you...

    Programming in C with comments/steps 1. Overview The purpose of this assignment is to give you some experience with writing functions that take in arguments and return values, as well as writing a program that consists of multiple files. This assignment also requires the use of a switch statement, in addition to more practice with printf& scanf declaring variables, using loops, and using logical expressions. For this assignment, you will prompt the user in the main function to enter their...

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
ADVERTISEMENT