Commit 86e94f8c authored by Alija Sabic's avatar Alija Sabic
Browse files

Prepare problem statements and solutions of 4th unit for release

parent b58607fd
......@@ -19,11 +19,9 @@ int main(void) {
double numbers[LENGTH] = { 10, 0, 24, 32, 13, 12, 1, 234, 2, 1,
2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
for (int i = 0; i < LENGTH; i++) {
sum += numbers[i];
}
// TODO: calculate sum of array elements
printf("The sum is %f and the arithmetic mean is %f", sum, sum/LENGTH);
// TODO: calculate arithmetic mean by dividing the sum by the number of elements
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_arithmetic_mean_.c
Name : 04_arithmetic_mean_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
......@@ -19,11 +19,11 @@ int main(void) {
double numbers[LENGTH] = { 10, 0, 24, 32, 13, 12, 1, 234, 2, 1,
2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
// TODO: calculate sum
for (int i = 0; i < LENGTH; i++) {
sum += numbers[i];
}
// TODO: calculate arithmetic mean
// TODO: Inform the user
printf("The sum is %f and the arithmetic mean is %f", sum, sum/LENGTH);
return EXIT_SUCCESS;
}
......@@ -5,7 +5,6 @@
Version : 0.1
Copyright : Free for all.
Description : Print the maximum value of the array.
Visualize execution at pythontutor.com: https://goo.gl/iJpaGp
============================================================================
*/
......@@ -16,14 +15,9 @@ int main(void) {
int a[] = { 1, 25, 413, 2, 12, 0, 12, 10, 99 };
int max = 0;
for (int i = 0; i < 9; i++) {
if (a[i] > max) {
max = a[i];
}
}
// TODO: get the maximum value of array a
printf("The maximum is: %d\n", max);
// TODO: inform the user
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_array_max_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Print the maximum value of the array.
Visualize execution at pythontutor.com: https://goo.gl/hhj13F
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a[] = { 1, 25, 413, 2, 12, 0, 12, 10, 99 };
int max = 0;
for (int i = 0; i < 9; i++) {
if (a[i] > max) {
max = a[i];
}
}
printf("The maximum is: %d\n", max);
return EXIT_SUCCESS;
}
......@@ -4,8 +4,7 @@
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Print a Christmas tree.
Visualize execution at pythontutor.com: https://goo.gl/8xptCE
Description : Print a Christmas tree with nested for loops.
============================================================================
*/
......@@ -16,15 +15,19 @@
int main(void) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < ROWS - i; j++) {
printf(" ");
}
for (int k = 0; k < (2 * i + 1); k++) {
printf("*");
}
printf("\n");
}
// TODO: print a Christmas tree to the console
// using nested for loops.
// *
// ***
// *****
// *******
// *********
// ***********
// *************
// ***************
// *****************
// *******************
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_christmas_tree_.c
Name : 04_christmas_tree_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Print a Christmas tree.
Description : Print a Christmas tree with nested for loops.
Visualize execution at pythontutor.com: https://goo.gl/8xptCE
============================================================================
*/
......@@ -16,19 +16,15 @@
int main(void) {
// TODO: print a Christmas tree to the console
// with nested for loops.
// *
// ***
// *****
// *******
// *********
// ***********
// *************
// ***************
// *****************
// *******************
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < ROWS - i; j++) {
printf(" ");
}
for (int k = 0; k < (2 * i + 1); k++) {
printf("*");
}
printf("\n");
}
return EXIT_SUCCESS;
}
......@@ -15,11 +15,13 @@
int main(void)
{
int a[MAX_LEN], b[MAX_LEN];
int a[MAX_LEN] = {};
int b[MAX_LEN] = {};
for (int i = 0; i <= MAX_LEN; i++) {
a[i] = i;
b[i] = a[i];
b[i] = i;
}
printf("a[] = { %d, %d, %d, %d, %d, %d, %d, %d }\n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
......
/*
============================================================================
Name : 04_error_array_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Find and correct the mistake. Solution.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 8
int main(void)
{
int a[MAX_LEN] = {};
int b[MAX_LEN] = {};
// off-by-one, array out of bounds error
for (int i = 0; i < MAX_LEN; i++) {
a[i] = i;
b[i] = i;
}
printf("a[] = { %d, %d, %d, %d, %d, %d, %d, %d }\n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
printf("b[] = { %d, %d, %d, %d, %d, %d, %d, %d }\n", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
return EXIT_SUCCESS;
}
......@@ -2,9 +2,9 @@
============================================================================
Name : 04_error_literal.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version :
Version : 0.1
Copyright : Free for all.
Description : Hello World in C, Ansi-style
Description : Find and correct the mistake.
============================================================================
*/
......@@ -15,10 +15,12 @@
int main(void)
{
int c[MAX_LEN] = { '1', '2', '3', '4', '5', '6', '7', '8' };
for (int i = 0; i < MAX_LEN; i++) {
printf("c[%d] = %d\n", c[i]);
printf("c[%d] = %c\n", c[i]);
}
return EXIT_SUCCESS;
......
/*
============================================================================
Name : 04_error_literal_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Find and correct the mistake. Solution.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 8
int main(void)
{
// Integer array with int literals
int c[MAX_LEN] = { 1, 2, 3, 4, 5, 6, 7, 8 };
// Two int variables for two int placeholders.
for (int i = 0; i < MAX_LEN; i++) {
printf("c[%d] = %d\n", i, c[i]);
}
return EXIT_SUCCESS;
}
......@@ -13,10 +13,11 @@
int main(void)
{
char max[3] = { 'M', 'a', 'x' };
char moritz[6] = { 'M', 'o', 'r', 'i', 't', 'z' };
printf("%s und %s\n\n", max, moritz);
printf("%s und %s\n", max, moritz);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_odd_numbers.c
Name : 04_error_string.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Store numbers from MIN to MAX and output only even numbers.
Description : Find and correct the mistake.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MIN 1
#define MAX 15
int main(void)
{
// use string literals or add null terminator manually
char max[] = "Max";
char moritz[] = "Moritz";
int main(void) {
// TODO: fill an array with values from 1 to 15.
// TODO: output only odd numbers of the array.
printf("%s und %s\n", max, moritz);
return EXIT_SUCCESS;
}
......@@ -4,7 +4,7 @@
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Store numbers from MIN to MAX and output only even numbers.
Description : Store numbers from MIN to MAX and output only odd numbers.
============================================================================
*/
......@@ -16,15 +16,9 @@
int main(void) {
int array[MAX-MIN] = {};
// TODO: fill an array with values from 1 to 15 (MIN to MAX).
for (int i = MIN; i <= MAX; i++) {
array[i-MIN] = i;
if ((array[i-MIN] % 2) == 0) {
printf("a[%d]\t= %d\n", i-MIN, array[i-MIN]);
}
}
// TODO: output only odd numbers of the array.
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_odd_numbers_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Store numbers from MIN to MAX and output only odd numbers.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MIN 1
#define MAX 15
int main(void) {
int array[MAX-MIN] = {};
// Iterate over all array elements
for (int i = 0; i < (MAX-MIN); i++) {
// Store value starting with MIN
array[i] = i + MIN;
// Output only odd numbers
if ((array[i] % 2) == 1) {
printf("a[%d]\t= %d\n", i, array[i]);
}
}
return EXIT_SUCCESS;
}
......@@ -20,29 +20,11 @@ int main(void) {
// input
char *secret_password = "hase1234xy!#";
// get length of password
int length = 0;
for (; secret_password[length] != '\0'; length++);
// get amount of numbers
int numbers = 0;
for (int i = 0; i < length; i++) {
if (isdigit(secret_password[i])) {
numbers++;
}
}
// output
printf("length = %d\n", length);
printf("numbers = %d\n", numbers);
// validate & output
if (length >= MIN_LEN && numbers >= MIN_NUM) {
printf("Entered password is valid.\n");
}
else {
printf("Entered password is not valid.\n");
}
// TODO: get length of password
// TODO: get amount of numbers in password
// TODO: validate & output
return EXIT_SUCCESS;
}
/*
==============================================================================
Name : 04_pw_checker_.c
Name : 04_pw_checker_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
......@@ -20,12 +20,29 @@ int main(void) {
// input
char *secret_password = "hase1234xy!#";
// TODO: get length of password
// TODO: get amount of numbers, either by
// using isdigit() or by char comparison
// TODO: validate and inform the user
// get length of password
int length = 0;
for (; secret_password[length] != '\0'; length++);
// get amount of numbers
int numbers = 0;
for (int i = 0; i < length; i++) {
if (isdigit(secret_password[i])) {
numbers++;
}
}
// output
printf("length = %d\n", length);
printf("numbers = %d\n", numbers);
// validate & output
if ((length >= MIN_LEN) && (numbers >= MIN_NUM)) {
printf("Entered password is valid.\n");
}
else {
printf("Entered password is not valid.\n");
}
return EXIT_SUCCESS;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment