CH-230-A Assignment 9 – C++ Introduction, Function Overloading, References, Dynamic Memory Allocation solution

$29.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (4 votes)

Problem 9.1 Check g++ compiler & Write simple program (1 point) Presence assignment, due by 11:00 AM today Graded manually Language: C++ 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) Due by Monday, November 8th, 23:00 Graded manually Language: C++ 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) Due by Monday, November 8th, 23:00 Graded manually Language: C++ Consider the abs function, which determines the absolute values of a real number. The function returns the following values: abs(x) =  -x for x < 0, x for x ≥ 0. Write a function which determines and returns the absolute values of a float parameter. Then write a main() function which calls the function from above and prints on the screen the returned value. You may not use any library functions related to the absolute value. You can assume that the input will be valid. Problem 9.4 Function overloading (1 point) Due by Monday, November 8th, 23:00 Graded manually 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) Due by Monday, November 8th, 23:00 Graded manually 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) Due by Monday, November 8th, 23:00 Graded manually 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. Write a program which calls the above functions and illustrates their effect. You may choose to enter test data from the keyboard or to initialize variables within your code. You can assume that the input will be valid. Problem 9.7 Swapping with call-by-reference (1 point) Due by Monday, November 8th, 23:00 Graded manually Language: C++ 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 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) Due by Monday, November 8th, 23:00 Graded manually Language: C++ 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) Due by Monday, November 8th, 23:00 Graded manually 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 description of 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 a9 p1.[c or cpp or h] Firstname Lastname myemail@jacobs-university.de */ • You have to submit your solutions via Grader at https://cantaloupe.eecs.jacobs-university.de. If there are problems (but only then) you can submit the programs by sending mail to k.lipskoch@jacobs-university.de with a subject line that begins with CH-230-A. It is important that you do begin your subject with the coursenumber, otherwise I might have problems to identify your submission. • Please note, that after the deadline it will not be possible to submit any solutions. It is useless to send late solutions by mail, because they will not be accepted. This assignment is due by Monday, November 8th, 23:00.