CS 1073 Engineering Programming
|
#include <stdio.h>
int main () {
if ( -342.81 == -3.4281e2)
printf("-3.4281e2 is the answer\n");
}
Run of the program:
-3.4281e2 is the answer
3 + 4*5 23 3.2 + 4 7.2 (int)(4.7 - 3) (int)(1.7) == 1 3/4 0 15%4 remainder when 15 is divided by 4 == 3
#include <stdio.h>
int main () {
double a, b, c;
double d;
scanf("%lf %lf %lf", &a, &b, &c);
d = b*b - 4*a*c;
if (d > 0)
printf("real, not equal\n");
else if (d == 0)
printf("real, equal\n");
else
printf("complex\n");
}
Three Runs:
1 2 1
real, equal
1 2 -1
real, not equal
1 2 2
complex
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>
#include <math.h>
int main () {
int n = 2;
double x = exp(n);
printf("e to the power %i: %0.15f\n", n, x);
}
#include <stdio.h>
int main () {
double x, y;
double minxy;
scanf("%lf %lf", &x, &y);
if (x < y) minxy = x;
else minxy = y;
printf("min of %0.5f and %0.5f = %0.5f\n",
x, y, minxy);
}
Runs of the program:
2 4
min of 2.00000 and 4.00000 = 2.00000
4 2
min of 4.00000 and 2.00000 = 2.00000
4 4
min of 4.00000 and 4.00000 = 4.00000
#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. See Lecture 4 for an answer.
Add code that uses a counter to stop the loop after 25 iterations. (Do this without looking up the answer.) See Lecture 4 for an answer.
Make use of this function in the earlier example involving the same type of data.
#include <stdio.h>
double disc(double a, double b, double c);
int main () {
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if (disc(a, b, c) > 0)
printf("real, not equal\n");
else if (disc(a, b, c) == 0)
printf("real, equal\n");
else
printf("complex\n");
printf("disc(1.0, -4.0, 4.0) = %0.5f", disc(1.0, -4.0, 4.0));
}
double disc(double a, double b, double c) {
return b*b - 4*a*c;
}
Same output as before, followed by:
disc(1.0, -4.0, 4.0) = 0.00000
10 * ** * * * * * * * * * * * * * * ********** | 5 * ** * * * * ***** | 13 * ** * * * * * * * * * * * * * * * * * * * * ************* | 3 * ** *** | 2 * ** | 8 * ** * * * * * * * * * * ******** |
// triangle.c: print a triangle
#include <stdio.h>
void chars(int n, char ch);
int main () {
int n;
int line;
scanf("%i", &n);
chars(1, '*'); chars(1, '\n');
for (line = 2; line < n; line ++) {
chars(1, '*'); chars(line-2, ' ');
chars(1, '*'); chars(1, '\n');
}
chars(n, '*'); chars(1, '\n');
}
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
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
***
***
***
|
// tree.c: print a tree
#include <stdio.h>
void chars(int n, char ch);
int main () {
int n;
int line;
scanf("%i", &n);
for (line = 1; line <= n; line ++) {
chars(n - line, ' ');
chars(2*line - 1, '*');
chars(1, '\n');
}
for (line = 1; line <= 3; line++) {
chars(n - 2, ' ');
chars(3, '*');
chars(1, '\n');
}
}
void chars(int n, char ch) {
int i;
for(i = 0; i < n; i++)
printf("%c", ch);
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll();
int main() {
int d1, d2, d3;
int count = 0;
srand((long)time(NULL));
while (1) {
d1 = roll(); d2 = roll(); d3 = roll();
count++;
if (d1 ==1 && d2 == 1 && d3 == 1)
break;
if (count > 5000) break;
}
printf("Count: %i\n", count);
}
int roll() {
return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
}
/* successive runs:
125 258 15 303 177 4 26 52 262 254 452 117
38 676 98 1532 172 233 207 145 93 243 269 226
155 516 338 338 66 182 145 208 307 617 30 125
*/
Here are three versions of the basic loop in this example:
while (1) {
d1 = roll(); d2 = roll(); d3 = roll();
count++;
if (d1 ==1 && d2 == 1 && d3 == 1)
break;
}
===============================================
d1 = roll(); d2 = roll(); d3 = roll();
while (!(d1 ==1 && d2 == 1 && d3 == 1)) {
count++;
d1 = roll(); d2 = roll(); d3 = roll();
}
===============================================
d1 = roll(); d2 = roll(); d3 = roll();
while (d1 !=1 || d2 != 1 || d3 != 1) {
count++;
d1 = roll(); d2 = roll(); d3 = roll();
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll();
int main() {
int d1, d2, d3;
int count = 0;
srand((long)time(NULL));
while (1) {
d1 = roll(); d2 = roll(); d3 = roll();
count++;
if (d2 == d1 && d3 == d1)
break;
if (count > 5000) break;
}
printf("Count: %i\n", count);
}
int roll() {
return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
}
/* successive runs:
85 52 77 58 228 89 2 17 20 60 15 28 27 7 53 82
*/