Commit 672c2f3e authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Added example solutions for tasks 7.04 to 7.10

parent 0e87b171
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.04: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
int average (int val1, int val2) { return val1 + val2 / 2; } // function returns int, but result of calculation may be with decimal places
// additionally it is advisable to always provide function prototypes
int main (int argc, char** argv) { // argc & argv are not used within this program
char number1 = 15;
int number2 = 42;
printf("Average of %d and %%d equals %d\n", \
number1,number2,average(number1,number2)); // there is a '%' too much (in printf the combination '%%' results in '%'),
// therefore printf only finds 2 placeholder, but has been given 3 variables
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.05: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is NOT compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
char answer = "n"; // "" denotes a string (appending '\0'), for a single char '' has to be used
char maoam[40];
printf("Do you want an apple [y/n]? "; // ERROR: bracket at the end is missing
scanf("%c",&answer);
if (answer == 'n') {
printf("What do you want then? ");
scanf("%s",maoam);
}
} // main function has return type int, but return statement is missing
# 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.06: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is NOT compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
char animal[] = 'frog'; // strings are denoted with "", '' are only applicable for single character
int count = sizeof(animal);
for (int i=0; i <= count; i++); // semicolon after loop head ends the loop ...
printf("%d letter: %c\n",i,*animal+i); // ... therefore this statement is executed only once
// loop head run condition has an off-by-one error, since the maximum index of animal is (count-1) ...
// ... but sizeof returns 5 since it also counts the '\0', so the maximum index here should be (count-2)
// the dereferencing operator binds stronger than addition, so this constellation should be *(animal+i)
// ERROR: additionally the variable i is only valid inside the loop, so it is not known in this case
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.07: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
char *string1, *string2;
string1 = malloc(25);
strcpy(string1, "You shall not parse!");
printf("String1 = %s\n",string1);
free(string1);
string2 = malloc(25);
memcpy(string2,string1,strlen(string1)); // memory of string1 has already been freed, so it cannot be used here
printf("String2 = %s\n",20); // format modifier %s expects a string, or rather a pointer to a string, not a number
// free() of string2 is missing
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.08: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
// required includes are missing (i.e., <stdio.h>)
int* get_double (int value) { // it is advisable to always provide function prototypes
value *= 2;
return &value; // the memory location of value is automatically freed after leaving the function
// therefore its address is invalid afterwards
}
int main (int argc, char** argv) { // argc & argv are not used within this program
int number = 42;
int* number2x = get_double(number);
printf("Double a value using an own function!\n");
printf("The double of %d is %d!\n",number,*number2x);
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.09: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
int check = 0;
void check_input (int value, int guess) { // it is advisable to always provide function prototypes
int check = value == guess ? 1 : 0; // declaration of local variable check shadows global variable
return;
}
int main (int argc, char** argv) { // argc & argv are not used within this program
int number = 42;
int input = 0;
do {
printf("Guess a number: ");
scanf("d",&input);
check_input(number,input);
} while(check = 0); // second = is missing (for comparison)
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.10: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is NOT compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
int grade;
printf("Please enter a grade (1-5): ");
scanf(%d,&grade); // ERROR: "" are missing for format string
if (grade > 0 || grade <= 5) { // condition is always true; right operator is "&&"
switch (grade) {
case 1: printf("Excellent!\n"); // break after statement is missing
case 2: printf("Very Good!\n"); // break after statement is missing
case 3: printf("Good!\n"); // break after statement is missing
case 4: printf("Improvement Needed!\n"); // break after statement is missing
default: printf("Fail!\n"); // (optional) break after statement is missing
}
} else {
printf("Input was not a valid grade!\n");
}
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
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