Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each pair of values, explain the difference between the values , if there is

ID: 3733620 • Letter: F

Question

For each pair of values, explain the difference between the values, if there is one. If they are the same, briefly explain why.

'' and 0x90

'' and 16

i++ and ++i

Indicate how many bytes are allocated by each line of code, as well as initial values of variables on that line. For multiple values, you can give one answer for all, or comma-separate. For junk, put JUNK.

Bytes               Values


enum bool {FALSE, TRUE};            ________    ________

int a[2]; ________    ________

void main() {                       

    static char array[6];           ________    ________

    const int b = 7;                ________    ________

    {                        

        enum bool a, b = TRUE;      ________    ________

    }                        

    printf("Hello Student. ");     ________    ________

}

Error Finding

Line #                          Code

0000   /* This program finds the two

0001   smallest numbers in an array */

0002   #include <stdio.h>

0003   #define ARRAY_SIZE = 4

0004   int arr[ARRAY SIZE]={-1,4,5,6};

0005   int FST_sml, SND_sml;

0006  

0007   void main() {

0008       int i;

0009

0010       for (i=0; i<ARRAY_SIZE;i--);

0011          if (arr[i] < fst_sml)

0012              fst_sml = arr[i];

0013              snd_sml = fst_sml;

0014          else if (arr[j]<snd_sml)

0015              snd_sml = arr[i]

0016

0017       printf('1st smallest: %s ', fst_sml);

0018       printf("2nd smallest: %s ", snd_sml);

0019       return "OK";

0020   }

Error    Lines                What & Why

1.         _______           _____________________

2.         _______           _____________________

3.         _______           _____________________


4.         _______           _____________________

5.         _______           _____________________

6.         _______           _____________________

7.         _______           _____________________

8.         _______           _____________________

9.         _______           _____________________

10.       _______           _____________________

Explanation / Answer

For each pair of values, explain the difference between the values, if there is one. If they are the same, briefly explain why.

'' represents Character

'0x90' represends a Hexadecimal number with value (9*161+0*160) = 144

If 'char' is "unsigned" (only positive numbers) then 0x90 = 144, which is no problem to hold.

If char is "signed" Meaning that one bit is reserved to indicate positive or negative (the sign bit). Therefore only 7 bits are used to represent the maximum positive number. 2^7 = 128. When you try to assign 0x90 to char, it is therefore larger than the largest positive value. This is signed overflow and undefined behavior.

Most implementations will just wrap around to the negatives, so it instead becomes -128 - (128-144) = -128 + 16 = -112.

The bits may be the same, but the interpretation is not.

'' represents a Character with value (1*161+0*160)=16

16 represents an Integer with value 16

For the increment in value of 'i' both statements are equal.

Pre Increment(++i): Value of 'i' is incremented before the evaluation of Expression.

Post Increment (i++): Value of 'i' is incremented after the evaluation of Expression.

Indicate how many bytes are allocated by each line of code, as well as initial values of variables on that line. For multiple values, you can give one answer for all, or comma-separate. For junk, put JUNK.

Bytes               Values


enum bool {FALSE, TRUE};            ____4____    ____null____

int a[2]; ____8____ ___0, 0____

void main() {                       

    static char array[6];           ____6___    null,null,null,null,null,null

    const int b = 7;                ____4____    ____7____

    {                        

        enum bool a, b = TRUE;      ___4,4__ __null,1____

    }                        

    printf("Hello Student. ");     ____4___ _Hello Student. _

}

PS: The values are based on GCC Compiler.

Error Finding

Line #                          Code

0000   /* This program finds the two

0001   smallest numbers in an array */

0002   #include <stdio.h>

0003   #define ARRAY_SIZE = 4

0004   int arr[ARRAY SIZE]={-1,4,5,6};

0005   int FST_sml, SND_sml;

0006  

0007   void main() {

0008       int i;

0009

0010       for (i=0; i<ARRAY_SIZE;i--);

0011          if (arr[i] < fst_sml)

0012              fst_sml = arr[i];

0013              snd_sml = fst_sml;

0014          else if (arr[j]<snd_sml)

0015              snd_sml = arr[i]

0016

0017       printf('1st smallest: %s ', fst_sml);

0018       printf("2nd smallest: %s ", snd_sml);

0019       return "OK";

0020   }

Error    Lines                What & Why

1. 0003 #define ARRAY_SIZE 4 { '=' is Incorrect}

2. 0010 for(i=0;i<ARRAY_SIZE;i++) { Loop counter 'i' should be increased for functioning of Loop}

3. 0010   for(i=0;i<ARRAY_SIZE;i++) ; {Semicolon ';' will limit the body of for loop}

4.         0011 {} Braces for body of 'for()' are missing

5.         0011 'fst_sml' is not initialized. {Initialized variable is 'FST_sml'}

6.         0012 'fst_sml' is not initialized. {Initialized variable is 'FST_sml'

7.         0013 'fst_sml' and 'snd_sml' are not initialized. {Initialized variable are 'FST_sml' and SND_sml}

8. 0014 variable 'j' is not declared in scope. {Declared variable is 'i' for 'for() loop' }

9. 0015 'snd_sml' is not initialized. {Initialized variable is 'SND_sml'}

10.       0017 '%s' is used in printf() instead of '%d' { for integer type of Output we use '%d' }

11. 0018   '%s' is used in printf() instead of '%d' { for integer type of Output we use '%d' }

12. 0019 void main() will not return any value "ok" {We can use 'return;' only with void functions}