Download C Programming Exam for Bachelor of Engineering Students - Autumn 2006 and more Exams Computer Science in PDF only on Docsity! Cork Institute of Technology Bachelor of Engineering in Communications Systems - Award Bachelor of Engineering in Electronic Automation and Robotics - Award Bachelor of Engineering in Applied Electronics Design - Award (NFQ Level 7) Autumn 2006 Computer Science (Time: 2 Hours) Answer any two questions [each 50 marks] Maximum available marks is 100. Use appropriate comments and indentation in program code. Examiners: Mr. P Cogan Mr. J. Berry Dr. R. O Dubhghaill Q1. (a) Explain why the use of global variables is normally avoided in program development. [10 marks] (b) Explain using appropriate examples what the ‘#define’ directive is used for in the C language. [10 marks] (c) Demonstrate how functions are used in C by writing a program that reads in permittivity (ε), area (A) and distance (d) and which then passes these values to a function. The function should calculate capacitance (C) using; d AC ε= and return the result. The main program should then display this value (C). [10 marks] (d) Explain what each line does in each of the following two programs. [20 marks] (i)#include <stdio.h> void main (void) { signed int a = 2, b = 4, c = 0x8; float d = 1e1; a==b; b-=c++; printf ("%d\t%d\t%d\n",a,b,c); a+= c = 5*(int)d; fprintf (stdout,"%d\t%4.2f\t%d\n",a,d,c); } continued overleaf …. 2 (ii)#include <stdio.h> main () { float i[5]={10.0,11.0,12.0,13.0,14.0}; float *p = &i[1]; int h; h=(int)i[2]; h<<=1; printf ("%d\t%4.1f\n",h,*p); printf("%d\t%4.1f\t%u\t%u\n",--h,*p,p,(p++)); } Q2. (a) Using example programs to illustrate your answer write a note discussing any TWO of the following with reference to the C programming language; (i) The usage of the ‘continue’ statement in C. (ii) The use of arrays. (iii) Storage classes. (iv) Pass by reference. [20 marks] (b) What does the following program display on the screen? Explain your answer. [12 marks] #include <stdio.h> const unsigned long MASK=0x10000002; void main () { unsigned long y= MASK; long int z=256; char c='M'; printf ("131 ^ y = %lx\n",131^y); printf ("0x07 | MASK = %x\n",0x07|MASK); printf ("c += 2 = %c\n",c += 2); printf ("c+('a'-'A')=%c\n",c +('a'-'A')); printf ("Z >>=3 = %d\n",z>>=3); printf ("~y = %lx\n",~y); } (c) The Type Of Service (TOS) bits are a set of four-bit flags in the IP header of a packet of network data (datagram). When any one of these bit flags is set, routers may handle the datagram differently to datagrams without any TOS bits set. Produce a program to find the type of service being requested in the IP header. The program should allow a technician to check the byte that the TOS bits are in by reading in the hexadecimal number that this byte contains. Your program should report on which type of service (if any) is requested using the table overleaf and repeat until the user decides to quit. continued overleaf ….