From 2d262f54d1881c3d4fbfe15d89255765ba843f0c Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 07:21:48 +0100 Subject: [PATCH 01/12] H11: in the E3 is interesting, printf returns unexpected string on the screen. Why? --- helloworld/H11_E1.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 helloworld/H11_E1.c diff --git a/helloworld/H11_E1.c b/helloworld/H11_E1.c new file mode 100644 index 0000000..db25511 --- /dev/null +++ b/helloworld/H11_E1.c @@ -0,0 +1,38 @@ +//1. Given three integer variables, x = 512, y = 1024, and z = 2048, write a program +//to print out their left values as well as their right values. + +#include + +main() { + int x = 512; + int y = 1024; + int z = 2048; + + printf("x: address=%p, content=%d\n", &x, x); + printf("y: address=%p, content=%d\n", &y, y); + printf("z: address=%p, content=%d\n", &z, z); + + +//2. Write a program to update the value of the double variable flt_num from 123.45 +//to 543.21 by using a double pointer. + + double flt_num = 123.45; + double * ptr_flt_num = & flt_num; + * ptr_flt_num = 543.21; + printf("flt_num =%f\n", flt_num); + + +//3. Given a character variable ch and ch = 'A', write a program to update the value of +//ch to decimal 66 by using a pointer. + + char ch = 'A'; + int *ptr_ch = &ch; + *ptr_ch = 66; + printf("ch returns %c"); + +//4. Given that x=5 and y=6, write a program to calculate the multiplication of the two +//integers and print out the result, which is saved in x, all in the way of indirection +//(that is, using pointers). + + return 0; +} \ No newline at end of file From 157f97de2d0e8cb8806a56a5771fd28603a467bb Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 07:22:57 +0100 Subject: [PATCH 02/12] H11 E3 works properly --- helloworld/H11_E1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helloworld/H11_E1.c b/helloworld/H11_E1.c index db25511..5859999 100644 --- a/helloworld/H11_E1.c +++ b/helloworld/H11_E1.c @@ -28,7 +28,7 @@ main() { char ch = 'A'; int *ptr_ch = &ch; *ptr_ch = 66; - printf("ch returns %c"); + printf("ch returns %c", ch); //4. Given that x=5 and y=6, write a program to calculate the multiplication of the two //integers and print out the result, which is saved in x, all in the way of indirection From b7d2dbe101b69c05a1af13c08e16372735fd955a Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 07:31:36 +0100 Subject: [PATCH 03/12] H11 done --- helloworld/H11_E1.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/helloworld/H11_E1.c b/helloworld/H11_E1.c index 5859999..ad4c2cc 100644 --- a/helloworld/H11_E1.c +++ b/helloworld/H11_E1.c @@ -26,13 +26,19 @@ main() { //ch to decimal 66 by using a pointer. char ch = 'A'; - int *ptr_ch = &ch; + char *ptr_ch = &ch; *ptr_ch = 66; - printf("ch returns %c", ch); + printf("ch returns %c\n", ch); //4. Given that x=5 and y=6, write a program to calculate the multiplication of the two //integers and print out the result, which is saved in x, all in the way of indirection //(that is, using pointers). + x = 5; + y = 6; + int *ptrx = &x; + int *ptry = &y; + *ptrx *= *ptry; + printf("x=%d\n", x); return 0; } \ No newline at end of file From bc7bfffc4d6510b77f830a309c4c9f685bd3d56b Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 09:35:39 +0100 Subject: [PATCH 04/12] =?UTF-8?q?H12=20E4-ben=20megint=20egy=20=C3=A9rdeke?= =?UTF-8?q?s=20m=C5=B1k=C3=B6d=C3=A9s.=20N=C3=A9zd=20meg,=20l=C3=A9ci,=20n?= =?UTF-8?q?em=20=C3=A9rtem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helloworld/H12_E1.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 helloworld/H12_E1.c diff --git a/helloworld/H12_E1.c b/helloworld/H12_E1.c new file mode 100644 index 0000000..e027424 --- /dev/null +++ b/helloworld/H12_E1.c @@ -0,0 +1,73 @@ +/*1. Given this character array: +char array_ch[5] = {'A', 'B', 'C', 'D', 'E'}; +write a program to display each element of the array on the screen.*/ +#include + +main() { + char array_ch[5] = {'A', 'B', 'C', 'D', 'E'}; + int i; + for (i = 0; i < 5; i++){ + printf("%c",array_ch[i]); + } + printf("\n"); + +/*2. Rewrite the program in Exercise 1, but this time use a for loop to initialize the +character array with 'a', 'b', 'c', 'd', and 'e', and then print out the value of +each element in the array.*/ + + for (i = 0; i < 5; i++){ + array_ch[i] = i + 97; + printf("%c",array_ch[i]); + } + + +/*3. Given this two-dimensional unsized array: +char list_ch[][2] = { +'1', 'a', +'2', 'b', +'3', 'c', +'4', 'd', +'5', 'e', +'6', 'f'}; +write a program to measure the total bytes taken by the array, and then print out all +elements of the array.*/ + + char list_ch[][2] = { + '1', 'a', + '2', 'b', + '3', 'c', + '4', 'd', + '5', 'e', + '6', 'f'}; + int total_byte = sizeof (list_ch); + int number_of_elements = total_byte / sizeof (char); + int imax = number_of_elements / 2; + for (i=0; i < imax; i++){ + printf("\n"); + for ( int j=0; j<2; j++){ + printf("%c", list_ch[i][j]); + } + } + printf("\n"); + +/*4. Rewrite the program in Listing 12.5. This time put a string of characters, I like +C!, on the screen.*/ + + char array_ch_new[] = {'I',' ','l','i','k','e',' ','C','!'}; + for (i=0; array_ch_new[i]; i++){ + printf("%c", array_ch_new[i]); + } + printf("\n"); + +/*5. Given the following array: +double list_data[6] = { +1.12345, +2.12345, +3.12345, +4.12345, +5.12345}; +use the two equivalent ways taught in this lesson to measure the total memory +space taken by the array, and then display the results on the screen.*/ + + return 0; +} \ No newline at end of file From e686cb572912cce68622b560dfd767bfde726a70 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 09:46:55 +0100 Subject: [PATCH 05/12] H12_E4 separated, working properly --- helloworld/H12_E1.c | 7 ++++--- helloworld/H12_E4_separated.c | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 helloworld/H12_E4_separated.c diff --git a/helloworld/H12_E1.c b/helloworld/H12_E1.c index e027424..90fb3a0 100644 --- a/helloworld/H12_E1.c +++ b/helloworld/H12_E1.c @@ -52,9 +52,10 @@ elements of the array.*/ /*4. Rewrite the program in Listing 12.5. This time put a string of characters, I like C!, on the screen.*/ - - char array_ch_new[] = {'I',' ','l','i','k','e',' ','C','!'}; - for (i=0; array_ch_new[i]; i++){ + setbuf(stdout, NULL); + char array_ch_new[9] = {'I',' ','l','i','k','e',' ','C','!'}; +// for (i=0; array_ch_new[i]; i++){ + for (i=0; i < 9; i++){ printf("%c", array_ch_new[i]); } printf("\n"); diff --git a/helloworld/H12_E4_separated.c b/helloworld/H12_E4_separated.c new file mode 100644 index 0000000..368ed6a --- /dev/null +++ b/helloworld/H12_E4_separated.c @@ -0,0 +1,12 @@ +#include + +main() { + int i; + char array_ch_new[9] = {'I',' ','l','i','k','e',' ','C','!'}; +// for (i=0; array_ch_new[i]; i++){ + for (i=0; i < 9; i++){ + printf("%c", array_ch_new[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file From 30336be826861940526aceca9b36003108ec18cf Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 09:56:57 +0100 Subject: [PATCH 06/12] H12 done. The strange behavior of E4 's wanished after writing E5 --- helloworld/H12_E1.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/helloworld/H12_E1.c b/helloworld/H12_E1.c index 90fb3a0..c9ab906 100644 --- a/helloworld/H12_E1.c +++ b/helloworld/H12_E1.c @@ -52,7 +52,7 @@ elements of the array.*/ /*4. Rewrite the program in Listing 12.5. This time put a string of characters, I like C!, on the screen.*/ - setbuf(stdout, NULL); + char array_ch_new[9] = {'I',' ','l','i','k','e',' ','C','!'}; // for (i=0; array_ch_new[i]; i++){ for (i=0; i < 9; i++){ @@ -69,6 +69,15 @@ double list_data[6] = { 5.12345}; use the two equivalent ways taught in this lesson to measure the total memory space taken by the array, and then display the results on the screen.*/ - + double list_data[6] = { + 1.12345, + 2.12345, + 3.12345, + 4.12345, + 5.12345}; + total_byte = sizeof (list_data); + printf("The total memory space taken by the array by method I.: %d\n", total_byte); + total_byte = sizeof (double) * 6; + printf("The total memory space taken by the array by method II.: %d\n", total_byte); return 0; } \ No newline at end of file From 0f6cb92cc3118dca92f7feb93e8e3d6b40f46862 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 10:02:04 +0100 Subject: [PATCH 07/12] H12 again. changed back the line 59, written by the book, and the E4 doesn't work properly again. Pls help --- helloworld/H12_E1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helloworld/H12_E1.c b/helloworld/H12_E1.c index c9ab906..bab9f00 100644 --- a/helloworld/H12_E1.c +++ b/helloworld/H12_E1.c @@ -54,8 +54,8 @@ elements of the array.*/ C!, on the screen.*/ char array_ch_new[9] = {'I',' ','l','i','k','e',' ','C','!'}; -// for (i=0; array_ch_new[i]; i++){ - for (i=0; i < 9; i++){ + for (i=0; array_ch_new[i]; i++){ +// for (i=0; i < 9; i++){ printf("%c", array_ch_new[i]); } printf("\n"); From 5b54e52dfc6d053c1713fc10bf7b00dfbaa2d851 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 12:09:19 +0100 Subject: [PATCH 08/12] H13 done --- helloworld/H13_E1.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 helloworld/H13_E1.c diff --git a/helloworld/H13_E1.c b/helloworld/H13_E1.c new file mode 100644 index 0000000..b5621f2 --- /dev/null +++ b/helloworld/H13_E1.c @@ -0,0 +1,55 @@ +//1. Given a character array in the following statement, +//char str1[] = "This is Exercise 1."; +//write a program to copy the string from str1 to another array, called str2. +#include +#include +main() { + setbuf(stdout, NULL); + char str1[] = "This is Exercise 1."; + char str2[strlen(str1)]; + strcpy (str2, str1); + printf("%s\n", str2); + + +//2. Write a program to measure the length of a string by evaluating the elements in a +//character array one by one until you reach the null character. To prove you get the +//right result, you can use the strlen() function to measure the same string again. + + char array_ch_new[10] = {'I',' ','l','i','k','e',' ','C','!','\0'}; + int i; + int length = 0; + for (i=0; array_ch_new[i]; i++){ + length ++; + } + printf("The lenght of string is %d\n", length); + length = strlen(array_ch_new); + printf("The lenght of string is %d\n", length); + +//3. Rewrite the program in Listing 13.4. This time, convert all uppercase characters to +//their lowercase counterparts. + + + char str[80]; + int delt = 'a' - 'A'; + printf("Enter a string less than 80 characters:\n"); + gets( str ); + i = 0; + while (str[i]){ + if ((str[i] >= 'A') && (str[i] <= 'Z')) + str[i] += delt; /* convert to lower case */ + ++i; + } + printf("The entered string is (in lowercase):\n"); + puts( str ); + + +//4. Write a program that uses the scanf() function to read in two integers entered by +//the user, adds the two integers, and then prints out the sum on the screen. + + int x = 0; + int y = 0; + printf("Enter two integer:\n"); + scanf("%d %d", &x, &y); + printf("The sum of the numbers is %d", x + y); + return 0; +} \ No newline at end of file From 8a977c65e965939913efc5815de18ee9f43380aa Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Fri, 1 Nov 2019 21:19:14 +0100 Subject: [PATCH 09/12] H12 E1 fix --- helloworld/H12_E1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helloworld/H12_E1.c b/helloworld/H12_E1.c index bab9f00..ec5c5ea 100644 --- a/helloworld/H12_E1.c +++ b/helloworld/H12_E1.c @@ -53,7 +53,7 @@ elements of the array.*/ /*4. Rewrite the program in Listing 12.5. This time put a string of characters, I like C!, on the screen.*/ - char array_ch_new[9] = {'I',' ','l','i','k','e',' ','C','!'}; + char array_ch_new[10] = {'I',' ','l','i','k','e',' ','C','!','\0'}; for (i=0; array_ch_new[i]; i++){ // for (i=0; i < 9; i++){ printf("%c", array_ch_new[i]); From d6fc13340bcde3231691b8f1a8c7709334812251 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Sat, 2 Nov 2019 06:48:29 +0100 Subject: [PATCH 10/12] Only transfer, no pull request --- helloworld/H14_E1.c | 10 ++++++++++ helloworld/H14_E2.c | 25 +++++++++++++++++++++++++ helloworld/H14_E3.c | 12 ++++++++++++ helloworld/H14_E4.c | 20 ++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 helloworld/H14_E1.c create mode 100644 helloworld/H14_E2.c create mode 100644 helloworld/H14_E3.c create mode 100644 helloworld/H14_E4.c diff --git a/helloworld/H14_E1.c b/helloworld/H14_E1.c new file mode 100644 index 0000000..ea72db6 --- /dev/null +++ b/helloworld/H14_E1.c @@ -0,0 +1,10 @@ +//1. Given the following: +//• An int variable with block scope and temporary storage +//• A constant character variable with block scope +//• A float local variable with permanent storage +//• A register int variable +//• A char pointer initialized with a null character +//write declarations for all of them. + + + diff --git a/helloworld/H14_E2.c b/helloworld/H14_E2.c new file mode 100644 index 0000000..f704bf8 --- /dev/null +++ b/helloworld/H14_E2.c @@ -0,0 +1,25 @@ +//2. Rewrite the program in Listing 14.2. This time, pass the int variable x and the +//float variable y as arguments to the function_1() function. What do you get on +//your screen after running the program? + +/#include +/ +int x = 1234; /* program scope */ +double y = 1.234567; /* program scope */ + +void function_1() { +printf("From function_1:\n x=%d, y=%f\n", x, y); + } + +main() { + int x = 4321; /* block scope 1*/ + function_1(); + printf("Within the main block:\n x=%d, y=%f\n", x, y); + /* a nested block */ + { + double y = 7.654321; /* block scope 2 */ + function_1(); + printf("Within the nested block:\n x=%d, y=%f\n", x, y); + } + return 0; +} \ No newline at end of file diff --git a/helloworld/H14_E3.c b/helloworld/H14_E3.c new file mode 100644 index 0000000..7452cc3 --- /dev/null +++ b/helloworld/H14_E3.c @@ -0,0 +1,12 @@ +//3. Compile and run the following program. What do you get on the screen, and why? +#include +int main() +{ +int i; +for (i=0; i<5; i++){ +int x = 0; +static int y = 0; +printf("x=%d, y=%d\n", x++, y++); +} +return 0; +} \ No newline at end of file diff --git a/helloworld/H14_E4.c b/helloworld/H14_E4.c new file mode 100644 index 0000000..f6fe7f3 --- /dev/null +++ b/helloworld/H14_E4.c @@ -0,0 +1,20 @@ +//4. Rewrite the add_two() function in Listing 14.3 to print out the previous result of +the addition, as well as the counter value. + +#include +/* the add_two function */ +int add_two(int x, int y) +{ +static int counter = 1; + +printf("This is the function call of %d,\n", counter++); +return (x + y); +} +/* the main function */ +main() { + int i, j; + + for (i=0, j=5; i<5; i++, j--) + printf("the addition of %d and %d is %d.\n\n", i, j, add_two(i, j)); + return 0; +} \ No newline at end of file From d61d235508cb2ccf5469940809656ea9454029d1 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Mon, 4 Nov 2019 10:37:23 +0100 Subject: [PATCH 11/12] H14 ready to check --- helloworld/H14_E1.c | 33 ++++++++++++++++++++++++++++----- helloworld/H14_E2.c | 21 ++++++++++++++++----- helloworld/H14_E3.c | 10 ++++++++++ helloworld/H14_E4.c | 8 +++++++- 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/helloworld/H14_E1.c b/helloworld/H14_E1.c index ea72db6..bedbb27 100644 --- a/helloworld/H14_E1.c +++ b/helloworld/H14_E1.c @@ -1,10 +1,33 @@ //1. Given the following: -//• An int variable with block scope and temporary storage -//• A constant character variable with block scope -//• A float local variable with permanent storage -//• A register int variable -//• A char pointer initialized with a null character +//• a.) An int variable with block scope and temporary storage +//• b) A constant character variable with block scope +//• c) A float local variable with permanent storage +//• d) A register int variable +//• e) A char pointer initialized with a null character //write declarations for all of them. +//a) +{ + int x; +} +//b) + +{ + const char ch; +} + +//c) + +{ + static float number; +} + +//d) + +register int i; + +// e) + +char * ptr_ch = 0; diff --git a/helloworld/H14_E2.c b/helloworld/H14_E2.c index f704bf8..ca679aa 100644 --- a/helloworld/H14_E2.c +++ b/helloworld/H14_E2.c @@ -2,24 +2,35 @@ //float variable y as arguments to the function_1() function. What do you get on //your screen after running the program? -/#include -/ +#include + int x = 1234; /* program scope */ double y = 1.234567; /* program scope */ -void function_1() { +void function_1(int x, double y) { printf("From function_1:\n x=%d, y=%f\n", x, y); } main() { int x = 4321; /* block scope 1*/ - function_1(); + function_1(x, y); printf("Within the main block:\n x=%d, y=%f\n", x, y); /* a nested block */ { double y = 7.654321; /* block scope 2 */ - function_1(); + function_1(x, y); printf("Within the nested block:\n x=%d, y=%f\n", x, y); } return 0; +/* Answer: +From function_1: + x=4321, y=1.234567 +Within the main block: + x=4321, y=1.234567 +From function_1: + x=4321, y=7.654321 +Within the nested block: + x=4321, y=7.654321 + */ + } \ No newline at end of file diff --git a/helloworld/H14_E3.c b/helloworld/H14_E3.c index 7452cc3..f196e92 100644 --- a/helloworld/H14_E3.c +++ b/helloworld/H14_E3.c @@ -9,4 +9,14 @@ static int y = 0; printf("x=%d, y=%d\n", x++, y++); } return 0; +/* +The screen: +x=0, y=0 +x=0, y=1 +x=0, y=2 +x=0, y=3 +x=0, y=4 + +x is set to 0 every time the program enters the for loop, +y has a permanent sorage, so the value saved in y is kept.*/ } \ No newline at end of file diff --git a/helloworld/H14_E4.c b/helloworld/H14_E4.c index f6fe7f3..515ffe4 100644 --- a/helloworld/H14_E4.c +++ b/helloworld/H14_E4.c @@ -1,13 +1,16 @@ //4. Rewrite the add_two() function in Listing 14.3 to print out the previous result of -the addition, as well as the counter value. +//the addition, as well as the counter value. #include /* the add_two function */ int add_two(int x, int y) { static int counter = 1; +static int prev_sum =0; printf("This is the function call of %d,\n", counter++); +printf("The previous result was %d,\n", prev_sum); +prev_sum = x + y; return (x + y); } /* the main function */ @@ -17,4 +20,7 @@ main() { for (i=0, j=5; i<5; i++, j--) printf("the addition of %d and %d is %d.\n\n", i, j, add_two(i, j)); return 0; + +//Azért ha őszinte vagyok, ezt a static dolgot nem értem. + } \ No newline at end of file From 93ad7aed455414e52cc256fe6b22e8947e4d4b17 Mon Sep 17 00:00:00 2001 From: Goodwitch68 on AcerS7 <56366861+Goodwitch68@users.noreply.github.com> Date: Sat, 9 Nov 2019 08:04:57 +0100 Subject: [PATCH 12/12] Cleanup H11 - H14 --- helloworld/H11_E1.c | 4 ++-- helloworld/H12_E1.c | 16 ++++++++-------- helloworld/H12_E4_separated.c | 2 +- helloworld/H13_E1.c | 8 ++++---- helloworld/H14_E1.c | 2 +- helloworld/H14_E2.c | 2 +- helloworld/H14_E3.c | 13 ++++++------- helloworld/H14_E4.c | 16 +++++++--------- 8 files changed, 30 insertions(+), 33 deletions(-) diff --git a/helloworld/H11_E1.c b/helloworld/H11_E1.c index ad4c2cc..7bbf6bd 100644 --- a/helloworld/H11_E1.c +++ b/helloworld/H11_E1.c @@ -17,8 +17,8 @@ main() { //to 543.21 by using a double pointer. double flt_num = 123.45; - double * ptr_flt_num = & flt_num; - * ptr_flt_num = 543.21; + double *ptr_flt_num = &flt_num; + *ptr_flt_num = 543.21; printf("flt_num =%f\n", flt_num); diff --git a/helloworld/H12_E1.c b/helloworld/H12_E1.c index ec5c5ea..4fcca5c 100644 --- a/helloworld/H12_E1.c +++ b/helloworld/H12_E1.c @@ -6,8 +6,8 @@ write a program to display each element of the array on the screen.*/ main() { char array_ch[5] = {'A', 'B', 'C', 'D', 'E'}; int i; - for (i = 0; i < 5; i++){ - printf("%c",array_ch[i]); + for (i=0; i<5; i++){ + printf("%c", array_ch[i]); } printf("\n"); @@ -15,7 +15,7 @@ main() { character array with 'a', 'b', 'c', 'd', and 'e', and then print out the value of each element in the array.*/ - for (i = 0; i < 5; i++){ + for (i=0; i<5; i++){ array_ch[i] = i + 97; printf("%c",array_ch[i]); } @@ -39,10 +39,10 @@ elements of the array.*/ '4', 'd', '5', 'e', '6', 'f'}; - int total_byte = sizeof (list_ch); - int number_of_elements = total_byte / sizeof (char); + int total_byte = sizeof(list_ch); + int number_of_elements = total_byte / sizeof(char); int imax = number_of_elements / 2; - for (i=0; i < imax; i++){ + for (i=0; i= 'A') && (str[i] <= 'Z')) str[i] += delt; /* convert to lower case */ ++i; } printf("The entered string is (in lowercase):\n"); - puts( str ); + puts(str); //4. Write a program that uses the scanf() function to read in two integers entered by diff --git a/helloworld/H14_E1.c b/helloworld/H14_E1.c index bedbb27..c0cbf0e 100644 --- a/helloworld/H14_E1.c +++ b/helloworld/H14_E1.c @@ -30,4 +30,4 @@ register int i; // e) -char * ptr_ch = 0; +char *ptr_ch = 0; diff --git a/helloworld/H14_E2.c b/helloworld/H14_E2.c index ca679aa..a0c1311 100644 --- a/helloworld/H14_E2.c +++ b/helloworld/H14_E2.c @@ -9,7 +9,7 @@ double y = 1.234567; /* program scope */ void function_1(int x, double y) { printf("From function_1:\n x=%d, y=%f\n", x, y); - } +} main() { int x = 4321; /* block scope 1*/ diff --git a/helloworld/H14_E3.c b/helloworld/H14_E3.c index f196e92..9363596 100644 --- a/helloworld/H14_E3.c +++ b/helloworld/H14_E3.c @@ -1,12 +1,11 @@ //3. Compile and run the following program. What do you get on the screen, and why? #include -int main() -{ -int i; -for (i=0; i<5; i++){ -int x = 0; -static int y = 0; -printf("x=%d, y=%d\n", x++, y++); +int main() { + int i; + for (i=0; i<5; i++){ + int x = 0; + static int y = 0; + printf("x=%d, y=%d\n", x++, y++); } return 0; /* diff --git a/helloworld/H14_E4.c b/helloworld/H14_E4.c index 515ffe4..a043083 100644 --- a/helloworld/H14_E4.c +++ b/helloworld/H14_E4.c @@ -3,15 +3,13 @@ #include /* the add_two function */ -int add_two(int x, int y) -{ -static int counter = 1; -static int prev_sum =0; - -printf("This is the function call of %d,\n", counter++); -printf("The previous result was %d,\n", prev_sum); -prev_sum = x + y; -return (x + y); +int add_two(int x, int y) { + static int counter = 1; + static int prev_sum = 0; + printf("This is the function call of %d,\n", counter++); + printf("The previous result was %d,\n", prev_sum); + prev_sum = x + y; + return (x + y); } /* the main function */ main() {