Problem 9.1 Check g++ compiler & Write simple program
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
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
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.
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 |
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 |
Language: C++
Write a simple program for implementing the guessing game as outlined in the slides (Tutorial 9, slides 56).
You can assume that the input will be valid.
Problem 9.6 Function overloading |
Language: C++
Write three overloaded functions myfirst() which should do the following:
- 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;
- 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;
- 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
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 (
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 |
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.
Reviews
There are no reviews yet.