Solution to Self Check
Concepts:
1. What are constants?
constants are names for values that never change in your program.
2. Why do we want to use constants?
Constants minimize updates to one location. For example, if we were to have a program that would calculate taxes and we used a constant, we would be able to change that constant in one place if the tax rates were to change. If we used the numeric rate in a large program, fixing these rates can be error prone. Constants provide a clear documentation of the value we are using in our program
3. Declare a constant named TAU using preprocessor statements to 5 decimal places. This value is 6.28318
#define TAU 6.28318
4. What is the difference between prefix increment and postfix increment
prefix and postfix increment with both increase the operand by 1. prefix evaluates to the updated value of the operand. postfix increment evaluates to the original value of the operand.
Evaluate:
Q1: 5 + 2 * 6 - 17 / 3 % 4
Q2: 15 + 29 / 4 / 2.0 - 3 * 2
Q3: a++
int a = 5;
Q4: x = b += 19 / 3 % 4 * 3 / 5
int b = 10;
int x;
Q5: y = c += 7 - d++ * 6 / 4 % 3
int c = 11;
int d = 3;
int y;
Walkthroughs
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;
}
Solution:
whole thing: -4
x = -4, y = -4
x++ = 2
++y = 2
x = 3
y = 3
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;
}
Solution:
whole thing: 2
x = 2, y = 2.666667
Programming:
Part A:
#include <stdio.h>
int main(void)
{
int soyAmount;
printf("Enter the number of ml of soy sauce are you using: ");
scanf("%d",&soyAmount);
printf("\nYour recipe will use:\n\n");
printf("%d ml of soy sauce\n", soyAmount);
printf("%d ml of vinegar\n", soyAmount*2);
printf("%d ml of sugar\n", soyAmount*3);
printf("%d ml of water\n", soyAmount*4);
return 0;
}
Note, you can also have 3 variables for amount of vinegar, sugar and water then calculate the amount of each and just print the variables. Both solutions are valid
Part B:
if all ingredients contribute fully to the volume of the sauce, then the proportioning is 1 to 2 to 3 to 4.
Summing this up, there are essentially 10 parts (1 + 2 + 3 + 4). Thus, if we took the total and divided by 10, we would get how much each portion is.
#include <stdio.h>
int main(void)
{
int total;
double part;
printf("Please enter how much sauce you want to make in ml: ");
scanf("%d",&total);
part = total/10.0; //note that 10.0 and 10 are not the same. total/10 is an int
//it would be a truncated result.
printf("\nYour recipe will use:\n\n");
printf("%.0lf ml of soy sauce\n", part);
printf("%.0lf ml of vinegar\n", part*2);
printf("%.0lf ml of sugar\n", part*3);
printf("%.0lf ml of water\n", part*4);
return 0;
}
Debugging
Here is the original code:
#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
}
Here are the syntax errors:
- } instead of { on the
int main(void)}
line - integer should be int in the
integer x = 0;
line - missing ; on the
return 0
line
Logical errors:
x is initially 0, the first line of output outputs x++. since x++ results in the original value of x, the first output would be 0. This program prints:
0
1
2
3
4
To fix this, we can do one of two things. Either we initialize x with 1 (int x = 1) or we use prefix increment instead printf("%d\n", ++x);
on each line
Thus, the solution is either:
#include <stdio.h>
int main(void)
{
int x=1;
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
printf("%d\n",x++);
return 0;
}
or:
#include <stdio.h>
int main(void)
{
int x=0;
printf("%d\n",++x);
printf("%d\n",++x);
printf("%d\n",++x);
printf("%d\n",++x);
printf("%d\n",++x);
return 0;
}