Commit 75ccb771 authored by Alija Sabic's avatar Alija Sabic
Browse files

Add solutions for exercises of 5th unit

parent 73d3f3c7
/*
============================================================================
Name : 05_floating_average_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Calculate floating average.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
void floating_average(double *avg, int value);
int main(void) {
int vector[] = { 1, 2, 3, 4, 5, 6 };
double avg = 0;
for (int i = 0; i < 6; i++) {
floating_average(&avg, vector[i]);
printf("Floating Average: %lf\n", avg);
}
return EXIT_SUCCESS;
}
void floating_average(double *avg, int value) {
*avg = (*avg + value) / 2;
}
/*
============================================================================
Name : 05_initialize_array_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Array initialization.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define LEN 6
void init_array(int* a, int length);
void init_array_val(int* a, int length, int value);
int main(void) {
int buf[LEN], buf2[LEN];
init_array(buf, LEN);
init_array_val(buf2, LEN, 42);
printf("==== 1. Buffer ====\n");
// TODO: reuse
for(int i = 0; i < LEN; i++) {
printf("%d. element\t%d\n", i, buf[i]);
}
printf("==== 2. Buffer ====\n");
for(int i = 0; i < LEN; i++) {
printf("%d. element\t%d\n", i, buf2[i]);
}
return EXIT_SUCCESS;
}
void init_array(int* a, int length) {
init_array_val(a, length, 0);
}
void init_array_val(int* a, int length, int value) {
for (int i = 0; i < length; i++) {
a[i] = value;
}
}
......@@ -26,10 +26,29 @@
int main(void) {
// TODO: Integer
int i = 3;
int* pI = &i;
int** ppI = &pI;
printf("ppI: addr=%p, value=%p\n", &ppI, ppI);
printf("pI: addr=%p, value=%p\n", &pI, pI);
printf("i: addr=%p, value=%d \n\n", &i, i);
// TODO: Character
char c = 'a';
char *pC = &c;
char **ppC = & pC;
printf("c : Addre= %p \t Wert= %c\n", pC, c);
printf("pC : Addre= %p \t Wert= %p\n", ppC, *ppC);
printf("ppC : Addre= %p \t Wert= %p\n\n", &ppC, ppC);
// TODO: Float
float f = 2.54321, *pF = &f, **ppF = &pF;
printf("f: addr = %p, wert = %f\n", &f, f);
printf("pF: addr = %p, wert = %p\n", &pF, pF);
printf("ppF: addr = %p, wert = %p\n\n", &ppF, ppF);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 05_properties_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Variable, pointer to Variable, and pointer to pointer.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// Integer
int i = 2;
int* pI = &i;
int** ppI = &pI;
printf("&ppI=%p, ppI=%p\n", &ppI, ppI);
printf("&pI=%p, pI=%p\n", &pI, pI);
printf("&i=%p, i=%d\n\n", &i, i);
// Character
char c = 'a';
char* pC = &c;
char** ppC = &pC;
printf("&ppC=%p, ppC=%p\n", &ppC, ppC);
printf("&pC=%p, pC=%p\n", &pC, pC);
printf("&c=%p, c=%c\n\n", &c, c);
// Float
float f = 2.5;
float* pF = &f;
float** ppF = &pF;
printf("&ppF=%p, ppF=%p\n", &ppF, ppF);
printf("&pF=%p, pF=%p\n", &pF, pF);
printf("&f=%p, f=%d\n\n", &f, f);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 05_reverse_array_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Reverse values of integer arrays.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
void reverse(int* a, int length);
void print(int* a, int length);
int main(void) {
int vector[] = { 0, 1, 2, 3, 4, 5, 6 };
printf("Initial\n");
print(vector, 7);
reverse(vector, 7);
printf("reverse()\n");
print(vector, 7);
int len = sizeof(vector)/sizeof(int);
reverse(vector, len);
printf("reverse()\n");
print(vector, len);
return EXIT_SUCCESS;
}
void reverse(int* a, int length) {
for (int i = 0; i <= (length/2); i++)
{
int h = a[i];
a[i] = a[length-i-1];
a[length-i-1] = h;
}
}
void print(int* a, int length) {
for(int i = 0; i < length; i++) {
printf("%d. element\t%d\n", i, a[i]);
}
printf("\n");
}
/*
============================================================================
Name : 05_reverse_string_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Print strings in reverse order.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
void print_reverse(char* text);
int main(void) {
char input[] = "Hello World";
print_reverse(input);
return EXIT_SUCCESS;
}
void print_reverse(char* text) {
int length = 0;
for (; text[length] != '\0'; length++);
for(; length != 0; length--) {
printf("%c", text[length-1]);
}
printf("\n");
}
/*
============================================================================
Name : 05_string_to_double_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : String to double conversion.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int string_to_double(char *input, double *output);
int main(void) {
char *input = "1.23s";
double val = 0;
if (string_to_double(input, &val) == 0) {
printf("The entry `%s` equals to %lf\n", input, val);
}
return EXIT_SUCCESS;
}
int string_to_double(char *input, double *output)
{
char *endptr;
int conv_status = -1;
*output = strtod(input, &endptr);
// Error handling (details cf. manpage `strtod()` and `strtol()`).
if (endptr == input) {
printf("No digits were found\n");
}
else
{
if (*endptr != '\0')
{
printf("Further characters after number: %s\n", endptr);
}
conv_status = 0;
}
return conv_status;
}
......@@ -15,6 +15,7 @@
* Implement a function, that exchanges the values of two integer
* variables, using pointers (references).
*/
void swap (int* a, int* b);
int main(void) {
......@@ -25,7 +26,16 @@ int main(void) {
// TODO: swap values
swap (&a, &b);
printf("a = %d, b = %d\n", a, b);
return EXIT_SUCCESS;
}
void swap (int* x, int* y) {
int tausch;
tausch = *x;
*x = *y;
*y = tausch;
}
/*
============================================================================
Name : 05_swap_pointer_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Swap two integer pointers using pointers-to-pointer.
============================================================================
*/
// call-by-value: https://goo.gl/oWvJV1 (wrong)
// call-by-reference: https://goo.gl/teBR8h (correct)
#include <stdio.h>
#include <stdlib.h>
void swap(int** a, int** b);
int main() {
int a = 2;
int b = 4;
int* pA = &a;
int* pB = &b;
printf("a = %d, b = %d\n", *pA, *pB);
swap(&pA, &pB);
printf("a = %d, b = %d\n", *pA, *pB);
return EXIT_SUCCESS;
}
void swap(int** a, int** b) {
int* h = *a;
*a = *b;
*b = h;
}
/*
============================================================================
Name : 05_swap_solution.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Swap two integer values using pointers.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
// TODO: call-by-value, call-by_reference
// call-by-value: https://goo.gl/aZFj1U (wrong)
// call-by-reference: https://goo.gl/imGvm8 (correct)
void swap(int* a, int* b);
int main(void) {
int a = 2;
int b = 4;
printf("a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return EXIT_SUCCESS;
}
void swap(int* a, int* b) {
int h = *a;
*a = *b;
*b = h;
}
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