Skip to main content

Self Check

Concepts:

  1. What are constants?
  2. Why do we want to use constants?
  3. Declare a constant named TAU using preprocessor statements to 5 decimal places. This value is 6.28318
  4. What is the difference between prefix increment and postfix increment

Evaluate:

Evaluate each of the following expressions. If the end result is a floating point value, express the result to 2 decimal places. If result is integer, do not have any decimal places. If the expression involves one or more variables, state the result of the expression as well as the value of stored in each variable after the expression.

Assume that the following variables have been declared and initialized when used in the expressions below:

int a = 5;
int b = 10;
int c = 11;
int d = 3;
int x;
int y;
  1. 5 + 2 * 6 - 17 / 3 % 4
  2. 15 + 29 / 4 / 2.0 - 3 * 2
  3. a++
  4. x = b += 19 / 3 % 4 * 3 / 5
  5. y = c += 7 - d++ * 6 / 4 % 3

Walkthroughs:

make sure you check out how to do walkthroughs before you start

Walkthrough 1:

What is the output of the following program?

#include <stdio.h>
int main(void){
int x=1;
int y=1;
printf("whole thing: %d\n", x = y += 3 / 4 + 5 * 2 % 3 - 6);
printf("x = %d, y = %d\n", x , y);
x = 2;
y = 2;
printf("x++ = %d\n", x++);
printf("++y = %d\n", y++);
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}

Walkthrough 2:

What is the output of the following program?

#include <stdio.h>
int main(void){
int x=1;
float y=1;
printf("whole thing: %d\n", x = y = (9 * 2.0 - 2 % 3 * 5) / 3);
printf("x = %d, y = %f\n", x , y);
return 0;
}

Programming:

A recipe for sweet and sour ribs makes the sauce out of 4 ingredients in different proportions. The recipe uses: 1 part soy sauce, 2 parts vinegar, 3 parts sugar and 4 parts water. Thus, if someone uses 100ml of soy sauce, they would need 200 ml of vinegar, 300ml of sugar and 400ml of water to make up the recipe.

Part A:

Write a program that will ask the user for the amount of soy sauce they are using for the recipe and print out the amount of each of the other 3 ingredients.

Sample Run:

Enter the number of ml of soy sauce are you using: 100

Your recipe will use:

100 ml of soy sauce
200 ml of vinegar
300 ml of sugar
400 ml of water

Part B:

Write a program that will ask the user for the total amount of sauce they want. The program will then figure out how much of each ingredient is needed. Output the result to the nearest ml. For simplicity you may assume that all ingredients contribute their full amount to the total sauce volume (this is not true in practice)

Sample Run:

Please enter how much sauce you want to make in ml: 200

Your recipe will use:
20 ml of soy sauce
40 ml of vinegar
60 ml of sugar
80 ml of water

Debugging:

The following is a program that should print out the numbers 1 to 5 one per line. If it was working, this would be the output:

1
2
3
4
5

However, there are bugs in the program (both syntactic and logical).

#include <stdio.h>
int main(void)}
integer x=0;
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
return 0
}
  1. What are the syntax error generated when you compile the program as is?
  2. What is the cause of the syntax error(s) and how would you fix it?
  3. What logical error exist in above program?
  4. How do you fix it?