Question

For this activity, you are required to provide files called fun.cpp as well as a makefile to comp...

For this activity, you are required to provide files called fun.cpp as well as a makefile to compile and run it. In your file, fun.cpp, should have a skeleton of a main program as per normal which you will then fill it in as per the following.

The objective of this activity is to demonstrate the basics of functions and their use. For this activity, you will need to declare your functions alongside your main program skeleton. You will use these functions inside your main program.
You will need to declare a function: echo. The echo function returns a string, and receives one as an input. Set the default value of this input variable as “echo”.

The echo function merely receives its input and returns it.
Once echo has been defined, you are going to perform a function overloading of this function. Function overloading lets one reuse a function name, but change the input vari- ables so that the same function can be used under different conditions. Therefore, define another echo function. This is declared as before, but instead of taking a single string variable, this one takes a string variable and an integer variable. This will not have any default values.

In this function, you will still return the input of the string that is passed into the function. If the number passed in is an even number (which includes 0 in this case), then it is simply appended to the string. If it is an odd number, then it is put in front of the passed in string.
The main program of your file, will work as follows. At the start of the program you will read from a file called values.txt. This file will have string lines, each on a separate line. You will then pass these strings, one line at a time, into the echo functions and print the results. The original echo function should be called first (and output first) with the second function called immediately after. Additionally, the second echo function should also be passed the line number (starting from 0) that the line they has. Each line should be printed on a new line.

For example, given the following:
this
is
a
sentence



The output (for the first line) will be:
this
this0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE: ( Compiled using Code::Blocks)

1. fun.cpp

#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

string echo(string input = "echo"){
    return input;
}

string echo(string input,int num){
    string result;
    if(num%2==0){
        stringstream stm;
        stm << input << num;
        result = stm.str();
    }else{
        stringstream stm;
        stm << num << input;
        result = stm.str();
    }
    return result;
}

int main()
{
    ifstream infile("values.txt");

    string line;
    int linenumber = 0;

    if(infile.is_open()){
        while (getline(infile, line))
        {
            cout<<echo(line)<<endl;
            cout<<echo(line,linenumber)<<endl;
            linenumber++;

        }
        infile.close();
    }
}

2. Makefile ( Generated by cbp2make):

WORKDIR = %cd%

CC = gcc.exe
CXX = g++.exe
AR = ar.exe
LD = g++.exe
WINDRES = windres.exe

INC =
CFLAGS = -Wall -fexceptions
RESINC =
LIBDIR =
LIB =
LDFLAGS =

INC_DEBUG = $(INC)
CFLAGS_DEBUG = $(CFLAGS) -g
RESINC_DEBUG = $(RESINC)
RCFLAGS_DEBUG = $(RCFLAGS)
LIBDIR_DEBUG = $(LIBDIR)
LIB_DEBUG = $(LIB)
LDFLAGS_DEBUG = $(LDFLAGS)
OBJDIR_DEBUG = obj\\Debug
DEP_DEBUG =
OUT_DEBUG = bin\\Debug\\echoo.exe

INC_RELEASE = $(INC)
CFLAGS_RELEASE = $(CFLAGS) -O2
RESINC_RELEASE = $(RESINC)
RCFLAGS_RELEASE = $(RCFLAGS)
LIBDIR_RELEASE = $(LIBDIR)
LIB_RELEASE = $(LIB)
LDFLAGS_RELEASE = $(LDFLAGS) -s
OBJDIR_RELEASE = obj\\Release
DEP_RELEASE =
OUT_RELEASE = bin\\Release\\echoo.exe

OBJ_DEBUG = $(OBJDIR_DEBUG)\\main.o

OBJ_RELEASE = $(OBJDIR_RELEASE)\\main.o

all: debug release

clean: clean_debug clean_release

before_debug:
   cmd /c if not exist bin\\Debug md bin\\Debug
   cmd /c if not exist $(OBJDIR_DEBUG) md $(OBJDIR_DEBUG)

after_debug:

debug: before_debug out_debug after_debug

out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG)
   $(LD) $(LIBDIR_DEBUG) -o $(OUT_DEBUG) $(OBJ_DEBUG) $(LDFLAGS_DEBUG) $(LIB_DEBUG)

$(OBJDIR_DEBUG)\\main.o: main.cpp
   $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c main.cpp -o $(OBJDIR_DEBUG)\\main.o

clean_debug:
   cmd /c del /f $(OBJ_DEBUG) $(OUT_DEBUG)
   cmd /c rd bin\\Debug
   cmd /c rd $(OBJDIR_DEBUG)

before_release:
   cmd /c if not exist bin\\Release md bin\\Release
   cmd /c if not exist $(OBJDIR_RELEASE) md $(OBJDIR_RELEASE)

after_release:

release: before_release out_release after_release

out_release: before_release $(OBJ_RELEASE) $(DEP_RELEASE)
   $(LD) $(LIBDIR_RELEASE) -o $(OUT_RELEASE) $(OBJ_RELEASE) $(LDFLAGS_RELEASE) $(LIB_RELEASE)

$(OBJDIR_RELEASE)\\main.o: main.cpp
   $(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c main.cpp -o $(OBJDIR_RELEASE)\\main.o

clean_release:
   cmd /c del /f $(OBJ_RELEASE) $(OUT_RELEASE)
   cmd /c rd bin\\Release
   cmd /c rd $(OBJDIR_RELEASE)

.PHONY: before_debug after_debug clean_debug before_release after_release clean_release

OUTPUT:

D:lechoolbin\Debuglechoo.exe this thise 1s lis a2 sentence 3sentence Process returned θ (8x8) execution time : θ. 554 s Press

Add a comment
Know the answer?
Add Answer to:
For this activity, you are required to provide files called fun.cpp as well as a makefile to comp...
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++ 2.3 Activity 3: Bubble Sort For this activity, you are required to provide files called...

    C++ 2.3 Activity 3: Bubble Sort For this activity, you are required to provide files called act3.cpp as well as a makefile to compile and run it. In your file, act3.cpp, should have a skeleton of a main program as per normal which you will then fill it in as per the following. The objective of this activity is to demonstrate the bubble sort algorithm for arrays. You are going to implement this as a function with the following definition:...

  • Programming languages allow the programmer to create his or her own functions which are called __________...

    Programming languages allow the programmer to create his or her own functions which are called __________ __________ functions. The function that returns the number of characters in a string is the __________ function. __________ files contain records that must be processed in the order in which they were created. The elements of an array are stored in __________ storage locations in the computer’s memory. A variable that is declared outside all program modules, including the main module, has __________ scope....

  • Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors....

    Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes,  etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...

  • C++ Project You should be comfortable with the content in the modules up to and including...

    C++ Project You should be comfortable with the content in the modules up to and including the module "Input Output" for this project. For this project, download the text file weblog.txt. Note: To download this file, right click on the link and select SAVE AS This file is an Apache web log taken from the web server for St. Mary's University. When a visitor goes to their web site, the visitor's browser makes a request to the web server to...

  • In this question you have to write a C++ program to convert a date from one...

    In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the string that the function...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of amain () function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • 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....

  • There is a file called mat2.txt in our files area under the Assignments folder. Download it...

    There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...

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