






Besser lernen dank der zahlreichen Ressourcen auf Docsity
Heimse Punkte ein, indem du anderen Studierenden hilfst oder erwirb Punkte mit einem Premium-Abo
Diese Seite wird in der Vorschau nicht angezeigt
Lass dir nichts Wichtiges entgehen!







Programming in C and C++ Course: CH-230-A Jacobs University Bremen October 11th, 2022 Dr. Kinga Lipskoch
First Name: ....................................................................................
Last Name: ....................................................................................
Matriculation Number: ....................................................................................
Signature: .................................................................
Grade 5.0 4.7 4.3 4.
Grade 3.7 3.3 3.0 2.
Decimal Character Decimal Character Decimal Character
32 space 64 @ 96 ‘ 33! 65 A 97 a 34 ” 66 B 98 b 35 # 67 C 99 c 36 $ 68 D 100 d 37 % 69 E 101 e 38 & 70 F 102 f 39 ’ 71 G 103 g 40 ( 72 H 104 h 41 ) 73 I 105 i 42 * 74 J 106 j 43 + 75 K 107 k 44 , 76 L 108 l 45 - 77 M 109 m
#include
char ∗^ strcat(char ∗^ dest, const char ∗^ src);
char ∗^ strncat(char ∗^ dest, const char ∗^ src, size_t n);
char ∗^ strchr(const char ∗^ s, int c);
char ∗^ strstr(const char ∗^ searchin, const char ∗^ what);
int strcmp(const char ∗^ s1, const char ∗^ s2);
int strncmp(const char ∗^ s1, const char ∗^ s2, size_t n);
char ∗^ strcpy(char ∗^ dest, const char ∗^ src);
char ∗^ strncpy(char ∗^ dest, const char ∗^ src, size_t n);
size_t strlen(const char ∗^ s);
void ∗^ memset(void ∗^ s, int c, size_t n);
char ∗^ strdup(const char ∗^ s);
char ∗^ strtok(char ∗^ str, const char ∗^ delim);
#include
void exit(int __status);
void free(void ∗^ __ptr);
void ∗^ malloc(size_t __size);
void qsort(void ∗^ base, size_t nmemb, size_t size, int (∗^ compar)(const void ∗^ , const void ∗^ ));
logic_error(const string& what_arg);
logic_error(const char ∗^ what_arg);
exception() throw();
const char ∗^ what() const throw();
Language: C Please complete the following program fragment such that it prints n rows with the pattern below using nested loops.
int n; scanf("%d", &n); // add your code completion below
So if the user enters 6 for n, the following 6 rows should be printed:
A AB ABC ABCD ABCDE ABCDEF
Language: C Write a program which does the following:
a) reads a double from the keyboard,
b) reads a float from the keyboard,
c) reads an integer from the keyboard,
d) stores the product of these three values into the variable result, No information should be lost.
e) prints the value of result,
f) uses a pointer r ptr to add 5 to result,
g) prints the new values twice, once by using result, once by using r ptr.
Language: C Write a program where you first read an integer n, then n integers are read from the keyboard and stored in a dynamically allocated array. Then these numbers and their square should be written to a file named squares.txt in the opposite order of their input. So if the user enters
6 1 2 3 4 5 6
your output should look like in the following:
6 36 5 25 4 16 3 9 2 4 1 1
Language: C We want to record the time a runner completes a lap. Write the definition of the following function void total time(int mins[], int secs[], int n, int (^) *sum min, int *sum sec); The function accepts two integer vectors containing the lap times in minutes and seconds, and then calculates the sum of all times. The sum expressed in minutes and seconds is copied into two locations pointed by sum_min and sum_sec. You can assume that n is greater than 1. The seconds part should not be higher than 59 (thus convert the seconds to minutes if necessary after summing up).
Language: C Complete the following program fragment such that the program prints a triangle as described further below.
int n; char ch; scanf("%d", &n); getchar(); scanf("%c", &ch); // add your code completion below
Then the program prints a triangle with n rows, n columns using the character ch as illustrated in the testcase.
Language: C Consider (do not write) a program that creates a matrix of r rows and c columns and then ini- tializes it with values that are read from a file. Then the matrix is printed to the screen. Write a program fragment which reads the data from a file named matrix.dat, which contains the number of rows in the first line, the number of columns in the second line, then values are given for given rows and columns (see input structure and example below). Also implement and call a function to print the matrix where the prototype looks like this: void print_matrix(int ∗∗A, int rows, int cols)
The given input creates are 4 by 3 matrix and sets 1 , 1 to 3 and 2 , 2 to 5.
Language: C Consider (do not write) a program that reads strings from standard input (the keyboard) and adds them to the beginning of a linked list. Write a function struct node∗^ insertBegin(struct node ∗head, char str[]) that implements the insertion of elements into the list. Also write a function void printList(struct node ∗head) which prints the content of the list with spaces between the elements. Use the following data structure:
struct node { char str[20]; struct node∗^ next; };
Running the program could look like the following:
apple banana orange plum
For simplicity there is a space after the last string in each row.
apple banana apple orange banana apple plum orange banana apple
Language: C Consider the following data structure:
struct river { char name[40]; int length; int drainage_area; };
Write a program fragment that reads data from a file named data.txt and then sorts the rivers by length using the predefined qsort() function from stdlib.h. The result should be written to a file called output.txt. You will also need to implement a comparison function to make qsort() work correctly. You can assume that the struct above are part of your program, and that data.txt will not contain more than 30 entries. An example for the content of data.txt can be the following:
Nile 6650 334900 Amazon 6400 6915000 Yangtze 6300 1800000 Mississippi-Missouri 6275 2980000 Yenisei-Angara-Selenga 5539 2580000 Yellow 5464 745000 Ob-Irtysh 5410 2990000 Congo-Chambeshi 4700 3680000
Language: C A coffee machine is offering its services. Internally the orders by the users are handled via the following struct:
struct coffee { int id; char property; };
char∗^ title; int pages; public: Book(const Book&); Book(const char∗^ t, int p); Book(); // ... // setter and getter methods omitted };
a) Write an implementation of the copy constructor for Book
b) Normally a copy constructor will be automatically created by the compiler if none is pro- vided. In short words, what does the compiler-created copy constructor do, and why should the programmer provide its own version for the class above?
Language: C++ Consider the following class (complex.h):
class Complex { private: float real; // real part float imag; // imaginary part public: Complex(); Complex(float, float = 0); double magnitude(); void print(); };
and the according definition of the print method in this fragment of complex.cpp:
void Complex::print() { if (imag) cout << noshowpos << real << showpos << imag << "i" << endl; else cout << noshowpos << real << endl; }
Provide an overloaded << operator, which will replace the print function and creates the same output. Also overload the < and > operators so you may compare complex numbers via their magnitude (so just use a magnitude() method for comparison (which you do NOT need to implement)).
Add the declarations of the overloaded operators into complex.h above. Then just write down the definition below; it will be considered as part of the file complex.cpp.
Language: C++ Write an appropriate class for the program below such that the class declaration, definition and this main function will compile and run together.
int main() { worker a(234, "John McEnroe"); worker b(324, "Jack Nicholson");
cout << a << b; return 0; }
Language: C++
Model and write down classes and their relationships (no diagram needed) corresponding to the following entities: Object3D, Sphere, Cylinder, RectPrism (i.e., rectangular prism), and Cube. An Object3D is characterized by a name and the rest are additionally characterized by their mathematical properties. Make sure that the Object3D class is abstract with respect to a method called volume() which serves for computing and returning the volume of a 3D-object. Implement the parametric constructors for RectPrism and Cube only, and the volume() method for all the classes from above such that they will be consistent with the main() function from below. The formulas for computing the volumes of the 3D-objects are:
V (Sphere) =
· π · radius^3
V (Cylinder) = π · radius^2 · height
V (RectP rism) = width · length · height
V (Cube) = side^3
For π you can use the M_PI macro from cmath.
#include #include #include using namespace std;
// add your code below
int main(int argc, char ∗^ argv[]) { Object3D ∗^ arr[5]; arr[0] = new Sphere("sphere", 1.3); arr[1] = new RectPrism("rectprism", 2.0, 3.0, 4.0); arr[2] = new Cylinder("cylinder", 2.0, 1.3); arr[3] = new Cube("cube", 2.0); arr[4] = new RectPrism("rectprism2", 1.0, 2.0, 3.5); int i; for(i=0; i<5; i++) cout << arr[i]->volume() << endl; for(i=0; i<5; i++) delete arr[i]; return 0; }
Language: C++ Consider the program from below. Write down the exact output which this program would produce when running it.
#include using namespace std; class Person { protected: string name; int age; public: ˜Person() { cout << "Person destructor" << endl; } }; class Student : public virtual Person { protected: