Commit 7317a4f6 authored by Alija Sabic's avatar Alija Sabic
Browse files

Add exercises for 4th unit

parent 375f7e20
/*
============================================================================
Name : 04_arithmetic_mean.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Calculate the arithmetic mean of 20 freely chosen numbers.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 20
int main(void) {
double sum = 0;
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];
}
printf("The sum is %f and the arithmetic mean is %f", sum, sum/LENGTH);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_array_max.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/iJpaGp
============================================================================
*/
#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;
}
/*
============================================================================
Name : 04_christmas_tree.c
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
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define ROWS 10
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");
}
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_error_array.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Find and correct the mistake.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 8
int main(void)
{
int a[MAX_LEN], b[MAX_LEN];
for (int i = 0; i <= MAX_LEN; i++) {
a[i] = i;
b[i] = a[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;
}
/*
============================================================================
Name : 04_error_literal.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version :
Copyright : Free for all.
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 8
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]);
}
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_error_string.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Find and correct the mistake.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
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);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 04_odd_numbers.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.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MIN 1
#define MAX 15
int main(void) {
int array[MAX-MIN] = {};
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]);
}
}
return EXIT_SUCCESS;
}
/*
==============================================================================
Name : 04_pw_checker.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Password validation (min. length: 12, min. amount of numbers: 2)
==============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MIN_LEN 12
#define MIN_NUM 2
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");
}
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