Skip to main content

Excercise Solutions

Concepts:

1. 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

5+2617/3%45 + 2 * 6 - 17 / 3 \% 4\\ 5+1217/3%45 + 12 - 17 / 3 \% 4\\ 5+125%45 + 12 - 5 \% 4\\ 5+1215 + 12 - 1\\ 17117 - 1\\ 1616

Q2: 15 + 29 / 4 / 2.0 - 3 * 2

15+29/4/2.03215 + 29 / 4 / 2.0 - 3 * 2\\ 15+7/2.03215 + 7 / 2.0 - 3 * 2\\ 15+3.503215 + 3.50 - 3 * 2\\ 15+3.50615 + 3.50 - 6\\ 18.50618.50 - 6\\ 12.5012.50\\

Q3: a++

int a = 5;

a++a++\\ 55\\ ais6a is 6\\

Q4: x = b += 19 / 3 % 4 * 3 / 5

int b = 10;
int x;

x=b+=19/3%43/5x = b += 19 / 3 \% 4 * 3 / 5\\ x=b+=6%43/5x = b += 6 \% 4 * 3 / 5\\ x=b+=23/5x = b += 2 * 3 / 5\\ x=b+=6/5x = b += 6 / 5\\ x=b+=1x = b += 1\\ x=11x = 11\\ 1111\\ xis11,bis11x is 11, b is 11

Q5: y = c += 7 - d++ * 6 / 4 % 3

int c = 11;
int d = 3;
int y;

y=c+=7d++6/4%3y = c += 7 - d++ * 6 / 4 \% 3\\ y=c+=736/4%3y = c += 7 - 3 * 6 / 4 \% 3\\ y=c+=718/4%3y = c += 7 - 18 / 4 \% 3\\ y=c+=74%3y = c += 7 - 4 \% 3\\ y=c+=71y = c += 7 - 1\\ y=c+=6y = c += 6\\ y=17y = 17\\ 1717\\ yis17,cis17,dis4y is 17, c is 17, d is 4\\

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:

  1. } instead of { on the int main(void)} line
  2. integer should be int in the integer x = 0; line
  3. 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;
}