Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3.2 Sample Inputs/Outputs red 361 % gcc lab2C0.c -o lab2c0 red 362 % lab2c0 k[0]: 0 k[1]: 0 k[2]: 0 k[3]: 0 k[4]: 0 k[5]:
3.2 Sample Inputs/Outputs red 361 % gcc lab2C0.c -o lab2c0 red 362 % lab2c0 k[0]: 0 k[1]: 0 k[2]: 0 k[3]: 0 k[4]: 0 k[5]: 0 k[6]: 0 k[7]: 0 k[8]: 0 k[9]: 0 msg:
3. Problem CO Array and Character array ("Strings") 3.1 Specification Download program lab2cD.e, compile and run the program. * Observe that we use marcodefine SIZE 2 0 to define a constant, avoiding magic numbers. This is one of the two common ways to define a constant in C. When the is compiled, the pre-processor replaces every occurrence of SIZE with 20 program .Observe the strange values for elements of array k. Run it again and you might see the change of the strange values. The point here is that, in Ansi-C, an array element is not given an initial value such as 0, rather a garbled value that is randomly generated is given. Modify the declaration int k[SIZE]; to int kISIZE-3,5 Compile and run it again, what do you see? The point here is that if you specify initial values for one or more of the first few elements of the array, the rest elements of the array gets initial value 0. Based on this 'trick, and without using loop, modify the declaration of k so that bs initialized to all Os, as shown in the sample output below. Observe that for char array msg, which was initilized with a 11 character string literal "Hello World", the memory size allocated by compiler is 12 bytes. (Observe sizeof operator is used to get the memory size of array.) The extra 1 byte is used to store the null character * . On the other hand, a library function call of strlen, which returns the length of string. returns 11. We will discuss string library functions later in class. Complete the program by printing out all the elements of array msg, first the encoding (index of the character in the ASCII table), and then the character itself, as shown in the sample output below Observe the special (invisible) character whose index is 0, denoted as '0', at the end of the array. "O' is added automatically for you at the end of the array. For more information about "strings, see section 4.2 below .Complete the program by printing out all the elements of array msg2, first the index of the character in the ASCIl table, and then the character itself, as shown in the sample output below. Observe that the complier appends O' for the rest of space. Observe that for msg2 which is declared to be a size 20 a array and is initilized with literal Hello World", the memory size is 20 bytes, as it declared to be. Despite its larger memory size, however, same as formsg, the library function call of strlen returns 11, and printf also prints Hello World. The point here is that these library functions treat the first NO' (from left to right) as the end of the string, ignoring values thereafter Remove the comments that around the last 6 lines near the end of the . Observe that it is mse3, not ms3,that is passed to scanf as argument, That is, when passing a char array variable to scanf, no & is used. This is a big topic that we will learn later in class. Complete the program by printing out all the elements of array msg3, first the index of the character in ASCII table, and then the character itself, as shown in the sample output below Complie and run the program, entering a string with no more than 19 characters (why 19?) and without space, such as Helloworld. Observe that the complier may append 0 for the rest of space. Observe that for msg3 the memory size is 20 bytes, as it declared to be. On the other hand, the library function call of strlen returns 10, and printf prints HelLoworld Re-run the program, and enter a string with spaces, e.g., Hello World, and observe that only Hello is stored in the array. Accordingly, printf prints Hello and strlen returns 5 This examplifies an issue with reading string simply using scanf"s it reads input character by character until it encounters a white-space character or a new line character Thus if the input string contains white spaces it cannot be fully read in. (You are not asked to fix this.) Another issue of simply using scanf ("%s") is that there is no way to detect when the argument array is full. Thus, it may store characters pass the end of the array, causing undfined hehavior (don't try that). Later we will see other ways to read in a string that contains spaces, and control the input length. 3.2 Sample Inputs/Outputs red 361 % gcc ab2C0.c -1ab2c0 red 362 % ab2c0 k[0 0 k[1] 0 k [2]: 0 k[3] 0 k[4]: O k[5] 0 k[6]: 0 k[7]: 0 k[8]: O k [9): 0 msg: Hello world memory size of msg: 12 (bytes) strlen of msg: 11 msg [0] 72 H msg[1] 101 e msg [2] 108 1 msg[3] 108 1 msg [4] 111 o msg[5] 32 msg [6] 119 w msg[7] 111 o msg[8] 114 r msg [9] 108 1 msg [10] 100 d msg [11] 0 msg2: Hello world memory size of msg2: 20 (bytes) strlen of msg2: 11 msg2 [0] 72 H msg2[1] 101 e msg2 [2] 108 1 msg2 [3] 108 1 msg2 [4] 111 o msg2 [5] 32 msg2 [6] 119 w msg2 [7] 111 o msg2 [8] 114 r msg2 [9] 108 1 msg2 [10] 100 d msg2 [11] 0 msg2 [12] 0 msg2 [13] 0 msg2 [14] 0 msg2 [15] 0 msg2[16] 0 msg2 [17] 0 msg2 [18] 0 msg2 [19] 0 3. Problem CO Array and Character array ("Strings") 3.1 Specification Download program lab2cD.e, compile and run the program. * Observe that we use marcodefine SIZE 2 0 to define a constant, avoiding magic numbers. This is one of the two common ways to define a constant in C. When the is compiled, the pre-processor replaces every occurrence of SIZE with 20 program .Observe the strange values for elements of array k. Run it again and you might see the change of the strange values. The point here is that, in Ansi-C, an array element is not given an initial value such as 0, rather a garbled value that is randomly generated is given. Modify the declaration int k[SIZE]; to int kISIZE-3,5 Compile and run it again, what do you see? The point here is that if you specify initial values for one or more of the first few elements of the array, the rest elements of the array gets initial value 0. Based on this 'trick, and without using loop, modify the declaration of k so that bs initialized to all Os, as shown in the sample output below. Observe that for char array msg, which was initilized with a 11 character string literal "Hello World", the memory size allocated by compiler is 12 bytes. (Observe sizeof operator is used to get the memory size of array.) The extra 1 byte is used to store the null character * . On the other hand, a library function call of strlen, which returns the length of string. returns 11. We will discuss string library functions later in class. Complete the program by printing out all the elements of array msg, first the encoding (index of the character in the ASCII table), and then the character itself, as shown in the sample output below Observe the special (invisible) character whose index is 0, denoted as '0', at the end of the array. "O' is added automatically for you at the end of the array. For more information about "strings, see section 4.2 below .Complete the program by printing out all the elements of array msg2, first the index of the character in the ASCIl table, and then the character itself, as shown in the sample output below. Observe that the complier appends O' for the rest of space. Observe that for msg2 which is declared to be a size 20 a array and is initilized with literal Hello World", the memory size is 20 bytes, as it declared to be. Despite its larger memory size, however, same as formsg, the library function call of strlen returns 11, and printf also prints Hello World. The point here is that these library functions treat the first NO' (from left to right) as the end of the string, ignoring values thereafter Remove the comments that around the last 6 lines near the end of the . Observe that it is mse3, not ms3,that is passed to scanf as argument, That is, when passing a char array variable to scanf, no & is used. This is a big topic that we will learn later in class. Complete the program by printing out all the elements of array msg3, first the index of the character in ASCII table, and then the character itself, as shown in the sample output below Complie and run the program, entering a string with no more than 19 characters (why 19?) and without space, such as Helloworld. Observe that the complier may append 0 for the rest of space. Observe that for msg3 the memory size is 20 bytes, as it declared to be. On the other hand, the library function call of strlen returns 10, and printf prints HelLoworld Re-run the program, and enter a string with spaces, e.g., Hello World, and observe that only Hello is stored in the array. Accordingly, printf prints Hello and strlen returns 5 This examplifies an issue with reading string simply using scanf"s it reads input character by character until it encounters a white-space character or a new line character Thus if the input string contains white spaces it cannot be fully read in. (You are not asked to fix this.) Another issue of simply using scanf ("%s") is that there is no way to detect when the argument array is full. Thus, it may store characters pass the end of the array, causing undfined hehavior (don't try that). Later we will see other ways to read in a string that contains spaces, and control the input length. 3.2 Sample Inputs/Outputs red 361 % gcc ab2C0.c -1ab2c0 red 362 % ab2c0 k[0 0 k[1] 0 k [2]: 0 k[3] 0 k[4]: O k[5] 0 k[6]: 0 k[7]: 0 k[8]: O k [9): 0 msg: Hello world memory size of msg: 12 (bytes) strlen of msg: 11 msg [0] 72 H msg[1] 101 e msg [2] 108 1 msg[3] 108 1 msg [4] 111 o msg[5] 32 msg [6] 119 w msg[7] 111 o msg[8] 114 r msg [9] 108 1 msg [10] 100 d msg [11] 0 msg2: Hello world memory size of msg2: 20 (bytes) strlen of msg2: 11 msg2 [0] 72 H msg2[1] 101 e msg2 [2] 108 1 msg2 [3] 108 1 msg2 [4] 111 o msg2 [5] 32 msg2 [6] 119 w msg2 [7] 111 o msg2 [8] 114 r msg2 [9] 108 1 msg2 [10] 100 d msg2 [11] 0 msg2 [12] 0 msg2 [13] 0 msg2 [14] 0 msg2 [15] 0 msg2[16] 0 msg2 [17] 0 msg2 [18] 0 msg2 [19] 0Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started