Description
Assignment 9 – C++ Introduction, Function Overloading, References, Dynamic Memory Allocation
• The problems of this assignment must be solved in C or C++ (instruction in each problem).
• Your programs should have the input and output formatting according to the testcaseslisted after the problems.
Problem 9.1 Check g++ compiler & Write simple program (1 point)
Make sure that you have g++ installed and you can use an IDE or an editor to write and compile C++ code.
Write a program that reads your country of origin from the standard input (i.e., keyboard) and prints it on the standard output (i.e., screen) using cin and cout. You can assume that the input will be valid and will not contain spaces.
Problem 9.2 Using different variables (1 point)
Write a program which reads one integer value n, one double value x and a string s from the keyboard. Then s and x should be printed on the screen (separated by ’:’ with a newline after the double value). This printing should be repeated n times.
You can assume that the input will be valid and the string will not contain spaces.
Problem 9.3 Absolute value function (1 point)
Consider the abs function, which determines the absolute values of a real number. The function returns the following values:
-x for x < 0, x for x≥ 0.
You can assume that the input will be valid.
Problem 9.4 Function overloading (1 point)
Language: C++
Write a program that provides two overloaded functions named … mycount(…). This function either computes the difference between the second and first parameter (in this order) if integers are passed or counts the number of occurrences of a character if a character and a string are passed.
For example, mycount(7, 3) should return −4 and mycount(’i’, “this is a string”) should return 3. In case of no occurrence 0 should be returned.
Write a simple main() function that demonstrates the above described behavior for both functions.
You can assume that the input will be valid.
Problem 9.5 A guessing game (2 points)
Language: C++
Write a simple program for implementing the guessing game as outlined in the slides (Tutorial 9, slides 5−6).
You can assume that the input will be valid.
Problem 9.6 Function overloading (1 point)
Language: C++
Write three overloaded functions … myfirst(…) which should do the following:
1) if called with an array of integers, it determines and returns the first positive and even value from the array. If no such element exists then −1 should be returned;
2) if called with an array of doubles, it determines and returns the first negative element which does not have a fractional part. If no such element exists then −1.1 should be returned;
3) if called with an array of chars, it determines and returns the first element which is a consonant. If no consonants are present in the array then the character ’0’ should be returned.
Problem 9.7 Swapping with call-by-reference (1 point)
Write a program swapref.cpp, which provides three overloaded functions void swapping(…). These functions should swap two integers, two floats, and two pointers to char. The swapping should be done by a “real” call-by-reference (i.e., not by using ∗). Complete the following code fragment:
#include <iostream>
using namespace std;
void swapping(…) { …. } // swap ints void swapping(…) { …. } // swap floats void swapping(…) { …. } // swap char pointers
int main(void) {
int a = 7, b = 15; float x = 3.5, y = 9.2; const char ∗str1 = “One”; const char ∗str2 = “Two”;
cout << “a=” << a << “, b=” << b << endl; cout << “x=” << x << “, y=” << y << endl; cout << “str1=” << str1 << “, str2=” << str2 << endl;
swapping(a, b); swapping(x, y); swapping(str1, str2);
cout << “a=” << a << “, b=” << b << endl; cout << “x=” << x << “, y=” << y << endl; cout << “str1=” << str1 << “, str2=” << str2 << endl; return 0; }
Problem 9.8 Dynamic allocation and references (1 point)
Write a program which reads from the keyboard an integer n followed by n integer values which are to be stored in a dynamically allocated array a. Your program should define a function void subtract_max(…) for determining the maximum value in the array and subtracting this maximum from all elements of the array. You should also define a function called void deallocate(…) for releasing the memory which was allocated for the array. The main() function should allocate memory for the array, call the function, illustrate its effect by printing the values of the array and finally deallocate the memory occupied by the array by calling the second function from above. You can assume that the input will be valid.
Problem 9.9 Word guessing (2 points)
Language: C++
Write a program that stores an array of words (containing ”computer”, ”television”, ”keyboard”, ”laptop”, ”mouse”) and 12 other words of your choice in an array of strings. Inside of a game loop your program should randomly choose one word out of the 17 possible words. The program should print the word on the screen after replacing all vowels by underscores, then the player should try to guess the word. After the player has guessed the right word, the number of tries should be printed on the screen and the player should be asked whether he/she wishes to play again. If the player enters ”quit” as a word guess, then the game should immediately stop.
You can assume that the input will be valid and that ”quit” will not be in the set of words to be guessed.
How to submit your solutions
• Your source code should be properly indented and compile with gcc or g++ depending on the problem without any errors or warnings (You can use gcc -Wall -o program program.c or g++ -Wall -o program program.cpp). Insert suitable comments (not on every line …) to explain what your program does.
• Please name the programs according to the suggested filenames (they should match the descriptionof the problem) in Grader. Otherwise you might have problems with the inclusion of header files. Each program must include a comment on the top like the following:
/* CH-230-A
a9p1.[c or cpp or h]
*/
If there are problems (but only then) you can submit the programs by sending mail to




Reviews
There are no reviews yet.