This page is under construction

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 5
Question 2

n1 n2 n3
1 1 1
1 1 2
1 2 1
1 2 2
1  


Chapter 5
Question 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 1


10 1

5  2

2 3

1 4

Chapter 6
Question 3

total = 0 ;
for ( i = 0 ; i< 10 ; i++ )
{
  scanf ( "%d", &n ) ;
  total += n ;
 


Chapter 6
Question 5

See programming pitfall number 5.

There is a limit to the precision with which floating-point numbers are represented
internally in a computer. The variable i may never be
equal 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 while
loop never stops (an infinite loop) and you'll have to press control and c together to
stop 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 7
Question 1

(a) [0] to [7]
(b) [0] to [3]
(c) [0][0] to [5][2]
(d) [0][0] to [3][3


Chapter 7
Question 2

(a) float float_array[10]
(b) char char_array[5]
(c) int int_array[7][8]
(d) double double_array[10][5]
(e) long long_array[10][8][15


Chapter 7
Question 3

c1=5 c2=2

Chapter 8
Question 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 8
Question 3

1 1

1 1

15 1

12 1

12 12

13 12

13 50

Chapter 9
Question 2


The 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 3


Since the name of an array is the same as a pointer (an address) to the first element
of 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 10
Question 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[7]= { '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 10
Question 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"[2] is the third element of the array.
(i)

xt



Chapter 10
Question 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 11
Question 4

The value of var is 1

Chapter 11
Question 18

(a)

10
20
30
40
50
60
70
80
90
100

(b)

10
10
10
10
10
10
10
10
10
10

Chapter 12
Question 1

(a)

struct playing_card
{
  char suit ; /* S=Spades,H=Hearts,D=Diamonds,C=Clubs */
  int value ;
} ;

 

(c)

struct library_book
{
  char ISBN[14] ;
  char title[31] ;
  char author[26] ;
  float price ;
} ;

 

(e)

struct date
{
  int day ;
  int month ;
  int year ;
} ;

typedef struct date DATE ;

struct transaction
{
  char type[1] ;
  DATE date_of_transaction ;
  long amount ;
} ;

 

(g)

struct position
{
  int degrees ;
  int minutes ;
  char direction ;
} ;



Chapter 12
Question 2

(a)

d.a = 1 ;
d.b = 2 ;

 

(b)

d->a = 1 ;
d->b = 2 ;

 

(c)

(*d).a = 1 ;
(*d).b = 2 ;


Chapter 12
Question 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 13
Question 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 13
Question 2

(a)

r+

(b)

a+

(c)

w+



Chapter 14
Question 1

(a)

#define EQUALS ==

(b)

#define SPACE ' '

(c)

#define BYTE char

(d) #define AND &&
(e) #define OR ||


Chapter 14
Question 8

#DEFINE TRUNCATE(N)  ((int)(N))

© 2000 - 2012 powered by
Doteasy Web Hosting