Commit 0e87b171 authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Added example solutions for tasks 7.01 to 7.03

parent 46e9307c
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.01: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-04 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
unsigned int var1 = 4;
int var2 = 8.0; // The x.0 denotation is normally used for float/double
if (var1 == var2); // Semicolon after bracket ends the condition ...
printf("var1 equals var2\n"); // ... therefore this statement is always executed
return 0;
}
# Get the current path(s) (relevant directory on last position)
TASKPATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CHPATH := $(dir $(TASKPATH:%/=%))
# Variables for text substitution
empty:=
slash:= /
space:= $(empty) $(empty)
task:= task_
ch:= ch_
# Get TASK number
TASK := $(subst $(slash),$(space),$(TASKPATH))
TASK := $(lastword $(TASK))
TASK := $(subst $(task),$(empty),$(TASK))
# Get CH number
CH := $(subst $(slash),$(space),$(CHPATH))
CH := $(lastword $(CH))
CH := $(subst $(ch),$(empty),$(CH))
# Specify SRD directory
SRCDIR = .
# math.h is required for this example
INCS += -lm
# Call superior makefile
include ../../makefile
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.02: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-04 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
unsigned int input=0;
printf("Enter a number: ");
scanf("%i",input); // The address operator "&" is missing
do {
printf("Row row row your boat ...\n");
} while (--input >= 0); // An unsigned integer is always positive, hence, this condition results in an endless loop
return 0;
}
# Get the current path(s) (relevant directory on last position)
TASKPATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CHPATH := $(dir $(TASKPATH:%/=%))
# Variables for text substitution
empty:=
slash:= /
space:= $(empty) $(empty)
task:= task_
ch:= ch_
# Get TASK number
TASK := $(subst $(slash),$(space),$(TASKPATH))
TASK := $(lastword $(TASK))
TASK := $(subst $(task),$(empty),$(TASK))
# Get CH number
CH := $(subst $(slash),$(space),$(CHPATH))
CH := $(lastword $(CH))
CH := $(subst $(ch),$(empty),$(CH))
# Specify SRD directory
SRCDIR = .
# math.h is required for this example
INCS += -lm
# Call superior makefile
include ../../makefile
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.03: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-04 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include "stdio.h"
void swap (int* var1, int* var2) {
int temp = var1; // var1 is a pointer, therefore its value has to be accessed with *var1
var1 = var2; // same as above, the dereferencing of the pointers is missing
var2 = temp; // -"-
}
int main (int argc, char** argv) { // argc & argv are not used within this program
int value1 = 10;
int value2 = 15;
printf("Before: %d / %d\n",value1,value2);
swap(value1,value2); // Functions requires pointers, hence, the addresses of the variables have to be passed (&value1,&value2)
printf("After: %d / %d\n",value1,value2);
}
# Get the current path(s) (relevant directory on last position)
TASKPATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CHPATH := $(dir $(TASKPATH:%/=%))
# Variables for text substitution
empty:=
slash:= /
space:= $(empty) $(empty)
task:= task_
ch:= ch_
# Get TASK number
TASK := $(subst $(slash),$(space),$(TASKPATH))
TASK := $(lastword $(TASK))
TASK := $(subst $(task),$(empty),$(TASK))
# Get CH number
CH := $(subst $(slash),$(space),$(CHPATH))
CH := $(lastword $(CH))
CH := $(subst $(ch),$(empty),$(CH))
# Specify SRD directory
SRCDIR = .
# math.h is required for this example
INCS += -lm
# Call superior makefile
include ../../makefile
Markdown is supported
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