Question

C++ program The aim of this assignment is to practice writing recursive functions. For this homework...

C++ program

The aim of this assignment is to practice writing recursive functions. For this homework assignment write two recursive functions: The first one receives a vector of File pointers and recursively outputs properties of every file stored inside this vector. The second function receives a vector of File pointers and a string which represents a type of the file (gif or txt). The function should return another vector of File pointers containing only Image files or Text files, depending on the second parameter. The function should be recursive.

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

Hi,

The problem has been resolved. Two functions have been created named "fileProp1" and "fileProp2". One method has only one argument "vector" and the second method have two arguments "vector and string". To show the file property,I'm printing the size of file. Both methods are recursive.

Output screenshot has been attached below, Please refer to that for better understanding.

Code:

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

#include<fstream>
#include<vector>
#include<iostream>
using namespace std;

void fileProp1(vector<ifstream*> files){

     
   if(files.size()==0){
       return;
   }
     
   ifstream* file= files.at(0);


   if(!file->is_open())
{
return;
}

file->seekg(0, ios::end);
int fileSize = file->tellg();
file->close();


   cout<< "\nsize: " << fileSize << endl;

  
   files.erase (files.begin());


   fileProp1(files);
}

void fileProp2(vector<ifstream*> files, string type){

     
   if(files.size()==0){
       return;
   }
     
   ifstream* file= files.at(0);


   if(!file->is_open())
{
return;
}

file->seekg(0, ios::end);
int fileSize = file->tellg();
file->close();


   cout<< "\nsize: " << fileSize << endl;

  
   files.erase (files.begin());


   fileProp2(files,"txt");
}


int main()
{
   vector<ifstream*> files;


   ifstream f("file.txt");
   ifstream f1("file1.txt");
   ifstream f2("HomeworkLib1.gif");
   files.push_back(&f);
   files.push_back(&f1);
   files.push_back(&f2);

   cout<<"\nvecto only method:\n";
   fileProp1(files);

   vector<ifstream*> files1;
   ifstream f5("file.txt");
   ifstream f6("file1.txt");
   files1.push_back(&f5);
   files1.push_back(&f6);

   cout<<"\nvector and string method:\n";
   fileProp2(files1,".txt");

}

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

The problem has been resolved, I hope you will like the solution. If you have any question or query regarding this problem or other, please comment below and give the positive rating.

Thank You!! Happy Learning!! Keep Chegging!!

Add a comment
Know the answer?
Add Answer to:
C++ program The aim of this assignment is to practice writing recursive functions. For this homework...
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++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design...

    C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design a hierarchy of Files. You have two different kinds of Files, Text File and an Image File. The files are identified by their name and type, which is identified by either txt or gif extension. An Image file has dimensions of pixel rows and pixel columns. Each pixel has a color depth that can be represented by number of bits. (8 bits is one...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • can someone help with this? need to use c++. The aim of this homework assignment is...

    can someone help with this? need to use c++. The aim of this homework assignment is to practice writing classes. This homework assignment will help build towards project I, which will be to write a program implementing Management of Rosters System. For this assignment, write a class called Student. This class should contain information of a single student. This information includes: last name, first name, standing, credits gpa, date of birth, matriculation date. For now you can store date of...

  • In C++ In this homework, you will be tasked with creating functions to manipulate strings that...

    In C++ In this homework, you will be tasked with creating functions to manipulate strings that come from files and then outputting the strings to another file. All of these functions are housed together in a menu. The user will be asked to input a file name and then their menu selection. The menu is this: Get rid of white space Print amount of characters in the file Print amount of words in the file Replace all vowels with 3's...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • In this laboratory we are going to do part of the Homework 2 assignment. We will...

    In this laboratory we are going to do part of the Homework 2 assignment. We will be creating the program which takes a file containing the message in morse code and converts it to the alphabetic message. Your program should include the following functions : class Code { public: Code(); // Default constructor - loads and uses morse code string decode(vector< string> message); // decodes a message private: vector<string> codewords; // this is a codeword vector parallel to A-Z vector<char>...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

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