Commit 946e8688 authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Added example solutions for tasks 7.11 to 7.15

parent e65dd10f
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.11: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
char input;
char info;
printf("Please enter a character: ");
scanf("%s",input); // %s is used for strings, for single characters use %c (requires to pass the address of the variable with '&')
printf("Do you want information on the character [y/n]: ");
scanf("%s",info); // %s is used for strings, for single characters use %c (requires to pass the address of the variable with '&')
if (info == "y") // Better use brackets for if statements ...
if ((input >= 'a') && (input <= 'z'))
printf("You entered a small letter!\n");
else if ((input >= 'A') && (input <= 'Z'))
printf("You entered a capital letter!\n");
else // ... because this statement is evaluated as else branch of the inner if-condition
printf("You do not want info on your input!\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
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.12: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
#define VALUES 10
int main (int argc, char** argv) { // argc & argv are not used within this program
float values[VALUES] = { 0 };
float average;
for (int i=1; i <= VALUES; i++) { // out of array indexes: valid indexes are 0 .. (VALUES-1)
printf("Enter %d. value: ",i);
scanf("%d",&values[i]); // to read in float values use %f; %d reads in integers
}
for (int i=1; i <= VALUES; i++) // out of array indexes: valid indexes are 0 .. (VALUES-1)
average += values[i]; // average has no initial value, so its content is undefined
average /= VALUES;
printf("Average values is %3.2f\n",average);
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.13: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is NOT compilable! *
* *
******************************************************************************/
#include <stdio.h>
// for strcmp() the library <string.h> is required
int main (int argc, char** argv) { // argc & argv are not used within this program
char* string1 = "Testikus";
char string2[8] = "Testikus"; // array is too small, since it has to hold 8 characters + '\0' (=9)
if (strcmp(string1,string2) == 1) // opening bracket { is missing // strcmp returns 0 in case of match
printf("Match!\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
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.14: Program Syntax & Semantic *
* Author: Dominik Widhalm *
* Email: dominik.widhalm@technikum-wien.at *
* Date: 2017-09-05 *
* *
* Note: Source is compilable! *
* *
******************************************************************************/
#include <stdio.h>
int main (int argc, char** argv) { // argc & argv are not used within this program
unsigned int var1 = 3;
int var2 = 3;
float var3 = 3; // float numbers are better written as 3.0
if (var1 == var2 == var3) { // three-way comparison is not possible in C
// additionally comparison of int and float is unsafe
printf("var1 equals var2 and var3!\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
/******************************************************************************
* C PROGRAMMING *
* BASIC EXERCISES - EXAMPLE SOLUTIONS *
* *
* Task_7.15: 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 count = 0, input = 0; sum = 0; // ERROR: semicolon between input and sum, instead of colon
printf("How many numbers to you want to sum up: ");
scanf("%d",&count);
printf("Enter %d numbers now:\n",count);
while (--count > 0) // missing brackets for loop body ...
scanf("%d",&input); // ... therefore only this statement will be looped ...
sum += input; // ... while this statement is only executed once after leaving the loop
printf("The sum of all numbers equals %d\n",sum);
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