C operator problems

In the problems that ask for evaluation of an expression, if results are floating point, write out result to 2 decimal places

Problem 1:

int x=1;
int y=1;

x = y += 3 / 4 + 5 * 2 % 3 - 6;
  1. what does the above expression evaluate to?
  2. what is the final value of x?
  3. what is the final value of y?

Problem 2:

int x=1;
float y=1;

x = y = (6 * 2.0 - 2 % 3 * 6) / 3;
  1. what does the above expression evaluate to?
  2. what is the final value of x?
  3. what is the final value of y?

Problem 3:

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 to the nearest ml and show that to the user.

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

Problem 4:

The following is a program that should print out the numbers 1 to 5 one per line:

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 is the syntax error generated when you compile the program as is?
  2. Explain what the cause of the syntax error(s) and how to fix it.
  3. What logical error exist in above program?
  4. How do you fix it?

results matching ""

    No results matching ""