You are on page 1of 17

OLYMPIA COLLEGE SCHOOL OF COMPUTING DIPLOMA IN INFORMATION TECHNOLOGY INDIVIDUAL ASSIGNMENT INTRODUCTION TO PROGRAMMING

NAME AS PER IC / PASSPORT: COLLEGE INDEX NO: IC NO / PASSPORT NO: BATCH NO:

CAMPUS: PETALING JAYA

Table of content

Table of content NO Content 1. 2. 3. 4. 5. 6. 7. 8. Page i

Acknowledgement...........................

Table of content............................... ii List of figure.................................... iii Question 1........................................ 1 Answer 1......................................... 2-8 Question 2....................................... 9

Answer 2......................................... 10-12 Reference......................................... 13

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus ii

Table of content

List of figure NO Content Question 1


1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Figure 1.1........................................................... 2 Figure 1.2........................................................... 2 Figure 1.3........................................................... 3 Figure 1.4........................................................... 3 Figure 1.5........................................................... 4 Figure 1.6........................................................... 4 Figure 1.7........................................................... 5 Figure 1.8........................................................... 5 Figure 1.9........................................................... 6 Figure 1.10......................................................... 6 Figure 1.11......................................................... 7 Figure 1.12......................................................... 7 Figure 1.13......................................................... 8 Figure 1.14......................................................... 8

Page

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus iii

Question 1

Question 1 Given the following declarations: int m = 1, n = 2, i = 0, j = 5; Given the value of each expression, and state the values of the variables after the expression is evaluated. Consider each separately; that is, assume each one appears immediately after the given declarations.

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 1

Question 1 (Answer 1)

1. m = n++ + 3

Replace m = 1 and n = 2.

The question above shown that m is equal to n++ plus 3. So, I had key in the code that to do this question. It is show that m = 5 that is because n++ + 3 same as (2) + 3. As we know, if ++ is put at the back of the integer then the value will be nothing changes. Then the answer is 5. Next, the value of n is 3 that are because + 3 then the answer is 3 in this question.

The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; m= n++ +3; printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.1: Input for this question

Figure 1.2: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 2

Question 1 (Answer 1)

2. m = ++n

Replace m = 1 and n = 2.

The question number 2 above shown that m is equal to ++n. I had key in the code that to do this question. It is show that m = 3 that is because ++n same as 1 + (2). Then the answer is 5. Next, the value of n is 3 that are because m = ++ same as (1) + 2. As we know, if ++ is put front of the integer then the value will be add 1. Then the answer is 3 based on this question. The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; m= ++n; printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.3: Input for this question

Figure 1.4: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 3

Question 1 (Answer 1)

3. m = (n = 5) + 2

Replace i = 0, m = 1 and n = 2.

The third question shown that m is equal to (n = 5) plus 2. So, I had applied the code to do this question. It is show that m = 7 that is because (n = 5) then n = 5, then n + 2 same as (5) + 2. Then the answer for m will be equal to 7. Although that, the value of n is 5 that are because (n = 5) then n = 5. Then the answer for n is 5 in this question.

The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; m= (n = 5) + 2; printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.5: Input for this question

Figure 1.6: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 4

Question 1 (Answer 1)

4. i -- && (4 * ++m <= 4 || ++n 2)

Replace i = 0, m = 1 and n = 2

For the forth question, the question is shown as i -- && (4 * ++m <= 4 || ++n minus 2). So, I had prepared the code to do this question. It is show that i = -1 that is because i -- && and i-- is -1. Then the answer for m is equal to 1. So then, the answer for n is 2 in this question.

The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; i-- && (4 * ++m <= 4 || ++n - 2); printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.7: Input for this question

Figure 1.8: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 5

Question 1 (Answer 1)

5.

--i || (4 * ++m <= 4 && n++ 2)

Replace i = 0, m = 1 and n = 2

For this question, is shown as --i || (4 * ++m <= 4 && n++ minus 2). So, I had prepared the code to do this question. It is show that i = -1 that is because -- i && and -- i is 1. Next the answer for m is equal to 1 and for n is 2 in this question.

The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; --i || (4 * ++m <= 4 && n++ - 2); printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.9: Input for this question

Figure 1.10: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 6

Question 1 (Answer 1)

6.

--i && (4 * ++m <= 4 && n++ 2)

Replace i = 0, m = 1 and n = 2

For the sixth question, it is shown as --i || (4 * ++m <= 4 && n++ minus 2). So, I had prepared the code to do this question and it is show that i = -1 that is because -- i && and -- i is -1. Then the answer for m is equal to 2. At the last, answer for n is 2 in this question.

The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; --i && (4 * ++m <= 4 && n++ - 2); printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.11: Input for this question

Figure 1.12: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 7

Question 1 (Answer 1)

7. j > n > m

Replace j = 5, m = 1 and n = 2

For the last question, the question is shown as j > n > m. So, I had prepared the code to do this question. It is show that j = 5 that is because j is larger than n then j = 5. Then for n will be equal to 2 because is smaller than j but bigger than m. The answer for n is 2. Although that, the value of m is 1 that are m is smaller than n so it remain no changes. Then the answer for this question is j = 5, m = 1 and n = 2.

The code like below: #include <stdio.h> int main (void) { int m=1, n=2, i=0, j=5; j > n > m; printf("Output m :%d\n",m); printf("Output n :%d\n",n); printf("Output i :%d\n",i); printf("Output j :%d\n",j); return 0; }

Figure 1.13: Input for this question

Figure 1.14: Output for this question

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 8

Question 2

Question 2 What is printed by each piece of code?

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 9

Question 2 (Answer 2)

1.char fred[50]; strcpy(fred, "Today"); if (fred == "Today") printf("Of course!\n"); else printf("Today is not Today on planet C.\n");

The formula (fred == Today) is not true because fred is not equal to the code Today. So, the first printf( ) statement will not display. Then the output will be shown as (Today is not Today on planet C.\n) like the figure below. Which mean the output is Today is not Today on planet C.

2. char fred1[] = "barney", fred2[20] = "barney", *fred3 = "barney"; printf("%d %d %d %d %d %d\n", strlen(fred1), strlen(fred2), strlen(fred3), sizeof fred1, sizeof fred2, sizeof fred3);

For this question, the coding above shows that the output with 6, 6, 6, 7, 20, 7 is because start from the coding on the second line. The printf is to print the output from the variable which include the strlen(fred1), strlen(fred2), strlen(fred3),sizeof fred1, sizeof fred2, sizeof fred3. So, for the strlen is meaning string length of the fred1,fred2 and fred3 and the answer is 6, 6, 6 because b a r n e y. Then what is the size of fred1,fred2 and fred3, so the question is the size of the fred for each one and the answer is 7, 20, 7 because the size of array for every single fred. Lastly, the output shown 6, 6, 6, 7, 20, 7.

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 10

Question 2 (Answer 2)

3. char sue[] = "susan", *p; for(p = sue; *p++; ) printf("%s", p); printf("\n");

For question three, the coding above show susan is the main word actually and *p is just a variable. Then for the second line actually a looping, to loop the susan and for every time looping will minus one character from the main word. After that, %s is to display the second line which means will get the output with usansanann. For the \n is to make a new line. That is for this question.

4. char bill[50] = "william"; strcpy(bill + 3, "ey coyote"); printf("%s\n",bill);

For this question, the coding above bill at the second line actually referring to the first line to get back the words and plus 3 is get 3 alphabets from William. So, combine the 3 alphabets with the ey coyote. After that, %s is to display the copy string at the second line which means will get the output with wiley coyote.

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 11

Question 2 (Answer 2)

5.char *sharon = "sharon"; while(*++sharon) printf("%c", sharon[-1]); printf("\n");

The %c is to display the input from the first line. If, sharon[-1] that mean the output will be minus one character start from the last character of the word. For the \n is to make a new line. That is for this question.

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 12

Reference

Reference Book Computer Fundamentals and programming in C, Pradip Dey and Manas Ghosh,
Chapter 8 16.

Internet http://www.cs.umbc.edu/courses/104/spring02/burt/C_summary.html http://www.cs.cf.ac.uk/Dave/C/node4.html#SECTION00400000000000000000

_____________________________________
Diploma in Information Technology Olympia College Petaling Jaya Campus 13

You might also like