From 34e4cb489a9ff453596af1a8336c822e20da2fa0 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Sat, 2 Nov 2019 07:34:37 +0100 Subject: [PATCH 01/16] Only transfer, no pull request --- helloworld/H15_E1.c | 32 +++++++++++++++++++++++ helloworld/H15_E2.c | 4 +++ helloworld/H15_E3.c | 41 +++++++++++++++++++++++++++++ helloworld/H15_E4.c | 42 +++++++++++++++++++++++++++++ helloworld/H16_E1.c | 49 ++++++++++++++++++++++++++++++++++ helloworld/H17_E1.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ helloworld/H18_E1.c | 39 +++++++++++++++++++++++++++ 7 files changed, 271 insertions(+) create mode 100644 helloworld/H15_E1.c create mode 100644 helloworld/H15_E2.c create mode 100644 helloworld/H15_E3.c create mode 100644 helloworld/H15_E4.c create mode 100644 helloworld/H16_E1.c create mode 100644 helloworld/H17_E1.c create mode 100644 helloworld/H18_E1.c diff --git a/helloworld/H15_E1.c b/helloworld/H15_E1.c new file mode 100644 index 0000000..2032200 --- /dev/null +++ b/helloworld/H15_E1.c @@ -0,0 +1,32 @@ +//1. Rewrite the program in Listing 15.2. This time use the format specifier %c, instead +//of %s, to print out the character string of the local time on your computer. + +#include +3: #include +4: +5: void GetDateTime(void); +6: +7: main() +8: { +9: printf("Before the GetDateTime() function is called.\n"); +10: GetDateTime(); +11: printf("After the GetDateTime() function is called.\n"); +12: return 0; +13: } +14: /* GetDateTime() definition */ +15: void GetDateTime(void) +16: { +17: time_t now; +18: +19: printf("Within GetDateTime().\n"); +20: time(&now); +21: printf("Current date and time is: %s\n", +22: asctime(localtime(&now))); +23: } + + + + + + + diff --git a/helloworld/H15_E2.c b/helloworld/H15_E2.c new file mode 100644 index 0000000..48fb9b9 --- /dev/null +++ b/helloworld/H15_E2.c @@ -0,0 +1,4 @@ +//2. Declare and define a function, called MultiTwo(), that can perform multiplication +//on two integer variables. Call the MultiTwo() function from the main() function +//and pass two integers to MultiTwo(). Then print out the result returned by the +//MultiTwo() function on the screen. diff --git a/helloworld/H15_E3.c b/helloworld/H15_E3.c new file mode 100644 index 0000000..11f4345 --- /dev/null +++ b/helloworld/H15_E3.c @@ -0,0 +1,41 @@ +//3. Rewrite the program in Listing 15.3. This time, make a function that takes a variable number of int arguments and performs the operation of multiplication on +//these arguments. + +#include +#include + +double AddDouble(int x, ...); +main () { + double d1 = 1.5; + double d2 = 2.5; + double d3 = 3.5; + double d4 = 4.5; + + printf("Given an argument: %2.1f\n", d1); + printf("The result returned by AddDouble() is: %2.1f\n\n", + AddDouble(1, d1)); + printf("Given arguments: %2.1f and %2.1f\n", d1, d2); + printf("The result returned by AddDouble() is: %2.1f\n\n", + AddDouble(2, d1, d2)); + printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3); + printf("The result returned by AddDouble() is: %2.1f\n\n", + AddDouble(3, d1, d2, d3)); + printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n", d1, d2, d3, d4); + printf("The result returned by AddDouble() is: %2.1f\n", + AddDouble(4, d1, d2, d3, d4)); + return 0; + } + /* definition of AddDouble() */ + double AddDouble(int x, ...) + { + va_list arglist; + int i; + double result = 0.0; + + printf("The number of arguments is: %d\n", x); + va_start (arglist, x); + for (i=0; i +#include + +double AddDouble(int x, ...); +main () { + double d1 = 1.5; + double d2 = 2.5; + double d3 = 3.5; + double d4 = 4.5; + + printf("Given an argument: %2.1f\n", d1); + printf("The result returned by AddDouble() is: %2.1f\n\n", + AddDouble(1, d1)); + printf("Given arguments: %2.1f and %2.1f\n", d1, d2); + printf("The result returned by AddDouble() is: %2.1f\n\n", + AddDouble(2, d1, d2)); + printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3); + printf("The result returned by AddDouble() is: %2.1f\n\n", + AddDouble(3, d1, d2, d3)); + printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n", d1, d2, d3, d4); + printf("The result returned by AddDouble() is: %2.1f\n", + AddDouble(4, d1, d2, d3, d4)); + return 0; + } + /* definition of AddDouble() */ + double AddDouble(int x, ...) + { + va_list arglist; + int i; + double result = 0.0; + + printf("The number of arguments is: %d\n", x); + va_start (arglist, x); + for (i=0; i +/* function declarations */ +void StrPrint1(char **str1, int size); +void StrPrint2(char *str2); + +/* main() function */ +main() { + char *str[4] = {"There's music in the sighing of a reed;", + "There's music in the gushing of a rill;", + "There's music in all things if men had ears;", + "There earth is but an echo of the spheres.\n" + }; + int i, size = 4; + + StrPrint1(str, size); + for (i=0; i +#include +#include +/* function declaration */ +void StrCopy(char *str1, char *str2); +/* main() function */ +main() { + char *str[4] = {"There's music in the sighing of a reed;", + "There's music in the gushing of a rill;", + "There's music in all things if men had ears;", + "There earth is but an echo of the spheres.\n" + }; + char *ptr; + int i; + + int termination = 0; + + ptr = malloc((strlen(str[0]) + 1) * sizeof(char)); + if (ptr == NULL){ + printf("malloc() failed.\n"); + termination = 1; + } + else{ + StrCopy(str[0], ptr); + printf("%s\n", ptr); + for (i=1; i<4; i++){ + ptr = realloc(ptr, (strlen(str[i]) + 1) * sizeof(char)); + if (ptr == NULL){ + printf("realloc() failed.\n"); + termination = 1; + i = 4; /* break the fro loop */ + } + else{ + StrCopy(str[i], ptr); + printf("%s\n", ptr); + } + } + } + free(ptr); + return termination; + } + /* funciton definition */ + void StrCopy(char *str1, char *str2) + { + int i; + + for (i=0; str1[i]; i++) + str2[i] = str1[i]; + str2[i] = ‘\0'; + } \ No newline at end of file diff --git a/helloworld/H18_E1.c b/helloworld/H18_E1.c new file mode 100644 index 0000000..a1a6d96 --- /dev/null +++ b/helloworld/H18_E1.c @@ -0,0 +1,39 @@ +//1. Write a program to print out the values represented by the enumerated names +//declared in Quiz question 2 in this hour. +//2. Given the following declarations: +//typedef char WORD; +//typedef int SHORT; +//typedef long LONG; +//typedef float FLOAT; +//typedef double DFLOAT; +//write a program to measure the sizes of the synonyms to the data types. +//3. Rewrite the program in Listing 18.4. This time, add integers starting at the value of +//MIN_NUM instead of the value of MAX_NUM. + +#include +enum con{MIN_NUM = 0, +MAX_NUM = 100}; +int fRecur(int n); + +main() { + int i, sum1, sum2; + + sum1 = sum2 = 0; + for (i=1; i<=MAX_NUM; i++) + sum1 += i; + printf("The value of sum1 is %d.\n", sum1); + sum2 = fRecur(MAX_NUM); + printf("The value returned by fRecur() is %d.\n", sum2); + + return 0; +} +/* function definition */ +int fRecur(int n) { + if (n == MIN_NUM) + return 0; + return fRecur(n - 1) + n; +} + +//4. Write a program that accepts command-line arguments. If the number of command-line arguments, not including the name of the executable itself, is less than +//two, print out the usage format of the program and ask the user to reenter the command-line arguments. Otherwise, display all command-line arguments entered by +//the user. From bdcf78095d0a6cf4f1dd71ac79caf0f6f95e5a9b Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Mon, 4 Nov 2019 12:07:22 +0100 Subject: [PATCH 02/16] H15_E1-2 are ready for check --- helloworld/H15_E1.c | 42 +++++++++++++++++++++--------------------- helloworld/H15_E2.c | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/helloworld/H15_E1.c b/helloworld/H15_E1.c index 2032200..ff15b87 100644 --- a/helloworld/H15_E1.c +++ b/helloworld/H15_E1.c @@ -2,27 +2,27 @@ //of %s, to print out the character string of the local time on your computer. #include -3: #include -4: -5: void GetDateTime(void); -6: -7: main() -8: { -9: printf("Before the GetDateTime() function is called.\n"); -10: GetDateTime(); -11: printf("After the GetDateTime() function is called.\n"); -12: return 0; -13: } -14: /* GetDateTime() definition */ -15: void GetDateTime(void) -16: { -17: time_t now; -18: -19: printf("Within GetDateTime().\n"); -20: time(&now); -21: printf("Current date and time is: %s\n", -22: asctime(localtime(&now))); -23: } +#include + +void GetDateTime(void); + +main() { + printf("Before the GetDateTime() function is called.\n"); + GetDateTime(); + printf("After the GetDateTime() function is called.\n"); + return 0; +} +/* GetDateTime() definition */ +void GetDateTime(void) { + time_t now; + int i; + char *str; + printf("Within GetDateTime().\n"); + time(&now); + str = asctime (localtime (&now)); + printf("Current date and time is: "); + for (i=0; str[i]; i++) printf("%c", str[i]); +} diff --git a/helloworld/H15_E2.c b/helloworld/H15_E2.c index 48fb9b9..9a3a225 100644 --- a/helloworld/H15_E2.c +++ b/helloworld/H15_E2.c @@ -2,3 +2,19 @@ //on two integer variables. Call the MultiTwo() function from the main() function //and pass two integers to MultiTwo(). Then print out the result returned by the //MultiTwo() function on the screen. + +#include + +MultiTwo(int x, int y); + +main () { + int i = 24; + int j = 13; + printf("%d multiplied by %d equal %d.\n", i, j, MultiTwo(i,j)); + return 0; +} + +MultiTwo(int x, int y) { + return x * y; +} + From 006e8e3bf802c382e2b0b3d38d4470064635cc59 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Mon, 4 Nov 2019 12:08:23 +0100 Subject: [PATCH 03/16] H14_E3 and 4 have problem with solving, pls help --- helloworld/H15_E3.c | 31 ++++++++++++++----------------- helloworld/H15_E4.c | 28 ++++++++++++---------------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/helloworld/H15_E3.c b/helloworld/H15_E3.c index 11f4345..42b52d8 100644 --- a/helloworld/H15_E3.c +++ b/helloworld/H15_E3.c @@ -1,4 +1,5 @@ -//3. Rewrite the program in Listing 15.3. This time, make a function that takes a variable number of int arguments and performs the operation of multiplication on +//3. Rewrite the program in Listing 15.3. This time, make a function that takes +//a variable number of int arguments and performs the operation of multiplication on //these arguments. #include @@ -12,30 +13,26 @@ main () { double d4 = 4.5; printf("Given an argument: %2.1f\n", d1); - printf("The result returned by AddDouble() is: %2.1f\n\n", - AddDouble(1, d1)); + printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(1, d1)); printf("Given arguments: %2.1f and %2.1f\n", d1, d2); - printf("The result returned by AddDouble() is: %2.1f\n\n", - AddDouble(2, d1, d2)); + printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(2, d1, d2)); printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3); - printf("The result returned by AddDouble() is: %2.1f\n\n", - AddDouble(3, d1, d2, d3)); + printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(3, d1, d2, d3)); printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n", d1, d2, d3, d4); - printf("The result returned by AddDouble() is: %2.1f\n", - AddDouble(4, d1, d2, d3, d4)); + printf("The result returned by AddDouble() is: %2.1f\n", AddDouble(4, d1, d2, d3, d4)); return 0; - } +} /* definition of AddDouble() */ - double AddDouble(int x, ...) - { +double AddDouble(int x, ...) { va_list arglist; int i; double result = 0.0; - printf("The number of arguments is: %d\n", x); va_start (arglist, x); - for (i=0; i Date: Mon, 4 Nov 2019 14:54:06 +0100 Subject: [PATCH 04/16] H16 ready for checking --- helloworld/H16_E1.c | 56 ++++++++++++--------------------------------- helloworld/H16_E2.c | 25 ++++++++++++++++++++ helloworld/H16_E3.c | 25 ++++++++++++++++++++ helloworld/H16_E4.c | 35 ++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 42 deletions(-) create mode 100644 helloworld/H16_E2.c create mode 100644 helloworld/H16_E3.c create mode 100644 helloworld/H16_E4.c diff --git a/helloworld/H16_E1.c b/helloworld/H16_E1.c index 20753ca..b5bd7bb 100644 --- a/helloworld/H16_E1.c +++ b/helloworld/H16_E1.c @@ -1,49 +1,21 @@ -//1. Given a character string, I like C!, write a program to pass the string to a function that displays the string on the screen. - -//2. Rewrite the program in exercise 1. This time, change the string of I like C! to I -//love C! by moving a pointer that is initialized with the start address of the string -//and updating the string with new characters. Then, pass the updated string to the -//function to display the content of the string on the screen. - -//3. Given a two-dimensional character array, str, that is initialized as -//char str[2][15] = { "You know what,", "C is powerful." }; -//write a program to pass the start address of str to a function that prints out the -//content of the character array. - -//4. Rewrite the program in Listing 16.7. This time, the array of pointers is initialized -//with the following strings: -//"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", and -//"Saturday". +//1. Given a character string, I like C!, write a program to pass the string to +//a function that displays the string on the screen. #include -/* function declarations */ -void StrPrint1(char **str1, int size); -void StrPrint2(char *str2); -/* main() function */ +void Strprint(char *ch); + main() { - char *str[4] = {"There's music in the sighing of a reed;", - "There's music in the gushing of a rill;", - "There's music in all things if men had ears;", - "There earth is but an echo of the spheres.\n" - }; - int i, size = 4; - - StrPrint1(str, size); - for (i=0; i +#include +void Strprint(char *ch); + +main() { + char str[] = "I like C!"; + char *ptr_str; + ptr_str = str; + *(ptr_str + 3) = 'o'; + *(ptr_str + 4) = 'v'; + Strprint(ptr_str); + return 0; +} + +//function definition + +void Strprint(char *ch) { + printf("%s\n", ch); +} + + + diff --git a/helloworld/H16_E3.c b/helloworld/H16_E3.c new file mode 100644 index 0000000..73d66b5 --- /dev/null +++ b/helloworld/H16_E3.c @@ -0,0 +1,25 @@ +//3. Given a two-dimensional character array, str, that is initialized as +//char str[2][15] = { "You know what,", "C is powerful." }; +//write a program to pass the start address of str to a function that prints out the +//content of the character array. + +#include + +void Strprint(char * ch [][15]); + +main() { + char str[2][15] = { "You know what,", "C is powerful." }; + char *ptr_str; + ptr_str = str; + Strprint(ptr_str); + Strprint(ptr_str + 15); + return 0; +} + +//function defaul + +void Strprint(char *ch [][15]) { + printf("%s\n", ch); +} + +//fordításkor nyom egy csomó warningot, amit nem tudok értelmezni, viszont működik. \ No newline at end of file diff --git a/helloworld/H16_E4.c b/helloworld/H16_E4.c new file mode 100644 index 0000000..b7ead34 --- /dev/null +++ b/helloworld/H16_E4.c @@ -0,0 +1,35 @@ +//4. Rewrite the program in Listing 16.7. This time, the array of pointers is initialized +//with the following strings: +//"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", and +//"Saturday". + +#include +/* function declarations */ +void StrPrint1(char **str1, int size); +void StrPrint2(char *str2); + +/* main() function */ +main() { + char *str[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", +"Saturday\n"}; + int i, size = 7; + StrPrint1(str, size); + for (i=0; i Date: Tue, 5 Nov 2019 13:27:50 +0100 Subject: [PATCH 05/16] H17_E1 ready for checking --- helloworld/H17_E1.c | 85 ++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/helloworld/H17_E1.c b/helloworld/H17_E1.c index a85d13d..111aec1 100644 --- a/helloworld/H17_E1.c +++ b/helloworld/H17_E1.c @@ -2,63 +2,44 @@ //to allocate. Then, initialize the allocated memory with consecutive integers, starting //from 1. Add all the integers contained by the memory block and print out the final //result on the screen. -//2. Write a program that allocates a block of memory space to hold 100 items of the -//float data type by calling the calloc() function. Then, reallocate the block of -//memory in order to hold 50 more items of the float data type. -//3. Write a program to ask the user to enter the total number of float data. Then use -//the calloc() and malloc() functions to allocate two memory blocks with the -//same size specified by the number, and print out the initial values of the two memory blocks. -//4. Rewrite the program in Listing 17.4. This time, use the two special cases of the -//realloc() function to replace the malloc() and free() functions. - /* 17L04.c: Using the realloc() function */ #include #include -#include + /* function declaration */ -void StrCopy(char *str1, char *str2); +int SumNumbers (int x); + /* main() function */ + main() { - char *str[4] = {"There's music in the sighing of a reed;", - "There's music in the gushing of a rill;", - "There's music in all things if men had ears;", - "There earth is but an echo of the spheres.\n" - }; - char *ptr; - int i; - - int termination = 0; - - ptr = malloc((strlen(str[0]) + 1) * sizeof(char)); - if (ptr == NULL){ - printf("malloc() failed.\n"); - termination = 1; - } - else{ - StrCopy(str[0], ptr); - printf("%s\n", ptr); - for (i=1; i<4; i++){ - ptr = realloc(ptr, (strlen(str[i]) + 1) * sizeof(char)); - if (ptr == NULL){ - printf("realloc() failed.\n"); - termination = 1; - i = 4; /* break the fro loop */ - } - else{ - StrCopy(str[i], ptr); - printf("%s\n", ptr); + int SizeofAllmemory; + int *ptr_x; + int sum = 0; + int result; + setbuf(stdout, NULL); + printf("Enter the total number of bytes you want to allocate:"); + scanf("%d", &SizeofAllmemory); + ptr_x = malloc (SizeofAllmemory/2 * sizeof(int)); + //ptr_x = malloc (SizeofAllmemory); Ez vajon miért nem jó helyette? segmentation faultot ad vissza + if (ptr_x != NULL){ + for (int i = 0; i < SizeofAllmemory/2; i++){ + ptr_x[i] = i + 1; + printf("%d\n", *(ptr_x + i)); + } + printf("Az összeg az elején: %d\n", sum); + for (int i = 0; i < SizeofAllmemory/2; i++){ + sum += ptr_x[i]; + printf("Az összeg most: %d\n", sum); + } + printf("The sum is %d\n", sum); + result = 0; } + else { + printf("malloc() function failed.\n"); + result = 1; } - } - free(ptr); - return termination; - } - /* funciton definition */ - void StrCopy(char *str1, char *str2) - { - int i; - - for (i=0; str1[i]; i++) - str2[i] = str1[i]; - str2[i] = ‘\0'; - } \ No newline at end of file + printf("%d", result); + return result; +} + + From 3d4c18a424bd0415ea55d1a480b7b1b12ea32737 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Tue, 5 Nov 2019 13:31:19 +0100 Subject: [PATCH 06/16] H17_E1 this is ready for checking --- helloworld/H17_E1.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/helloworld/H17_E1.c b/helloworld/H17_E1.c index 111aec1..07da852 100644 --- a/helloworld/H17_E1.c +++ b/helloworld/H17_E1.c @@ -6,11 +6,6 @@ #include #include -/* function declaration */ -int SumNumbers (int x); - -/* main() function */ - main() { int SizeofAllmemory; int *ptr_x; From 0ad54d3a7da8ff1dff2573cf63e9a6cfe26c8039 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Tue, 5 Nov 2019 13:45:29 +0100 Subject: [PATCH 07/16] H17_E2 ready for checking --- helloworld/H17_E2.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 helloworld/H17_E2.c diff --git a/helloworld/H17_E2.c b/helloworld/H17_E2.c new file mode 100644 index 0000000..51dcae9 --- /dev/null +++ b/helloworld/H17_E2.c @@ -0,0 +1,31 @@ +//2. Write a program that allocates a block of memory space to hold 100 items of the +//float data type by calling the calloc() function. Then, reallocate the block of +//memory in order to hold 50 more items of the float data type. + + +#include +#include + +main() { + float *ptr_x; + int result; + ptr_x = calloc (100, sizeof(float)); + if (ptr_x != NULL){ + result = 0; + printf("First allocation is done\n"); + } + else { + printf("The first allocation wasn't successful! \n"); + result = 1; + } + ptr_x = realloc (ptr_x, 150); + if (ptr_x != NULL){ + result = 0; + printf("The reallocation is done\n"); + } + else { + printf("The reallocation wasn't successful! \n"); + result = 1; + } + return result; +} \ No newline at end of file From df4851220a54a5d4a5a03531a75ad4b8e027904d Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Tue, 5 Nov 2019 13:59:25 +0100 Subject: [PATCH 08/16] H17_3 ready for checking --- helloworld/H17_E3.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 helloworld/H17_E3.c diff --git a/helloworld/H17_E3.c b/helloworld/H17_E3.c new file mode 100644 index 0000000..2e836a3 --- /dev/null +++ b/helloworld/H17_E3.c @@ -0,0 +1,36 @@ + +//3. Write a program to ask the user to enter the total number of float data. Then use +//the calloc() and malloc() functions to allocate two memory blocks with the +//same size specified by the number, and print out the initial values of the two memory blocks. + +#include +#include + +main() { + int Nodata; + float *ptr_x1; + float *ptr_x2; + int result; + setbuf(stdout, NULL); + printf("Enter the total number of float data you want to allocate:"); + scanf("%d", &Nodata); + ptr_x1 = malloc (Nodata * sizeof(float)); + if (ptr_x1 != NULL){ + result = 0; + printf("First allocation is done\n, the first value of the block is: %f\n", *ptr_x1); + } + else { + printf("The first allocation wasn't successful! \n"); + result = 1; + } + ptr_x2 = calloc (Nodata, sizeof(float)); + if (ptr_x2 != NULL){ + result = 0; + printf("Second allocation is done\n, the first value of the block is: %f\n", *ptr_x2); + } + else { + printf("The 2nd allocation wasn't successful! \n"); + result = 1; + } + return result; +} \ No newline at end of file From 41903885c93a776c3263581907678aaff103240e Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Tue, 5 Nov 2019 14:11:03 +0100 Subject: [PATCH 09/16] H14_E4 ready for checking --- helloworld/H17_E4.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 helloworld/H17_E4.c diff --git a/helloworld/H17_E4.c b/helloworld/H17_E4.c new file mode 100644 index 0000000..ca5531b --- /dev/null +++ b/helloworld/H17_E4.c @@ -0,0 +1,54 @@ +//4. Rewrite the program in Listing 17.4. This time, use the two special cases of the +//realloc() function to replace the malloc() and free() functions. + + /* 17L04.c: Using the realloc() function */ +#include +#include +#include +/* function declaration */ +void StrCopy(char *str1, char *str2); +/* main() function */ +main() { + char *str[4] = {"There's music in the sighing of a reed;", + "There's music in the gushing of a rill;", + "There's music in all things if men had ears;", + "There earth is but an echo of the spheres.\n" + }; + char *ptr; + int i; + + int termination = 0; + + ptr = realloc(NULL, (strlen(str[0]) + 1) * sizeof(char)); //ptr = malloc((strlen(str[0]) + 1) * sizeof(char)); + if (ptr == NULL){ + printf("malloc() failed.\n"); + termination = 1; + } + else{ + StrCopy(str[0], ptr); + printf("%s\n", ptr); + for (i=1; i<4; i++){ + ptr = realloc(ptr, (strlen(str[i]) + 1) * sizeof(char)); + if (ptr == NULL){ + printf("realloc() failed.\n"); + termination = 1; + i = 4; /* break the fro loop */ + } + else{ + StrCopy(str[i], ptr); + printf("%s\n", ptr); + } + } + } + realloc(ptr, 0); //free(ptr) + return termination; + } + /* funciton definition */ + void StrCopy(char *str1, char *str2) + { + int i; + + for (i=0; str1[i]; i++) + str2[i] = str1[i]; + str2[i] = '\0'; + } \ No newline at end of file From 0a95a6d32f7b8c189a08b1fb785c8e8df58288f5 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Wed, 6 Nov 2019 10:04:03 +0100 Subject: [PATCH 10/16] H18_E1 ready for checking --- helloworld/H18_E1.c | 41 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/helloworld/H18_E1.c b/helloworld/H18_E1.c index a1a6d96..33f2b53 100644 --- a/helloworld/H18_E1.c +++ b/helloworld/H18_E1.c @@ -1,39 +1,14 @@ //1. Write a program to print out the values represented by the enumerated names //declared in Quiz question 2 in this hour. -//2. Given the following declarations: -//typedef char WORD; -//typedef int SHORT; -//typedef long LONG; -//typedef float FLOAT; -//typedef double DFLOAT; -//write a program to measure the sizes of the synonyms to the data types. -//3. Rewrite the program in Listing 18.4. This time, add integers starting at the value of -//MIN_NUM instead of the value of MAX_NUM. +//enum tag { name1,name2 = 10,name3,name4 }; #include -enum con{MIN_NUM = 0, -MAX_NUM = 100}; -int fRecur(int n); - main() { - int i, sum1, sum2; - - sum1 = sum2 = 0; - for (i=1; i<=MAX_NUM; i++) - sum1 += i; - printf("The value of sum1 is %d.\n", sum1); - sum2 = fRecur(MAX_NUM); - printf("The value returned by fRecur() is %d.\n", sum2); - - return 0; -} -/* function definition */ -int fRecur(int n) { - if (n == MIN_NUM) - return 0; - return fRecur(n - 1) + n; -} +enum tag { name1, name2 = 10,name3,name4 }; +printf("The value represented by name1 is: %d\n", name1); +printf("The value represented by name2 is: %d\n", name2); +printf("The value represented by name3 is: %d\n", name3); +printf("The value represented by name4 is: %d\n", name4); -//4. Write a program that accepts command-line arguments. If the number of command-line arguments, not including the name of the executable itself, is less than -//two, print out the usage format of the program and ask the user to reenter the command-line arguments. Otherwise, display all command-line arguments entered by -//the user. +return 0; +} From a31b58166b539cbe061c0b5c59e1f78ed4907514 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Wed, 6 Nov 2019 10:08:40 +0100 Subject: [PATCH 11/16] H18_E2 ready for checking --- helloworld/H18_E2.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 helloworld/H18_E2.c diff --git a/helloworld/H18_E2.c b/helloworld/H18_E2.c new file mode 100644 index 0000000..b200b7a --- /dev/null +++ b/helloworld/H18_E2.c @@ -0,0 +1,25 @@ +//2. Given the following declarations: +//typedef char WORD; +//typedef int SHORT; +//typedef long LONG; +//typedef float FLOAT; +//typedef double DFLOAT; +//write a program to measure the sizes of the synonyms to the data types. + +#include +main() { +typedef char WORD; +typedef int SHORT; +typedef long LONG; +typedef float FLOAT; +typedef double DFLOAT; + +printf("The size of WORD is: %d\n", sizeof(WORD)); +printf("The size of SHORT is: %d\n", sizeof(SHORT)); +printf("The size of LONG is: %d\n", sizeof(LONG)); +printf("The size of FLOAT is: %d\n", sizeof(FLOAT)); +printf("The size of DFLOAT is: %d\n", sizeof(DFLOAT)); + +return 0; +} + From 0b76c8f94318c223b99f92360bc549ece97f48dc Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Wed, 6 Nov 2019 10:12:52 +0100 Subject: [PATCH 12/16] H18_E3 ready for checking --- helloworld/H18_E3.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 helloworld/H18_E3.c diff --git a/helloworld/H18_E3.c b/helloworld/H18_E3.c new file mode 100644 index 0000000..f15cce8 --- /dev/null +++ b/helloworld/H18_E3.c @@ -0,0 +1,27 @@ +//3. Rewrite the program in Listing 18.4. This time, add integers starting at the value of +//MIN_NUM instead of the value of MAX_NUM. + +#include +enum con{MIN_NUM = 0, +MAX_NUM = 100}; +int fRecur(int n); + +main() { + int i, sum1, sum2; + + sum1 = sum2 = 0; + for (i=1; i<=MAX_NUM; i++) + sum1 += i; + printf("The value of sum1 is %d.\n", sum1); + sum2 = fRecur(MIN_NUM); + printf("The value returned by fRecur() is %d.\n", sum2); + + return 0; +} +/* function definition */ +int fRecur(int n) { + if (n > MAX_NUM) + return 0; + return fRecur(n + 1) + n; +} + From d02f23d014de57633c9954c955116d168fe5843d Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Wed, 6 Nov 2019 10:26:47 +0100 Subject: [PATCH 13/16] H18_E4 ready for checking --- helloworld/H18_E4.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 helloworld/H18_E4.c diff --git a/helloworld/H18_E4.c b/helloworld/H18_E4.c new file mode 100644 index 0000000..e8d7cab --- /dev/null +++ b/helloworld/H18_E4.c @@ -0,0 +1,25 @@ +//4. Write a program that accepts command-line arguments. If the number of +//command-line arguments, not including the name of the executable itself, +//is less than two, print out the usage format of the program and +//ask the user to reenter the command-line arguments. +//Otherwise, display all command-line arguments entered by +//the user. + +#include +main (int argc, char *argv[]) { + int i; + if (argc < 2) { + printf("The usage of this program is:\n"); + printf("a.EXE argument1 argument2 [...argumentN]\n"); + printf("Please start the program again'"); + } + else { + printf("The number of the command-line arguments are: %d\n", argc); + printf("The command-line arguments are:\n"); + for (i=1; i Date: Sat, 9 Nov 2019 08:12:53 +0100 Subject: [PATCH 14/16] Have problem with H15_E3 -E4, I can't understand the functions with variable number of arguments. Please help --- helloworld/H15_E3.c | 8 ++++---- helloworld/H15_E4.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/helloworld/H15_E3.c b/helloworld/H15_E3.c index 42b52d8..286a822 100644 --- a/helloworld/H15_E3.c +++ b/helloworld/H15_E3.c @@ -13,11 +13,11 @@ main () { double d4 = 4.5; printf("Given an argument: %2.1f\n", d1); - printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(1, d1)); + printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(1, d1)); printf("Given arguments: %2.1f and %2.1f\n", d1, d2); - printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(2, d1, d2)); + printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(2, d1, d2)); printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3); - printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(3, d1, d2, d3)); + printf("The result returned by AddDouble() is: %2.1f\n\n", AddDouble(3, d1, d2, d3)); printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n", d1, d2, d3, d4); printf("The result returned by AddDouble() is: %2.1f\n", AddDouble(4, d1, d2, d3, d4)); return 0; @@ -32,7 +32,7 @@ double AddDouble(int x, ...) { for (i=0; i Date: Sat, 9 Nov 2019 08:24:57 +0100 Subject: [PATCH 15/16] Cleanup H16 -H 18 --- helloworld/H16_E3.c | 4 ++-- helloworld/H17_E1.c | 6 +++--- helloworld/H17_E2.c | 6 +++--- helloworld/H17_E3.c | 2 +- helloworld/H17_E4.c | 44 ++++++++++++++++++++------------------------ helloworld/H18_E3.c | 10 +++++----- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/helloworld/H16_E3.c b/helloworld/H16_E3.c index 73d66b5..7a45d90 100644 --- a/helloworld/H16_E3.c +++ b/helloworld/H16_E3.c @@ -8,7 +8,7 @@ void Strprint(char * ch [][15]); main() { - char str[2][15] = { "You know what,", "C is powerful." }; + char str[2][15] = {"You know what,", "C is powerful."}; char *ptr_str; ptr_str = str; Strprint(ptr_str); @@ -20,6 +20,6 @@ main() { void Strprint(char *ch [][15]) { printf("%s\n", ch); +//fordításkor nyom egy csomó warningot, amit nem tudok értelmezni, viszont működik. } -//fordításkor nyom egy csomó warningot, amit nem tudok értelmezni, viszont működik. \ No newline at end of file diff --git a/helloworld/H17_E1.c b/helloworld/H17_E1.c index 07da852..150680d 100644 --- a/helloworld/H17_E1.c +++ b/helloworld/H17_E1.c @@ -16,13 +16,13 @@ main() { scanf("%d", &SizeofAllmemory); ptr_x = malloc (SizeofAllmemory/2 * sizeof(int)); //ptr_x = malloc (SizeofAllmemory); Ez vajon miért nem jó helyette? segmentation faultot ad vissza - if (ptr_x != NULL){ - for (int i = 0; i < SizeofAllmemory/2; i++){ + if (ptr_x != NULL) { + for (int i = 0; i < SizeofAllmemory/2; i++) { ptr_x[i] = i + 1; printf("%d\n", *(ptr_x + i)); } printf("Az összeg az elején: %d\n", sum); - for (int i = 0; i < SizeofAllmemory/2; i++){ + for (int i = 0; i < SizeofAllmemory/2; i++) { sum += ptr_x[i]; printf("Az összeg most: %d\n", sum); } diff --git a/helloworld/H17_E2.c b/helloworld/H17_E2.c index 51dcae9..8ecb077 100644 --- a/helloworld/H17_E2.c +++ b/helloworld/H17_E2.c @@ -9,8 +9,8 @@ main() { float *ptr_x; int result; - ptr_x = calloc (100, sizeof(float)); - if (ptr_x != NULL){ + ptr_x = calloc(100, sizeof(float)); + if (ptr_x != NULL) { result = 0; printf("First allocation is done\n"); } @@ -19,7 +19,7 @@ main() { result = 1; } ptr_x = realloc (ptr_x, 150); - if (ptr_x != NULL){ + if (ptr_x != NULL) { result = 0; printf("The reallocation is done\n"); } diff --git a/helloworld/H17_E3.c b/helloworld/H17_E3.c index 2e836a3..2f161a7 100644 --- a/helloworld/H17_E3.c +++ b/helloworld/H17_E3.c @@ -24,7 +24,7 @@ main() { result = 1; } ptr_x2 = calloc (Nodata, sizeof(float)); - if (ptr_x2 != NULL){ + if (ptr_x2 != NULL) { result = 0; printf("Second allocation is done\n, the first value of the block is: %f\n", *ptr_x2); } diff --git a/helloworld/H17_E4.c b/helloworld/H17_E4.c index ca5531b..db0228f 100644 --- a/helloworld/H17_E4.c +++ b/helloworld/H17_E4.c @@ -16,38 +16,34 @@ main() { }; char *ptr; int i; - int termination = 0; - ptr = realloc(NULL, (strlen(str[0]) + 1) * sizeof(char)); //ptr = malloc((strlen(str[0]) + 1) * sizeof(char)); - if (ptr == NULL){ - printf("malloc() failed.\n"); - termination = 1; - } - else{ - StrCopy(str[0], ptr); - printf("%s\n", ptr); - for (i=1; i<4; i++){ - ptr = realloc(ptr, (strlen(str[i]) + 1) * sizeof(char)); - if (ptr == NULL){ - printf("realloc() failed.\n"); - termination = 1; - i = 4; /* break the fro loop */ - } - else{ - StrCopy(str[i], ptr); - printf("%s\n", ptr); - } + if (ptr == NULL) { + printf("malloc() failed.\n"); + termination = 1; } + else { + StrCopy(str[0], ptr); + printf("%s\n", ptr); + for (i=1; i<4; i++) { + ptr = realloc(ptr, (strlen(str[i]) + 1) * sizeof(char)); + if (ptr == NULL) { + printf("realloc() failed.\n"); + termination = 1; + i = 4; /* break the for loop */ + } + else { + StrCopy(str[i], ptr); + printf("%s\n", ptr); + } + } } realloc(ptr, 0); //free(ptr) return termination; - } +} /* funciton definition */ - void StrCopy(char *str1, char *str2) - { + void StrCopy(char *str1, char *str2) { int i; - for (i=0; str1[i]; i++) str2[i] = str1[i]; str2[i] = '\0'; diff --git a/helloworld/H18_E3.c b/helloworld/H18_E3.c index f15cce8..3a045d8 100644 --- a/helloworld/H18_E3.c +++ b/helloworld/H18_E3.c @@ -2,17 +2,17 @@ //MIN_NUM instead of the value of MAX_NUM. #include -enum con{MIN_NUM = 0, -MAX_NUM = 100}; +enum con {MIN_NUM = 0, + MAX_NUM = 100}; int fRecur(int n); main() { int i, sum1, sum2; - sum1 = sum2 = 0; - for (i=1; i<=MAX_NUM; i++) + for (i=1; i<=MAX_NUM; i++) { sum1 += i; - printf("The value of sum1 is %d.\n", sum1); + } + printf("The value of sum1 is %d.\n", sum1); sum2 = fRecur(MIN_NUM); printf("The value returned by fRecur() is %d.\n", sum2); From 0079ccfe252f0ee9489e5d9dcf857742fcc3f06d Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Sun, 10 Nov 2019 15:26:52 +0100 Subject: [PATCH 16/16] remove comment --- helloworld/H15_E3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helloworld/H15_E3.c b/helloworld/H15_E3.c index 286a822..a443c7a 100644 --- a/helloworld/H15_E3.c +++ b/helloworld/H15_E3.c @@ -32,7 +32,7 @@ double AddDouble(int x, ...) { for (i=0; i