Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of this program is to take the input from quote and put it into 'darray.' Then reverse it and output it to a

The goal of this program is to take the input from "quote" and put it into 'darray.' Then reverse it and output it to a file in reverse order with one word per line.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

reverse_file.cpp

#include #include #include #include "array.hpp"

int main(int argc, char * argv[]) {

// check program launched with correct number of arguments

// open input file and check if opened correctly // return error code if failed

// open output file and check if opened correctly // return error code if failed

// darray to hold words read in from file (do not modify) darray<:string> words(1);

// read each word into darray (grow array with reset_capacity as needed)

// write each word to output file (one on a line) // in the revese order they were read from input file

// close input/output file or let // them go out of scope to close

return 0; }

array.hpp

#ifndef CS2_ARRAY_HPP #define CS2_ARRAY_HPP

#include #include

template class darray {

private: type * data; int cap;

darray(int new_cap, const darray & rhs);

public:

// constructors darray() : data(nullptr), cap(0) {} darray(int); darray(const darray & rhs);

// destructor ~darray();

// swap void swap(darray & rhs); // assignment darray & operator=(darray rhs);

// subscript type operator[](int) const; type & operator[](int);

// capacity() int capacity() const;

// reset capacity void reset_capacity(int new_cap);

};

template darray::darray(int new_cap) : cap(new_cap) { data = new type[new_cap]; }

template darray::~darray() { delete [] data; }

template darray::darray(const darray & rhs) : darray(rhs.cap) { for(int i = 0; i

template darray::darray(int new_cap, const darray & rhs) : darray(new_cap) {

int smaller = new_cap; if(rhs.cap

for(int i = 0; i

}

template void darray::swap(darray & rhs) {

int temp_cap = cap; cap = rhs.cap; rhs.cap = temp_cap;

type * temp_data = data; data = rhs.data; rhs.data = temp_data;

}

template darray & darray::operator=(darray rhs) { swap(rhs); return *this; }

template type darray::operator[](int i) const { assert(i >= 0 && i

template type & darray::operator[](int i) { assert(i >= 0 && i

template int darray::capacity() const { return cap; }

template void darray::reset_capacity(int new_cap) { darray temp(new_cap, *this); swap(temp);

/* int * temp_data = new int[new_cap]; for(int i = 0; i = cap) { temp_data[i] = 0; } else { temp_data[i] = data[i]; } }

delete [] data; data = temp_data; cap = new_cap; */ }

template std::ostream & operator & ar) { for(int i = 0; i

template std::istream & operator>>(std::istream & in, darray & ar) {

for(int i = 0; i > ar[i]; }

return in; }

#endif

quote.txt

The major cause of the software crisis is that the machines have become several orders of magnitude more powerful! To put it quite bluntly: as long as there were no machines, programming was no problem at all; when we had a few weak computers, programming became a mild problem, and now we have gigantic computers, programming has become an equally gigantic problem.

Command Line Arguments std::cin and files are two of the ways you have learned to provide input to a program Command line arguments are a third method that is generally far more preferable than std::cin Command line arguments are typed as part of the command you use to run the program Take clang++ main.cpp -o main: main.cpp, -o, and main are command line arguments . The terminal uses whitespace to split arguments and then passes them to the program The Real main Here is an expanded version of the main function int main(int argc, char * argv[]) { return 0; } We have a new argc and argv. These parameters are how the terminal supplies the command line arguments argc - the number of command line arguments (size of argy) argv - an array containing the arguments (as const char[]) Reverse File The program (reverse_file.cpp) will take the name of two files from the command line The goal is to read the words from the input file and output them to the output file in reverse order (one per line) You will use darray to perform the reverse Exact process is laid out in the comments in reverse_file.cpp Completion Requirements You have implemented the reverse_file.cpp correctly

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started