C: Redoing the Exercises From University for Fun

It’s been a while since I’ve done any C, so I’m redoing the exercises from university for fun. I have this problem where I always need to start at the beginning of a book… so here are the exercises and my answers for chapter 2 (chapter 1 didn’t have any exercises). If I thought the kids were actually still learning C in computing science I might hesitate posting answers.

One man’s constant is another man’s variable.

C Programming: A Modern Approach, Chapter 2 – C Fundamentals

Q1. Create and run Kernighan and Richie’s famous “hello, world” program:

#include <stdio.h>

main() {
  printf("hello, world\n");
}

Do you get a warning message from the compiler? If so, what’s needed to make it go away?

A1. The warnings are as follows.

# gcc -Wall hello-warnings.c -o hello
hello-warnings.c:3: warning: return type defaults to `int'
hello-warnings.c: In function `main':
hello-warnings.c:5: warning: control reaches end of non-void function

The fixed code is as follows.

#include <stdio.h>

int main() {
  printf("hello world!\n");
  return(0);
}

The diff is as follows.

# diff hello.c hello-warnings.c
3c3
< int main() {
---
> main() {
5d4
<   return(0);

Q2. Consider the following program:

(a) Indentify the directives and statements in this program.
(b) What output does the program produce?

#include <stdio.h>

main() {
  printf("Parkinson's Law:\nWork expands so as to "); printf("fill the time\n"); printf("available for its completion.\n");
  return(0);
}

A2a. Directives begin with a hash. The only directive is the standard I/O library inlcude. Statements end with a semicolon. There are three printf statements and one return statement.

A2b. The program produces the following output.

Parkinson’s Law:
Work expands so as to fill the time
available for its completion.

Q3. Write a program that uses printf to display the following picture on the screen:

-------*
      *
     *
*   *
 * *
  *

A3. The program is as follows.

#include <stdio.h>

int main() {
  printf("-------*\n"); printf("      *\n"); printf("     *\n"); printf("*   *\n"); printf(" * *\n"); printf("  *\n");
  return(0);
}

Q4. Condense the following program by (1) replacing the assignments to height, length, and width with initializers and (2) removing the weight variable, instead calculating (volume + 165) / 166 within the last printf.

#include <stdio.h>

int main() {
  int height, length, width, volume, weight;

  height = 8;
  length = 12;
  width = 10;
  volume = height * length * width;
  weight = (volume + 165) / 166;

  printf("Dimensions: %dx%dx%d\n", length, width, height); printf("Volume (cubic inches): %d\n", volume); printf("Dimensional weight (pounds): %d\n", weight);

  return(0);
}

A4. The condensed program is as follows.

#include <stdio.h>

int main() {
  int height = 8;
  int length = 12;
  int width = 10;
  int volume = height * length * width;

  printf("Dimensions: %dx%dx%d\n", length, width, height); printf("Volume (cubic inches): %d\n", volume); printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166);

  return(0);
}

Q5. Write a program that computes the volume of a sphere with a 10-meter radius. Write the fraction 4/3 as 4.0/3.0. Try writing it as 4/3. What happens?

A5. The program is as follows.

#include <stdio.h>

int main() {
  int radius = 10;
  float pi = 3.14;
  float volume = (4.0 / 3.0) * pi * radius * radius * radius;
  printf("Volume: %f\n", volume);
  return(0);
}

Using 4.0 / 3.0 = 1.3333333333333333

# gcc -Wall q5.c -o q5
# ./q5
Volume: 4186.666992

Using 4 / 3 = 1

# gcc -Wall q5.c -o q5
# ./q5
Volume: 3140.000000

Q6. Write a program that declares several int and float variables – without initalizing them – and then print the values. Is there a pattern to the values? (Usually there isn’t.)

A6. The program is as follows.

#include <stdio.h>

int main() {
  int i;
  int j;
  int k;
  float a;
  float b;
  float c;
  printf("%d-%d-%d-%f-%f-%f\n", i, j, k, a, b, c);
  return(0);
}
# ./q6
-809729980-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809723880-469763664-1-0.000000-155976597630386221961056109729771683840.000000-0
.000000
# ./q6
-809701340-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809706496-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809739544-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809570472-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809609160-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809660196-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809641356-469763664-1-0.000000--0.000000-0.000000
# ./q6
-809521244-469763664-1-0.000000-30489167153316507834731074445180403712.000000-0.
000000

Q7. Modify the program in exercise 5 so that it prompts the user to enter the radius of the sphere.

A7. The program is as follows.

#include <stdio.h>

int main() {
  int radius = 0;
  float volume = 0;
  float pi = 3.14;

  printf("Enter radius: ");
  scanf("%d", &radius);

  volume = (4.0 / 3.0) * pi * radius * radius * radius;
  printf("Volume: %f\n", volume);

  return(0);
}
# gcc -Wall q7.c -o q7
# ./q7
Enter radius: 10
Volume: 4186.666992

Q8. Write a program that asks the user to enter a dollar amount, then display the amount with 5% tax added:

Enter a dollar amount: 100.00
With tax added: 105.00

A8. The program is as follows.

#include <stdio.h>

int main() {
  float dollars = 0;
  printf("Enter a dollar amount: ");
  scanf("%f", &dollars);
  printf("With tax added: %.2f\n", dollars * 1.05);
  return(0);
}

Q9. Modify Exercise 7 by making PI a macro that represents the value of pi.

A9. The program is as follows.

#include <stdio.h>

#define PI 3.14

int main() {
  int radius = 0;
  float volume = 0;

  printf("Enter radius: ");
  scanf("%d", &radius);

  volume = (4.0 / 3.0) * PI * radius * radius * radius;
  printf("Volume: %f\n", volume);

  return(0);
}

The diff is as follows.

# diff q7.c q9.c
2a3,4
> #define PI 3.14
>
6d7
<   float pi = 3.14;
11c12
<   volume = (4.0 / 3.0) * pi * radius * radius * radius;
---
>   volume = (4.0 / 3.0) * PI * radius * radius * radius;

Q10. Which of the following are not legal C identifiers?

a) 100_bottles
b) _100_bottles
c) one_hundred_bottles
d) bottles_by_the_hundred_

A10. Identifiers may only start with a letter or underscore, and may only contain letters, numbers, and underscores. All but ‘100_bottles’ are valid.

#include <stdio.h>

int main() {
  int 100_bottles;
  int _100_bottles;
  int one_hundred_bottles;
  int bottles_by_the_hundred_;
  return(0);
}
# gcc -Wall q10.c -o q10
q10.c:4:7: invalid suffix "_bottles" on integer constant
q10.c: In function `main':
q10.c:4: error: syntax error before numeric constant
q10.c:5: warning: unused variable `_100_bottles'
q10.c:6: warning: unused variable `one_hundred_bottles'
q10.c:7: warning: unused variable `bottles_by_the_hundred_'

Q11. Which of the following are keywords in C?

a) for
b) If
c) main
d) printf
e) while

A11. Only ‘for’ and ‘while’ are keywords in C.

#include <stdio.h>

int main() {
  int for;
  int If;
  int main;
  int printf;
  int while;
  return(0);
}
# gcc -Wall q11.c -o q11
q11.c: In function `main':
q11.c:4: error: syntax error before "for"
q11.c:6: warning: `main' is usually a function
q11.c:8: error: syntax error before "while"
q11.c:5: warning: unused variable `If'
q11.c:6: warning: unused variable `main'
q11.c:7: warning: unused variable `printf'

Q12. How many tokens are there in the following statement?

a=(3*q-p*p)/3;

A12. There are 14 tokens in the above statement.

Q13. Insert spaces between the tokens in Excerise 12 to make this statement easier to read.

A13. The statement with spaces is as follows.

a = ( 3 * q - p * p ) / 3;

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply