首页 > > 详细

代做Variables, Operators and Expressions

1.3 - Variables, Operators and Expressions
You are required to do and submit all homework assignments in each level.
Note: the following exercises are for for their respective video lecture denoted by 1.x (1.3
corresponds to video lecture 1.3)
If you have not done so yet, please read these two threads, as HW
submissions and questions will only be responded to with strict adherence
to these policies:
Getting started on the C++ course
Instruction on homework submission
All exercises in this Level must be coded exclusively in C syntax (no
, cout, cin, classes, etc.)
Exercise 1
printf() is a standard function. Each compiler will support this function. Between the () is
stated what has to be printed (on screen). Create a C-program that prints the following when
executed:
My first C-program
is a fact!
Good, isn’t it?
Exercise 2
Write a program that calculates the surface of a triangle with one 90 degree angle. The
formula is half the height multiplied by the base. The program should take an input from
the user (base & height), and output the result.
Exercise 3
In the following program various operators are used to assign a value to the variable x. In
this example the string that is passed to printf() has the format specification %d. This
means that a decimal will be printed in place of %d. This decimal is passed to printf() as
the second argument. The first argument of printf() must be a string. In this example the
second argument is the variable x.
Predict what will be printed on screen (provide a code file with comments stating the
output for each line).
/* Operators */
#include
int main()
{
int x;
x=-3+4*5-6;
printf("x=%d\n", x);
x=3+4%5-6;
printf("x=%d\n", x);
x=-3*4%-6/5;
printf("x=%d\n", x);
x=(7+6)%5/2;
printf("x=%d\n", x);
return 0;
}
Exercise 4
Create a C-program that uses the fact that 0 (zero) is interpreted as FALSE and non-zero
is interpreted as TRUE. The C-program can be made easier to read when this 0 (or nonzero) is assigned to a variable e.g. an int called married. Use the ?: operator to print if
someone is married or not. (See if you can use a single printf)
See forum discussion on this exercise
Exercise 5
Create a C-program that clearly shows the difference between --i and i--.
Exercise 6
Write a C-program that shifts any number two places to the right. Input should be an
integer. Output should be the shifted result, as well as output an indication of
whether a logical or arithmetic shift is performed (if a 1 or 0 is shifted in at the
left side) for the inputted number. For more info and example, see
http://en.wikipedia.org/wiki/Logical_shift
Exercise 7
Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The
number to multiply and n are variables, which get a value at the start of the program.
Clue:
1 shift to the left is the same as multiplying by 2.
2 shifts to the left are the same as multiplying by 4.
3 shifts to the left are the same as multiplying by 8.
Exercise 8
The following program uses assignment-operators. Predict what will be printed on
screen (provide a code file with comments stating the output for each line). The
operators + and = = have a higher priority than the assignment-operators.
/* Assignment operators */
#include
int main()
{
int x=2;
int y;
int z;
x*=3+2;
printf("x=%d\n", x);
x*=y=z=4;
printf("x=%d\n", x);
x=y==z;
printf("x=%d\n", x);
return 0;
}
Exercise 9
Predict what the following program prints on screen (provide a code file with comments
stating the output for each line).
/* Conditional expressions */
#include
int main()
{
int x=1;
int y=1;
int z=1;
x+=y+=x;
printf("%d\n\n", (x

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!