Skip to content

Commit 2532748

Browse files
committed
Add week9 homework
1 parent 3b498ea commit 2532748

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed

weeks/week9/cheatsheet.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
## Compilation
2+
3+
After setting up your IDE (VS Code, JetBrains CLion, Vim are among many
4+
of the options available) and having your program ready, you have to compile it
5+
(i.e. turn the text of the program into a machine code which your computer will
6+
be able to run later).
7+
8+
This can be done like this, in your terminal:
9+
```
10+
gcc programname.c -o outputname
11+
```
12+
13+
If there were errors or notes during the compilation process, the compiler
14+
(here we are using the GCC compiler) will notify you. If the program compiled
15+
correctly, you can later run the generated executable:
16+
```
17+
./outputname
18+
```
19+
20+
## General tutorial
21+
22+
### Code
23+
24+
Code for execution goes into files with “.c” suffix. Shared decl’s
25+
(included using #include “mylib.h”) in “header” files, end in “.h”
26+
27+
### Comments
28+
29+
Characters to the right of `//` are not interpreted; they’re a comment.
30+
Text between `/*` and `*/` (possibly across lines) is commented out as well.
31+
32+
### Data types
33+
34+
* `char` - an ASCII value: e.g. ‘a’ (see: man ascii)
35+
* `int` - a signed integer: e.g. 97 or hex 0x61, oct 0x141
36+
* `float` - a floating-point (possibly fractional) value
37+
* `double` - a double length float
38+
39+
`char`, `int`, and `double` are most frequently and easily used in small
40+
programs.
41+
42+
sizeof(double) computes the size of a double in bytes.
43+
44+
Zero values represent logical false, nonzero values are logical true.
45+
46+
### Functions
47+
48+
A function is a pointer to some code, parameterized by formal parameters,
49+
that may be executed by providing actual parameters. Functions must be
50+
declared before they are used, but code may be provided later.
51+
52+
A `sqrt` function for positive `n` might be declared as:
53+
54+
```
55+
int addNumbers(int a, int b) // function definition with return type, name and parameters
56+
{
57+
int result;
58+
result = a+b;
59+
return result; // return statement
60+
}
61+
Functions that do not return anything return `void`.
62+
There must always be a main function that returns an int:
63+
```c
64+
int main()
65+
{
66+
return 0;
67+
}
68+
```
69+
70+
### Statements
71+
72+
Angle brackets identify syntactic elements and don’t appear in real
73+
statements
74+
75+
```c
76+
<expression> ; //semicolon indicates end of a simple statement
77+
break; //quits the tightest loop or switch immediately
78+
continue; //jumps to next loop test, skipping rest of loop body
79+
return x; //quits this function, returns x as value
80+
if (<condition>) <stmt>! //stmt executed if cond true (nonzero)
81+
if (<condition>) <stmt> else <stmt> // two-way condition
82+
while (<condition>) <stmt> //repeatedly execute stmt only if condition true
83+
do <stmt> while (<condition>); //note the semicolon, executes at least once
84+
for (<init>; <condition>; <step>) { <statements> }
85+
```
86+
87+
### Includes
88+
89+
The homework requires you to include several header files with the needed functions:
90+
91+
#### I/O (`#include <stdio.h>`)
92+
Default input comes from “stdin”; output goes to “stdout”; errors to “stderr”.
93+
Standard input and output routines are declared in `stdio.h`: `#include <stdio.h>`
94+
* `scanf(p,...)` - reads ... args using format p (below);
95+
* `printf(p, ...)` - write ... args using format p (below); pass args as is fprintf(f,p,...)
96+
97+
Format specifiers:
98+
`%c` - character
99+
`%d` - decimal integer
100+
`%s` - string
101+
`%f` - float
102+
103+
#### MEMORY (`#include <stdlib.h>`)
104+
* `malloc(n)` - allocates `n` bytes of memory; (for type T: `p = (T*)malloc`)

weeks/week9/homework.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
There is a short introduction and cheat sheat [available here](./cheatsheet.md), which explains
2+
the basics of the C programming language and its compilation process on Linux.
3+
4+
5+
## Problem 1 (Mario)
6+
7+
Let's recreate the pyramids from Super Mario in text:
8+
```c
9+
#
10+
##
11+
###
12+
####
13+
#####
14+
######
15+
```
16+
17+
So, the program should prompt the user to input the height of the pyramid
18+
and then output the pyramid of the specified height:
19+
```c
20+
$ ./mario
21+
Height: 6
22+
#
23+
##
24+
###
25+
####
26+
#####
27+
######
28+
```
29+
30+
Other examples:
31+
```c
32+
$ ./mario
33+
Height: 2
34+
#
35+
##
36+
```
37+
```c
38+
$ ./mario
39+
Height: 1
40+
#
41+
```
42+
43+
## Problem 2
44+
45+
Create a program that takes two integer numbers as an input from the user
46+
(a ≤ b) and for every number in the interval [a; b] outputs:
47+
48+
* the number in plain text english if it's less than 10
49+
* otherwise, whether it's `odd` or `even`
50+
51+
```c
52+
$ ./interval
53+
Input a: 8
54+
Input b: 11
55+
56+
eight
57+
nine
58+
even
59+
odd
60+
```
61+
62+
Other examples:
63+
```c
64+
$ ./interval
65+
Input a: 13
66+
Input b: 15
67+
68+
odd
69+
even
70+
odd
71+
```
72+
```c
73+
$ ./interval
74+
Input a: 1
75+
Input b: 4
76+
77+
one
78+
two
79+
three
80+
four
81+
```
82+
83+
## Problem 3
84+
85+
Develop a program which is going to output English text according to these rules:
86+
* First, all the whitespace are deleted
87+
* Let the length of the text taken as an input be `L`.
88+
Then, output the text as a table with the number of rows and columns, where
89+
`[√L] ≤ rows ≤ columns ≤ [√L] + 1` ([x] is an integer part of the number)
90+
91+
For example, the message `capital is an abstract parasite, an insatiable
92+
vampire and zombie maker; but the living flesh it converts into dead labor
93+
is ours, and the zombies it makes are us` has a length of 134 symbols without
94+
the whitespace, we can output it in a table 12 x 12.
95+
96+
```c
97+
capitalisan
98+
abstractpar
99+
asite,anins
100+
atiablevamp
101+
ireandzombi
102+
emaker;butt
103+
helivingfle
104+
shitconvert
105+
sintodeadla
106+
borisours,a
107+
ndthezombie
108+
sitmakesare
109+
us
110+
```
111+
112+
You have to check whether `rows × columns ≥ L` and if there are several
113+
possible rectangles choose the one with the smaller area.
114+
115+
Other examples:
116+
```c
117+
$ ./table
118+
Input your text: it is time that lived moments replace the dead memory that has stamped acquaintance with the hidden restriction that nothing can ever be experienced.
119+
120+
Characters total: 126
121+
Rows: 11
122+
Columns: 12
123+
124+
itistimethat
125+
livedmoments
126+
replacethede
127+
admemorythat
128+
hasstampedac
129+
quaintancewi
130+
ththehiddenr
131+
estrictionth
132+
atnothingcan
133+
everbeexperi
134+
enced.
135+
```
136+
137+
## Problem 4
138+
139+
Develop a program which, taking two integer arrays as its input will
140+
create a new integer array. The new array consists of the digits of the sum of the original
141+
two arrays' elements.
142+
143+
So, for the example if given two arrays {23, 5, 2, 7, 87} and {4, 67, 2, 8},
144+
the new array will look like this {2, 7, 7, 2, 4, 1, 5, 8, 7}.
145+
146+
If the array is empty, just consider the respective elements as zeros.
147+
Then, if given two arrays { } and {4, 67, 3, 8} then the new array will look
148+
like {4, 6, 7, 3, 8}.
149+
150+
An example usage:
151+
```
152+
$ ./arrays
153+
Input the length of the first array: 5
154+
Input the numbers: 5
155+
23
156+
5
157+
2
158+
7
159+
87
160+
161+
Input the length of the second array: 5
162+
Input the numbers: 4
163+
4
164+
67
165+
2
166+
8
167+
168+
The output array:
169+
2 7 7 2 4 1 5 8 7
170+
```

0 commit comments

Comments
 (0)