CS 2073 Engineering Programming
|
For answers, see answers. Please attempt each question before checking the answer.
3 + 4*5 3.2 + 4 (int)(4.7 - 3) 3/4 15%4
int n = 3; /* or int n = 2; */ double x = exp(n); printf( /* answer here */ ); Output: e to the power 3: 20.085536923187 Output if "int n = 3;" is changed to "int n = 2;" e to the power 2: 7.389056098930
#include <stdio.h>
int main () {
double x = 0.1;
while (x != 1.0) {
printf("x = %20.15f\n", x);
x = x + 0.1;
}
}
Explain why this is an infinite loop.
Add code that uses a counter to stop the loop after 25 iterations. (Do this without looking up the answer.)
Make use of this function in the earlier example involving the same type of data.
10 * ** * * * * * * * * * * * * * * ********** | 5 * ** * * * * ***** | 13 * ** * * * * * * * * * * * * * * * * * * * * ************* | 3 * ** *** | 2 * ** | 8 * ** * * * * * * * * * * ******** |
You may use the following function to help print the characters:
void chars(int n, char ch) {
int i;
for(i = 0; i < n; i++)
printf("%c", ch);
}
Here, the format code %c is used to
print a single character, so that
chars(5, '*') will print 5 stars with no newline,
and chars(4, ' ') will print 4 blanks with no newline.
8
*
***
*****
*******
*********
***********
*************
***************
***
***
***
| 6
*
***
*****
*******
*********
***********
***
***
***
| 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
***
***
***
| 3 * *** ***** *** *** *** | 12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
***
***
***
|