  • Chapter 2
• Chapter 3
• Chapter 4
• Chapter 5
• Chapter 6
• Chapter 7
• Chapter 8
• Chapter 9
• Chapter 10
• Chapter 11
• Chapter 12
• Chapter 13
• Chapter 14

Chapter 2
Question 1

 Variable name Valid? Good Mnemonic? stock_code YES YES money\$ NO. Reason: \$ is not a valid character. Jan_Sales YES YES X-RAY NO. Reason: - is not a valid character. YES int NO. Reason: int is a keyword. xyz YES. NO. Reason: not a meaningful name. invoice_total YES. YES. john's_exam_mark NO. Reason: ' is not a valid character.

Chapter 2
Question 4

 (a) int number_of_transactions, age_in_years ; (b) float total_pay,  tax_payment,  distance, average ; (c) char account_type ; (d) double grosss_pay ;

Chapter 2
Question 11

 13 `printf( "v1 has the value %c\n",v1 ) ;` `/* The letter A is displayed  */` 15 `printf( "v3 has the value %d\n",v3 ) ;` `/* The number 97 is displayed */`

The value of all variables are held in binary (0s and 1s) inside a digital computer. Even the value of a char variable such as v3, which holds a letter, is stored in binary. Letters and all other characters have to be stored as a numerical value inside the computer. The numerical equivalent of each character is given in the ASCII table on pages 183 and 184. For example, the letter a has an ASCII value of 97 (binary 01100001) and the letter A has an ASCII value of 65 (binary 01000001).

The format specifiers %d and %c in a printf statement allow you to display the binary value of a variable in different ways.

Line 13 now displays the value of V1 as a character. The value of  v1 is 65, which, according to the ASCII table on page 184 is the character A.

v3 was assigned 'a' (ASCII code 97) on line11 of program P2C and is displayed as a decimal number on line 15.

Chapter 3
Question 2

 (a) 3 (b) 1 (c) 7 (d) 100 (e) 2 (f) 45 (g) 0

Chapter 3
Question 3

 (a) d = 2 * ( b + c ) ; (b) d = 5 * b + 9 * c ; (c) d = b - 3 * 19 ; (d) d = b * c + 10 ; (e) d = ( a + b ) / c ; There is no error in this statement.

Chapter 3
Question 5

 a b c d (a) 13 0 3 undefined (b) 13 -1 3 undefined (c) 13 -1 4 4 (d) 13 -1 3 4 (e) 14 -1 3 11 (f) 15 0 2 10

Chapter 3
Question 8

 (a) float (b) float (c) long (d) double Note: Casting int_val to double is not strictly necessary, since it would be promoted automatically. (e) float (f) double Note: Floating-point constants such as 3.0 are stored as double types. If you wanted to store 3.0 as type float, then use a cast, e.g. int_val + (float)(3.0). The entire expression is now of type float.

Chapter 4
Question 1
`scanf("%d",&num) ;`
see Programming pitfalls 1 and 4.

Chapter 4
Question 2

 (a) `scanf( "%d", &first ) ;` (b) `scanf ( "%d%d%d", &second, &third, &fourth ) ;` (c) `scanf ( "%f%f%f", &principal, &rate, &time ) ;` (d) `scanf ( "%c%c", &keyval1, &keyval2 ) ;` (e) `scanf ( "%ld%ld%ld", &total1, &total2, &total3 ) ;` (f) `scanf ( "%c%d%f%ld%lf", &c, &i, &f, &l, &d ) ;`

Chapter 4
Question 4
`printf( "v1 =%2.0f   v2 =%5.1f   v3 =%6.2f\n", v1, v2, v3 ) ;Chapter 5Question 2`
 n1 n2 n3 1 1 1 1 1 2 1 2 1 1 2 2 1
`Chapter 5Question 8/* Simple four-function calculator. *//* This program illustrates the use of the switch statement.*/#include <stdio.h>main(){char operator ;float num1, num2, answer ;printf( "Please enter an arithmetic expression (e.g. 1 + 2) ") ;scanf( "%f%1s",&num1,&operator ) ;if ( operator != 'D' && operator != 'd' &&     operator != 'I' && operator != 'i')  /* read a 2nd operand for all operators except increment and decrement */   scanf( "%f", &num2 ) ;switch (operator){case '+' :  answer = num1 + num2 ;  printf( "%f plus %f equals %f\n",num1,num2,answer ) ;  break ;case '-' :  answer = num1 - num2 ;  printf( "%f minus %f equals %f\n",num1,num2,answer ) ;  break ;case '*' :  answer = num1 * num2 ;  printf("%f multiplied by %f equals %f\n",num1,num2,answer) ;  break ;case '/' :  answer = num1 / num2 ;  printf( "%f divided by %f equals %f\n",num1,num2,answer ) ;  break ;case 'I':case 'i':  printf("%f incremented by 1 equals %f\n",num1,++num1) ;  break ;case 'D':case 'd':  printf("%f decremented by 1 equals %f\n",num1,--num1) ;  break ;default :  printf( "Invalid operator\n" ) ;}}Chapter 6 Question 110 15  22 31 4Chapter 6Question 3`
 total = 0 ; for ( i = 0 ; i< 10 ; i++ ) { scanf ( "%d", &n ) ; total += n ;
`Chapter 6Question 5See programming pitfall number 5.There is a limit to the precision with which floating-point numbers are representedinternally in a computer. The variable i may never beequal to 100.0 exactly. Using the compiler supplied on the CD-ROM, the closest i can get to 100.0 is 99.999046 or 100.099045.  Since i is never equal to 100.0, the whileloop never stops (an infinite loop) and you'll have to press control and c together tostop the program.`

The solution to this problem is not to test for 100.0 exactly in the while loop. You want to stop the loop as soon as i goes over 100.0. Change the while loop to:

while ( i <= 100.0)

`Chapter 7Question 1`
 (a)  to  (b)  to  (c)  to  (d)  to [3
`Chapter 7Question 2`
 (a) float float_array (b) char char_array (c) int int_array (d) double double_array (e) long long_array[15
`Chapter 7Question 3c1=5 c2=2Chapter 8Question 1/* Program Name: ex1c8.c                 Exercise 1 chapter 8, program to display addresses of variables.    Written by : Paul Kelly    Date       : 31/3/99                                                         */#include <stdio.h>main(){char c = 'a' ;int i = 1 ;long l = 123456 ;float f = 125.5 ;double d = 1234.25 ;printf("Address of c is %p\n", &c) ;printf("Address of i is %p\n", &i) ;printf("Address of l is %p\n", &l) ;printf("Address of f is %p\n", &f) ;printf("Address of d is %p\n", &d) ;}Chapter 8Question 31 11 115 112 112 1213 1213 50Chapter 9 Question 2The letters HELLO are displayed.`

Starting with i=0, *(c+i) is the letter G. From the ASCII table, *(c+i) is H.
When i is 1, *(c+i) is the leter D and so *(c+1)+1 is E ...etc.

`Chapter 9	Question 3Since the name of an array is the same as a pointer (an address) to the first elementof the array, scanf( "%d" , a+i ) ;reads in a value for each of the five elements of the array a.`

The for loop

for ( p = a ; p< a+5 ; p++ )

assigns p to a, then a+1, a+2, a+3 and finally a+4.
Therefore the statement

printf ( " %d", *p) ;

displays the values *a, *(a+1), *(a+2), *(a+3) and *(a+4). In effect, the elements of the array a are displayed with a blank between them.

`Chapter 9 Question 4`
 (a) 1 5 -1 (b) b f d (c) 1.25 5.25 6.5 (d) 3 7 10
`Chapter 10Question 2`
 (a) name is a character array. However, since it does not end with the character '\0'  it is not a proper C string. To correct the problem place '\0' after 't' in name or define name as: char *name = "Robert" ; (b) name is the name of an array and as such is a pointer (address) to the first element 'R'. Therefore the & in &name is not necessary. (c) name contains 6 elements. The string "Philip" is 7 characters long (don't forget the '\0' at the end of a C string). To correct the problem define name as: char name= { 'R', ... (d) "a" should be 'a' (single characters in C are enclosed in single quotation marks). (e) To copy a string in C you must use strcpy. In addition,  name is too small to hold "Philip" (see (c) above).
`Chapter 10Question 3`
 Comments (a) some text (b) s (c) m "more text" is a string or array of characters. Strings are pointers. * in front of a pointer gives the value stored at that pointer. (d) o (e) ome text (f) some text You don't always need %s, but see programming pitfall 10. (g) some The blank character in text is replaced by '\0'. This indicates the end of the string to the %s in the printf. (h) x "text" is an array of characters. "text" is the third element of the array. (i) xt
`Chapter 10Question 4`
 (a) a string (b) a string of text. (c) string of text. (d) ring of text.
`Chapter 11 Question 1`
 (a) char f1(int) ; int f2(long int) ; void f3(int, int) ; char * f4(char *) ; float f5(void) ; void f6(void) ; (b) char c_val ; int i_val ; int another_i_val ; long int l_val ; float f_val ; char *ptr_to_string ; char *another_ptr_to_string ; /* Don't forget to initialise any variables you are passing to    a function.                                               */ ... c_val = f1(i_val) ; i_val = f2(l_val) ; f3( i_val, another_i_val) ; ptr_to_string = f4(another_ptr_to_string) ; f_val = f5(); f6();
`Chapter 11Question 4 The value of var is 1Chapter 11Question 18`
 (a) 10 20 30 40 50 60 70 80 90 100 (b) 10 10 10 10 10 10 10 10 10 10
`Chapter 12Question 1`
 (a) struct playing_card {   char suit ; /* S=Spades,H=Hearts,D=Diamonds,C=Clubs */   int value ; } ; (c) struct library_book {   char ISBN ;   char title ;   char author ;   float price ; } ; (e) struct date {   int day ;   int month ;   int year ; } ; typedef struct date DATE ; struct transaction {   char type ;   DATE date_of_transaction ;   long amount ; } ; (g) struct position {   int degrees ;   int minutes ;   char direction ; } ;
`Chapter 12Question 2`
 (a) d.a = 1 ; d.b = 2 ; (b) d->a = 1 ; d->b = 2 ; (c) (*d).a = 1 ; (*d).b = 2 ;
`Chapter 12Question 3`
 (a) stock.no = 1 ; strcpy( stock.description , "any description" ) ; stock.price = 2.34 ; stock.qty = 5 ; (b) scanf ( "%d %s %f  %d", &stock.no, stock.description, &stock.price, &stock.qty ) ; (c) printf ( "%d %s %f  %d", stock.no, stock.description, stock.price, stock.qty ) ;
`Chapter 13Question 1`

supp_ptr = fopen ( "supp.dat" , "rb" ) ;

cust_ptr = fopen ( "cust.dat" , "r+b" ) ;

fp = fopen ( "temp.dat" , "w" ) ;

price_file = fopen ( "price.dat" , "a" ) ;

`Chapter 13Question 2`
 (a) r+ (b) a+ (c) w+

`Chapter 14Question 1`
 (a) #define EQUALS == (b) #define SPACE ' ' (c) #define BYTE char (d) #define AND && (e) #define OR ||
`Chapter 14Question 8#DEFINE TRUNCATE(N)  ((int)(N))` © 2000 - 2012 powered by 