Sie sind auf Seite 1von 6

C programming Homework4

10.5 Provide the definition for each of the following structures and unions: a) Structure inventory containing character array partName[ 30 ], integer partNumber, floating point price, integer
stock and integer reorder.

ANS:
struct inventory { char partName[ 30 ]; int partNumber; float price; int stock; int reorder; };

b) Union data containing char c, short s, long b, float f and double d. ANS:
union data { char c; short s; long l; float f; double d; };

c) A structure called address that contains character arrays


streetAddress[ 25 ], city[ 20 ], state[ 3 ] and zipCode[ 6 ].

ANS:
struct address { char streetAddress[ 25 ]; char city[ 20 ]; char state[ 3 ]; char zipCode[ 6 ]; };

d) Structure student that contains arrays firstName[ 15 ] and lastName[ 15 ] and variable homeAddress of type struct
address from part (c).

ANS:
struct student { char firstName[ 15 ]; char lastName[ 15 ]; struct address homeAddress; };

e) Structure test containing 16 bit fields with widths of 1 bit. The names of the bit fields are the letters a to p. ANS:
struct test { unsigned a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1, i:1, j:1, k:1, l:1, m:1, n:1, o:1, p:1; };

10.6 Given the following structure and variable definitions,


struct customer { char lastName[ 15 ]; char firstName[ 15 ]; int customerNumber; struct { char phoneNumber[ 11 ]; char address[ 50 ];

C programming Homework4
char city[ 15 ]; char state[ 3 ]; char zipCode[ 6 ]; } personal; } customerRecord, *customerPtr; customerPtr = &customerRecord;

write an expression that can be used to access the structure members in each of the following parts: a) Member lastName of structure customerRecord. ANS: customerRecord.lastName b) Member lastName of the structure pointed to by customerPtr. ANS: customerPtr->lastName c) Member firstName of structure customerRecord. ANS: customerRecord.firstName d) Member firstName of the structure pointed to by customerPtr. ANS: customerPtr->firstName e) Member customerNumber of structure customerRecord. ANS: customerRecord. customerNumber f) Member customerNumber of the structure pointed to by customerPtr. ANS: customerRecord-> customerNumber g) Member phoneNumber of member personal of structure customerRecord. ANS: customerRecord.personal.phoneNumber h) Member phoneNumber of member personal of the structure pointed to by customerPtr. ANS: customerRecord->personal.phoneNumber i) Member address of member personal of structure customerRecord. ANS: customerRecord.personal.address j) Member address of member personal of the structure pointed to by customerPtr. ANS: customerRecord->personal.address k) Member city of member personal of structure customerRecord. ANS: customerRecord.personal.city l) Member city of member personal of the structure pointed to by customerPtr. ANS: customerRecord->personal.city m) Member state of member personal of structure customerRecord. ANS: customerRecord.personal.state n) Member state of member personal of the structure pointed to by customerPtr. ANS: customerRecord->personal.state o) Member zipCode of member personal of customerRecord. ANS: customerRecord.personal.zipCode p) Member zipCode of member personal of the structure pointed to by customerPtr. ANS: customerRecord->personal.zipCode

C programming Homework4
.

10.19 The following program uses function multiple to determine if the integer entered from the keyboard is a multiple of some integer X. Examine the function multiple, then determine the value of X. ANS: 1 /* ex10_19.c */ 2 /* This program determines if a value is a multiple of X. */ 3 #include <stdio.h> 4 5 int multiple( int num ); /* prototype */ 6 7 int main() 8{ 9 int y; /* y will hold an integer entered by the user */ 10 11 printf( "Enter an integer between 1 and 32000: " ); 12 scanf( "%d", &y ); 13 14 /* if y is a multiple of X */ 15 if ( multiple( y ) ) { 16 printf( "%d is a multiple of X\n", y ); 17 } /* end if */ 18 else { 19 printf( "%d is not a multiple of X\n", y ); 20 } /* end else */ 21 22 return 0; /* indicates successful termination */ 23 } /* end main */ 24 25 /* determine if num is a multiple of X */ 26 int multiple( int num ) 27 { 28 int i; /* counter */ 29 int mask = 1; /* initialize mask */ 30 int mult = 1; /* initialize mult */ 31 32 for ( i = 1; i <= 10; i++, mask <<= 1 ) { 33 34 if ( ( num & mask ) != 0 ) { 35 mult = 0; 36 break; 37 } /* end if */ 38 39 } /* end for */ 40 41 return mult; 42 } /* end function multiple */
Enter an integer between 1 and 32000: 1024 1024 is a multiple of X

X=1024 10.20 What does the following program do? ANS: 1 /* ex10_20.c */ 2 #include <stdio.h>

C programming Homework4
3 4 int mystery( unsigned bits ); /* prototype */ 5 6 int main() 7{ 8 unsigned x; /* x will hold an integer entered by the user */ 9 10 printf( "Enter an integer: " ); 11 scanf( "%u", &x ); 12 13 printf( "The result is %d\n", mystery( x ) ); 14 15 return 0; /* indicates successful termination */ 16 } /* end main */ 17 18 /* What does this function do? */ 19 int mystery( unsigned bits ) 20 { 21 unsigned i; /* counter */ 22 unsigned mask = 1 << 31; /* initialize mask */ 23 unsigned total = 0; /* initialize total */ 24 25 for ( i = 1; i <= 32; i++, bits <<= 1 ) { 26 27 if ( ( bits & mask ) == mask ) { 28 total++; 29 } /* end if */ 30 31 } /* end for */ 32 33 return !( total % 2 ) ? 1 : 0; 34 } /* end function mystery */
Enter an integer: 5678 // odd number of 1 bits in 5678 The result is 0 Enter an integer: 65 // even number of 1 bits in 65 The result is 1

9.4 Write a printf or scanf statement for each of the following: a) Print unsigned integer 40000 left justified in a 15-digit field with 8 digits. ANS: printf( %-15.8u, ( unsigned ) 40000 ); b) Read a hexadecimal value into variable hex. ANS: scanf( %x, hex ); c) Print 200 with and without a sign. ANS: printf( %+d %d\n, 200, 200 ); d) Print 100 in hexadecimal form preceded by 0x. ANS: printf( %#x\n, 100 ); e) Read characters into array s until the letter p is encountered. ANS: scanf( %[^p], s ); f) Print 1.234 in a 9-digit field with preceding zeros.

C programming Homework4
ANS: printf( %09.3f\n, 1.234 ); g) Read a time of the form hh:mm:ss, storing the parts of the time in the integer variables hour, minute and second. Skip the colons (:) in the input stream. Use the assignment suppression character. ANS: scanf( %d%*c%d%*c%d, &hour, &minute, &second ); h) Read a string of the form "characters" from the standard input. Store the string in character array s. Eliminate the quotation marks from the input stream. ANS: scanf( \%[^\], s ); i) Read a time of the form hh:mm:ss, storing the parts of the time in the integer variables hour, minute and second. Skip the colons (:) in the input stream. Do not use the assignment-suppression character. ANS: scanf( %d:%d:%d:, &hour, &minute, &second );

9.6 Find the error(s) in each of the following program segments. Explain how each error can be corrected. a) printf( "%s\n", 'Happy Birthday' ); ANS: printf( %s\n, Happy Birthday ); b) printf( "%c\n", 'Hello' ); ANS: printf( %s\n, Hello ); c) printf( "%c\n", "This is a string" ); ANS: printf( %s\n, This is a string ); d) The following statement should print "Bon Voyage":
printf( ""%s"", "Bon Voyage" );

ANS: printf( \%s\, Bon Voyage ); e) char day[] = "Sunday";


printf( "%s\n", day[ 3 ] );

ANS: printf( %s\n, day ); f) printf( 'Enter your name: ' ); ANS: printf( Enter your name: ); g) printf( %f, 123.456 ); ANS: printf( %f, 123.456 ); h) The following statement should print the characters 'O' and 'K':
printf( "%s%s\n", 'O', 'K' );

ANS: printf( %c%c\n, O, K ); i) char s[ 10 ];


scanf( "%c", s[ 7 ] );

ANS: scanf( %c, &s[ 7 ] );

10.10 Write a program that right shifts an integer variable 4 bits. The program should print the integer in bits before and after the shift operation. Does your system place 0s or 1s in the vacated bits? ANS: 1 /* Exercise 10.10 Solution */

C programming Homework4
2 #include <stdio.h> 3 4 void displayBits( unsigned value ); /* prototype */ 5 6 int main() 7{ 8 unsigned val; /* value from user */ 9 10 /* prompt user and read value */ 11 printf( "Enter an integer: " ); 12 scanf( "%u", &val ); 13 14 /* display value before shifting */ 15 printf( "%u before right shifting 4 bits is:\n", val ); 16 displayBits( val ); 17 18 /* display value after shifting */ 19 printf( "%u after right shifting 4 bits is:\n", val ); 20 displayBits( val >> 4 ); 21 22 return 0; /* indicate successful termination */ 23 24 } /* end main */ 25 26 /* function displayBits prints each bit of value */ 27 void displayBits( unsigned value ) 28 { 29 unsigned c; /* bit counter */ 30 unsigned displayMask = 1 << 15; /* bit mask */ 31 32 printf( "%7u = ", value ); 33 34 /* loop through bits */ 35 for ( c = 1; c <= 16; c++ ) { 36 value & displayMask ? putchar( '1' ) : putchar( '0' ); 37 value <<= 1; /* shift value 1 bit to the left */ 38 39 if ( c % 8 == 0 ) { /* print a space */ 40 putchar( ' ' ); 41 } /* end if */ 42 43 } /* end for */ 44 45 putchar( '\n' ); 46 } /* end function displayBits */
Enter an integer: 1234 1234 before right shifting 4 bits is: 1234 = 00000100 11010010 1234 after right shifting 4 bits is: 77 = 00000000 01001101

Das könnte Ihnen auch gefallen