Sams Teach Yourself C in 24 Hours (69 page)

/* Method 1: a C function */

int MyFunction( int x, int y)

{

int result;

result = x * y;

return result;

}

or

/* Method 2: a C function */

int MyFunction( int x, int y)

{

return (x * y);

}

5. The following is one possible solution:

/* 03A05.c */

#include

int integer_multiply( int x, int y )

{

int result;

result = x * y;

return result;

}

int main()

{

int sum;

sum = integer_multiply(3, 5);

printf(“The multiplication of 3 and 5 is %d\n”, sum);

return 0;

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 445

Answers to Quiz Questions and Exercises

445

Hour 4, “Understanding Data Types and

Keywords”

Quiz

1. Yes. Both 134/100 and 17/10 give the same result of 1.

2. Yes. The results of both 3000 + 1.0 and 3000/1.0 are floating-point values.

3. In scientific notation, we have the following expressions:

• 3.5e3

• 3.5e-3

• -3.5e-3

4. Among the four names, 7th_calculation and Tom’s_method are not valid names in C.

Exercises

1. The following is one possible solution:

/* 04A01.c */

B

#include

main()

{

char c1;

char c2;

c1 = ‘Z’;

c2 = ‘z’;

printf(“The numeric value of Z: %d.\n”, c1);

printf(“The numeric value of z: %d.\n”, c2);

return 0;

}

The output of the program is:

OUTPUT
The numeric value of Z: 90.

The numeric value of z: 122.

2. The following is one possible solution:

/* 04A02.c */

#include

main()

{

char c1;

33 067231861x AppxB 4.10.2000 11:05 AM Page 446

446

Appendix B

char c2;

c1 = 72;

c2 = 104;

printf(“The character of 72 is: %c\n”, c1);

printf(“The character of 104 is: %c\n”, c2);

return 0;

}

The output of the program is:

OUTPUT
The character of 72 is: H

The character of 104 is: h

3. No. 72368 is beyond the range of the int data type of 16 bits. If you assign a value that is too large for the data type, the resulting value will wrap around and the result will be incorrect.

4. The following is one possible solution:

/* 04A04.c */

#include

main()

{

double dbl_num;;

dbl_num = 123.456;

printf(“The floating-point format of 123.456 is: %f\n”,

dbl_num);

printf(“The scientific notation format of 123.456 is: %e\n”,

dbl_num);

return 0;

}

The output of the program from my machine is:

OUTPUT
The floating-point format of 123.456 is: 123.456000

The scientific notation format of 123.456 is: 1.234560e+002

5. The following is one possible solution:

/* 04A05.c */

#include

main()

{

char ch;

ch = ‘\n’;

printf(“The numeric value of newline is: %d\n”, ch);

return 0;

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 447

Answers to Quiz Questions and Exercises

447

The output of the program is:

OUTPUT

The numeric value of newline is: 10

Hour 5, “Handling Standard Input and

Output”

Quiz

1. Yes. By prefixing the minimum field specifier with the minus sign -.

2. The main difference between putc() and putchar() is that putc() requires the user to specify the file stream. For putchar(), the user doesn’t need to do so because the standard output (stdout) is used as the file stream.

3. The getchar() function returns a value of the int data type.

4. Within the %10.3f expression, 10 is the value of the minimum field width specifier;

.3 is called the precision specifier.

Exercises

B

1. The following is one possible solution:

/* 05A01.c */

#include

main()

{

char c1, c2, c3;

c1 = ‘B’;

c2 = ‘y’;

c3 = ‘e’;

/* Method I */

printf(“%c%c%c\n”, c1, c2, c3);

/* Method II */

putchar(c1);

putchar(c2);

putchar(c3);

return 0;

}

2. The following is one possible solution:

/* 05A02.c */

#include

33 067231861x AppxB 4.10.2000 11:05 AM Page 448

448

Appendix B

main()

{

int x;

double y;

x = 123;

y = 123.456;

printf(“x: %-3d\n”, x);

printf(“y: %-6.3f\n”, y);

return 0;

}

The output of the program is:

OUTPUT
x: 123

y: 123.456

3. The following is one possible solution:

/* 05A03.c */

#include

main()

{

int num1, num2, num3;

num1 = 15;

num2 = 150;

num3 = 1500;

printf(“The hex format of 15 is: 0x%04X\n”, num1);

printf(“The hex format of 150 is: 0x%04X\n”, num2);

printf(“The hex format of 1500 is: 0x%04X\n”, num3);

return 0;

}

The output of the program is:

OUTPUT
The hex format of 15 is: 0x000F

The hex format of 150 is: 0x0096

The hex format of 1500 is: 0x05DC

4. The following is one possible solution:

/* 05A04.c */

#include

main()

{

int ch;

printf(“Enter a character:\n”);

ch = getchar();

33 067231861x AppxB 4.10.2000 11:05 AM Page 449

Answers to Quiz Questions and Exercises

449

putchar(ch);

return 0;

}

5. You will probably get two error (warning) messages; one stating that getchar() is undefined and another saying that putchar() is undefined. The reason is that the header file, stdio.h, is missing in the code.

Hour 6, “Manipulating Data”

Quiz

1. The = operator is an assignment operator that assigns the value of the operand on the right side of the operator to the one on the left side. On the other hand, == is one of the relational operators; it just tests the values of two operands on both sides and finds out whether they are equal to each other.

2. In the x + - y - - z expression, the first and third minus signs are unary minus operators; the second minus sign is a subtraction operator.

3. 15/4 evaluates to 3. (float)15/4 evaluates to 3.750000.

B

4. No. The y *= x + 5 expression is actually equal to the y = y * (x + 5) expression.

Exercises

1. The following is one possible solution:

/* 06A01.c */

#include

main()

{

int x, y;

x = 1;

y = 3;

x += y;

printf(“The result of x += y is: %d\n”, x);

x = 1;

y = 3;

x += -y;

printf(“The result of x += -y is: %d\n”, x);

x = 1;

y = 3;

33 067231861x AppxB 4.10.2000 11:05 AM Page 450

450

Appendix B

x -= y;

printf(“The result of x -= y is: %d\n”, x);

x = 1;

y = 3;

x -= -y;

printf(“The result of x -= -y is: %d\n”, x);

x = 1;

y = 3;

x *= y;

printf(“The result of x *= y is: %d\n”, x);

x = 1;

y = 3;

x *= -y;

printf(“The result of x *= -y is: %d\n”, x);

return 0;

}

The output of the program is:

OUTPUT
The result of x += y is: 4

The result of x += -y is: -2

The result of x -= y is: -2

The result of x -= -y is: 4

The result of x *= y is: 3

The result of x *= -y is: -3

2. The value of z is 1 (one), after the expression z=x*y==18 expression is evaluated.

3. The following is one possible solution:

/* 06A03.c */

#include

main()

{

int x;

x = 1;

printf(“x++ produces: %d\n”, x++);

printf(“Now x contains: %d\n”, x);

return 0;

}

The output of the program is:

OUTPUT
x++ produces: 1

Now x contains: 2

33 067231861x AppxB 4.10.2000 11:05 AM Page 451

Answers to Quiz Questions and Exercises

451

4. The following is one possible solution:

/* 06A04.c */

#include

main()

{

int x;

x = 1;

printf(“x = x++ produces: %d\n”, x = x++);

printf(“Now x contains: %d\n”, x);

return 0;

}

I get 1 and 1 from the two printf() calls in this program. The reason is that, in the x = x++ expression, the original value of x is copied into a temporary location first, and then x is incremented by 1. Last, the value saved in the temporary location is assigned back to x. That’s why the final value saved in x is still 1.

5. The program incorrectly uses an assignment operator =, instead of an “equal to”

relational operator (==).

B

Hour 7, “Working with Loops”

Quiz

1. No.

2. Yes. The do-while loop prints out the character d, whose numeric value is 100.

3. Yes. Both for loops iterate 8 times.

4. Yes.

Exercises

1. The first for loop contains a statement:

printf(“%d + %d = %d\n”, i, j, i+j);

But the second for loop has a semicolon right after the for statement. This is a null statement—a semicolon by itself, which is a statement that does nothing.

2. The following is one possible solution:

/* 07A02.c */

#include

main()

{

33 067231861x AppxB 4.10.2000 11:05 AM Page 452

452

Appendix B

int i, j;

for (i=0, j=1; i<8; i++, j++)

printf(“%d + %d = %d\n”, i, j, i+j);

printf(“\n”);

for (i=0, j=1; i<8; i++, j++);

printf(“%d + %d = %d\n”, i, j, i+j);

return 0;

}

3. The following is one possible solution:

/* 07A03.c */

#include

main()

{

int c;

printf(“Enter a character:\n(enter K to exit)\n”);

c = ‘ ‘;

while( c != ‘K’ ) {

c = getc(stdin);

putchar(c);

}

printf(“\nOut of the for loop. Bye!\n”);

return 0;

}

4. The following is one possible solution:

/* 07A04.c: Use a for loop */

#include

main()

{

int i;

i = 65;

for (i=65; i<72; i++){

printf(“The numeric value of %c is %d.\n”, i, i);

}

return 0;

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 453

Answers to Quiz Questions and Exercises

453

5. The following is one possible solution:

/* 07A05.c */

#include

main()

{

int i, j;

i = 1;

while (i<=3) { /* outer loop */

printf(“The start of iteration %d of the outer loop.\n”, i);

j = 1;

do{ /* inner loop */

printf(“ Iteration %d of the inner loop.\n”, j);

j++;

} while (j<=4);

i++;

printf(“The end of iteration %d of the outer loop.\n”, i);

}

return 0;

}

B

Hour 8, “Using Conditional Operators”

Quiz

1. The (x=1)&&(y=10) expression returns 1; (x=1)&(y=10) returns 0.

2. In the !y ? x == z : y expression, !y produces 0, thus the value of the third operand y is taken as the value of the expression. That is, the expression evaluates to 1.

3. 1100111111000110 and 0011000000111001.

4. The (x%2==0)||(x%3==0) expression yields 1, and the (x%2==0)&&(x%3==0) expression evaluates to 0.

5. Yes. 8 >> 3 is equivalent to 8/23. 1 << 3 is equivalent to 23.

Exercises

1. ~x yields 0x1000 because ~0xEFFF is equivalent to ~0111111111111111 (in binary), which produces 1000000000000000 (in binary), (that is, 0x1000 in hex format).

Likewise, ~y evaluates to 0xEFFF because ~0x1000 is equivalent to

~1000000000000000 (in binary), which yields 0111111111111111 (in binary) (that is, 0xEFFF in hex format).

33 067231861x AppxB 4.10.2000 11:05 AM Page 454

454

Appendix B

2. The following is one possible solution:

/* 08A02.c */

#include

int main()

{

int x, y;

x = 0xEFFF;

y = 0x1000;

printf(“!x yields: %d (i.e., %u)\n”, !x, !x);

printf(“!y yields: %d (i.e., %u)\n”, !y, !y);

return 0;

}

The output of the program is:

!x yields: 0 (i.e., 0)

!y yields: 0 (i.e., 0)

3. The following is one possible solution:

/* 08A03.c */

#include

int main()

{

int x, y;

x = 123;

y = 4;

printf(“x << y yields: %d\n”, x << y);

printf(“x >> y yields: %d\n”, x >> y);

return 0;

}

[ic:output]The output of the program is:

x << y yields: 1968

x >> y yields: 7

4. The following is one possible solution:

/* 08A04.c */

#include

int main()

{

Other books

Kindred by Octavia Butler
Shipbuilder by Dotterer, Marlene
Imperfect Harmony by Jay Northcote
Snakeroot by Andrea Cremer
Ojos de hielo by Carolina Solé
F O U R by JASON
Farming Fear by Franklin W. Dixon
Never Street by Loren D. Estleman
Broken Bear by Demonico, Gabrielle


readsbookonline.com Copyright 2016 - 2024