Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Introduction

to Programming
C and C++ Exercises
Exercise Sheet: 4 https://ubicomp.eti.uni-siegen.de
Due on: 01/12/2023 20:00 228 hours left till deadline /teaching-page Moodle: 17761
Notice: Each source file that you write should start with the following comment header at the top:
/∗ Name:
StudentID:
Description: ∗/

Assignment 4.1 Reveal one character in a given word


Write a program that first asks the user for any word, and then asks the user for a particular character (that may or may not
be present in this word). Then your program should output only those characters of the word that are equal to the given
character to the terminal.
Example: If the user enters the word: "mamamiapizzeria" and the character ’a’, your program's output should be:
-a-a--a-------a
Follow the next steps (see chapter 5 in the course's slides ):
Create in your home directory a subdirectory ex031 and switch into this directory
Create a file called reveal.cpp by launching the nano editor with this file name as parameter
Once in nano, write a valid C++ program that does the above, by using an array of char elements
The word should be read as an array of characters from the terminal (use std::cin). You can assume
that the user will give a valid word with at most 100 characters.
End your output to the terminal always with a new line
Validate your program with cpplint and check, and use indentation and appropriate comments

Assignment 4.2 The add-a-third sequence


Write a program that asks the user for a number between 10 and 100, to then print out a series of numbers as follows:
Start by printing that number, then the following numbers should be 1/3 larger than the previous and rounded down to the
nearest integer, ensure that all numbers are smaller than 1000000 (so end the sequence once the next number is bigger), and
separate all numbers with just one comma.
Example: When the user types 45, the output in the terminal should start with:
45,60,80,106,141,188,250,333,444,592,789,1052,1402,1869,2492,3322,4429,5905,7873 (and so on)
Create in your home directory a subdirectory ex009 and switch into this directory
Create a file called addthird.cpp by launching the nano editor with this file name as parameter
To round a number down to the nearest integer, include cmath: #include <cmath> and use its function
floor(). The floor function takes a double variable as its one argument and returns the rounded
number as a double, for example: std::cout << floor(3.14); \\ will return 3
Ensure that floor's returned double variable is explicitly treated as an integer when printing out to
the terminal, and end your sequence of numbers with a new line (see section 2.5 in the slides)
Comment specifially in your code why solving this with an array would be inefficient and difficult
Comment why using floor might be preferable to just converting the double variables to integers
Validate your program with cpplint and check, and use indentation and appropriate comments

Assignment 4.3 A vowel- and digit-counter


Write a program that counts the numbers of lower case vowels (a, e, i, o, u) and the number of digits
(0,1,2,3,4,5,6,7,8, or 9) in a given sequence of symbols. The part of the sequence which should be analyzed is always
terminated with hash symbol '#'.
Note that this terminator symbol may be in the middle, at the end or even at the beginning of the sequence, but when '#' is
reached, all further symbols should be disregarded.
Example: When the user gives: abaec2ude3o9fg#habghfb, the output in the terminal should be:
vowels: a=2, e=2, i=0, o=1, u=1
digits: 3
Create in your home directory a subdirectory ex008 and switch into this directory
Create a file called vowels.cpp by launching the nano editor with this file name as parameter
For the input sequence, use a char array of size 100
In this exercise, you can assume that the user always enters a series of maximally 100 symbols
Validate your program with cpplint and check, and use indentation and appropriate comments

You might also like