diff --git a/helloworld/H15_E1.c b/helloworld/H15_E1.c new file mode 100644 index 0000000..ff15b87 --- /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 +#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 new file mode 100644 index 0000000..9a3a225 --- /dev/null +++ b/helloworld/H15_E2.c @@ -0,0 +1,20 @@ +//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. + +#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; +} + diff --git a/helloworld/H15_E3.c b/helloworld/H15_E3.c new file mode 100644 index 0000000..a443c7a --- /dev/null +++ b/helloworld/H15_E3.c @@ -0,0 +1,38 @@ +//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 + +void Strprint(char *ch); + +main() { + char str[] = "I like C!"; + char *ptr_str; + ptr_str = str; + Strprint(ptr_str); + return 0; +} + +//function defaul + +void Strprint(char *ch) { + printf("%s\n", ch); +} + diff --git a/helloworld/H16_E2.c b/helloworld/H16_E2.c new file mode 100644 index 0000000..712176e --- /dev/null +++ b/helloworld/H16_E2.c @@ -0,0 +1,25 @@ +//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.#include +#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..7a45d90 --- /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. +} + 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 +#include + +main() { + 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; + } + printf("%d", result); + return result; +} + + diff --git a/helloworld/H17_E2.c b/helloworld/H17_E2.c new file mode 100644 index 0000000..8ecb077 --- /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 diff --git a/helloworld/H17_E3.c b/helloworld/H17_E3.c new file mode 100644 index 0000000..2f161a7 --- /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 diff --git a/helloworld/H17_E4.c b/helloworld/H17_E4.c new file mode 100644 index 0000000..db0228f --- /dev/null +++ b/helloworld/H17_E4.c @@ -0,0 +1,50 @@ +//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 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) { + 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..33f2b53 --- /dev/null +++ b/helloworld/H18_E1.c @@ -0,0 +1,14 @@ +//1. Write a program to print out the values represented by the enumerated names +//declared in Quiz question 2 in this hour. +//enum tag { name1,name2 = 10,name3,name4 }; + +#include +main() { +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); + +return 0; +} 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; +} + diff --git a/helloworld/H18_E3.c b/helloworld/H18_E3.c new file mode 100644 index 0000000..3a045d8 --- /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; +} + 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