CS 2073 Engineering Programming
|
int time = 74747; /8 or some other value in seconds */ int hours = ...; /* fill in for ... */ int minutes = ...; /* fill in for ... */ int seconds = ...; /* fill in for ... */ /* print the answer in the form: 74747 seconds is 20 hours, 45 minutes, 47 seconds. */
| Two versions |
|---|
/* Hours, minutes and seconds */
#include <stdio.h>
int main() {
int time = 74747; /* or some other value in seconds */
int hours = time/3600;
int minutes = (time - hours*3600)/60;
int seconds = (time - hours*3600 - minutes*60);
printf("%i seconds is\n%5i hours,\n%5i minutes,\n%5i seconds\n",
time, hours, minutes, seconds);
}
-------------------------------------------------------------------
/* Hours, minutes and seconds */
#include <stdio.h>
int main() {
int time = 74747; /* or some other value in seconds */
int hours = time/3600;
int minutes = time%3600/60;
int seconds = (time%3600)%60;
printf("%i seconds is\n%5i hours,\n%5i minutes,\n%5i seconds\n",
time, hours, minutes, seconds);
}
|
74747 seconds is 20 hours, 45 minutes, 47 seconds |
| Is size between 2 and 10? |
|---|
/ range.c: is size between 2 and 10 (inclusive)?
#include <stdio.h>
int main() {
while(1) {
int size;
scanf("%i", &size);
if (size == 0) break;
/* Question 2 above */
if ( 2 <= size && size <= 10) printf("%i is in range\n", size);
/* Question 3 above (3 separate answers -- study them all!) */
if (!(2 <= size && size <= 10)) printf("%i out of range (version 1)\n", size);
if (!(2 <= size) || !(size <= 10)) printf("%i out of range (version 2)\n", size);
if ( 2 > size || size > 10) printf("%i out of range (version 3)\n", size);
}
}
|
| A run (input in bold) |
1 1 out of range (version 1) 1 out of range (version 2) 1 out of range (version 3) 2 2 is in range 6 6 is in range 10 10 is in range 11 11 out of range (version 1) 11 out of range (version 2) 11 out of range (version 3) 0 |
Note below that there are many possible ways to write the function quadrant. I gave two versions that are sort of at the extremes.
| Quadrant: first version | Second version plus run |
|---|---|
/* find quadrant of a pair of points */
#include <stdio.h>
int quadrant(double x, double y);
int main() {
double x;
double y;
int quad;
while (1) {
scanf("%lf%lf", &x, &y);
if (x == 0 && y == 0) break;
quad = quadrant(x, y);
/* quad = quadrant2(x, y); */
printf("(%5.2f, %5.2f)", x, y);
if (quad != 0)
printf(" is in quadrant: %i\n", quad);
else
printf(" is on an axis\n");
}
}
int quadrant(double x, double y) {
if (x == 0 || y == 0) return 0;
if (x > 0 && y > 0) return 1;
if (x < 0 && y > 0) return 2;
if (x < 0 && y < 0) return 3;
if (x > 0 && y < 0) return 4;
}
| int quadrant2(double x, double y) {
/* default: if x == 0 or y == 0*/
int result = 0;
if (x > 0) {
if (y > 0) result = 1;
else if (y < 0) result = 4;
}
else if (x < 0) {
if (y > 0) result = 2;
else if (y < 0) result = 3;
}
return result;
}
Results of a run: input in bold:
1 2
( 1.00, 2.00) is in quadrant: 1
-1 3
(-1.00, 3.00) is in quadrant: 2
3 -2
( 3.00, -2.00) is in quadrant: 4
-1 -4
(-1.00, -4.00) is in quadrant: 3
0 5
( 0.00, 5.00) is on an axis
-3 0
(-3.00, 0.00) is on an axis
0 0
|
int i = 1;
int sum = 0;
while (i < 10) {
sum = sum + i*i;
i++;
}
printf("sum: %i", sum);
int k;
for (k = 10; k >= 1; k--) {
printf("%i", k);
if (k != 10) printf(", ");
}
printf("\n");
1 + 1/22 + 1/32 + 1/42 + 1/52 + ...
| Approximate pi | Multiple runs |
|---|---|
// pi_calc.c: series for pi
#include <stdio.h>
#include <math.h>
int main() {
double sum = 0.0;
int n = 1000000;
double pi = atan(1)*4.0;
int i;
for (i = 1; i < n; i++)
sum = sum + 1.0/((double)i*i);
printf("# terms: %18i\n", n);
printf("Final sum: %18.15f\n", sum);
printf("Exact ans: %18.15f\n", pi*pi/6.0);
printf("Pi value: %18.15f\n", pi);
}
| # terms: 100000 Final sum: 1.644924066798243 Exact ans: 1.644934066848226 Pi value: 3.141592653589793 # terms: 1000000 Final sum: 1.644933066847771 Exact ans: 1.644934066848226 Pi value: 3.141592653589793 # terms: 10000000 Final sum: 1.644933966847308 Exact ans: 1.644934066848226 Pi value: 3.141592653589793 |
Two slight modifications to the above formulas give much more accurate approximations to Pi: Calculate Pi.
| Compounding Interest | Run of the program |
|---|---|
/* compound interest yearly & quarterly */
#include <stdio.h>
int main() {
double amount; /* initial amount of money */
double amount_save; /* saved amount */
int years; /* years to apply interest */
double rate; /* interest rate in percent */
int i; /* loop variable */
scanf("%lf%i%lf", &amount, &years, &rate);
amount_save = amount;
/* apply interest each year */
for(i = 1; i <= years; i++) {
amount = amount + amount*rate/100.0;
printf("End of year %2i:", i);
printf(" amount: %10.6f\n", amount);
}
printf("\n");
/* apply 0.25*interest 4 times per year */
amount = amount_save;
for(i = 1; i <= years*4; i++) {
amount = amount + amount*rate/4/100.0;
if (i%4 != 0) { /* all but end of year */
printf(" End of quarter %2i:", i);
printf(" amount: %10.6f\n", amount);
}
else { /* end of year */
printf("End of year %2i:", i/4);
printf(" amount: %10.6f\n", amount);
}
}
printf("\n");
}
| 1000 4 10 End of year 1: amount: 1100.000000 End of year 2: amount: 1210.000000 End of year 3: amount: 1331.000000 End of year 4: amount: 1464.100000 End of quarter 1: amount: 1025.000000 End of quarter 2: amount: 1050.625000 End of quarter 3: amount: 1076.890625 End of year 1: amount: 1103.812891 End of quarter 5: amount: 1131.408213 End of quarter 6: amount: 1159.693418 End of quarter 7: amount: 1188.685754 End of year 2: amount: 1218.402898 End of quarter 9: amount: 1248.862970 End of quarter 10: amount: 1280.084544 End of quarter 11: amount: 1312.086658 End of year 3: amount: 1344.888824 End of quarter 13: amount: 1378.511045 End of quarter 14: amount: 1412.973821 End of quarter 15: amount: 1448.298166 End of year 4: amount: 1484.505621 (Only print at end of year) 1000 10 30 End of year 1: amount: 1300.000000 End of year 2: amount: 1690.000000 End of year 3: amount: 2197.000000 End of year 4: amount: 2856.100000 End of year 5: amount: 3712.930000 End of year 6: amount: 4826.809000 End of year 7: amount: 6274.851700 End of year 8: amount: 8157.307210 End of year 9: amount: 10604.499373 End of year 10: amount: 13785.849185 End of year 1: amount: 1335.469141 End of year 2: amount: 1783.477826 End of year 3: amount: 2381.779599 End of year 4: amount: 3180.793154 End of year 5: amount: 4247.851100 End of year 6: amount: 5672.874058 End of year 7: amount: 7575.948244 End of year 8: amount: 10117.445090 End of year 9: amount: 13511.535700 End of year 10: amount: 18044.238970 |
a0 = 2*sqrt(3); b0 = 3; a1 = 2*a0*b0/(a0 + b0); b1 = sqrt(a1*b0);
Then replace a1 and b1 by a0 and b0, and repeat the last two equations. Keep repeating until a1 and b1 are within 1.0e-15 of one another. The final answer should be the average of a1 and b1.
| Calculate PI | Execution of the program |
|---|---|
#include <stdlib.h>
#include <math.h>
int main() {
double a0 = 2*sqrt(3);
double b0 = 3;
double a1, b1;
int count = 0;
printf(" iter a1 b1\n\n");
while (1) {
a1 = 2*a0*b0/(a0 + b0);
b1 = sqrt(a1*b0);
printf("# %2i: %18.15f ", ++count, a1);
printf("%18.15f\n", b1);
if (fabs(a1 - b1) < 1.0e-14) break;
a0 = a1;
b0 = b1;
}
printf("Final Answer: %18.15f\n",
(a1 + b1)/2);
} | iter a1 b1 # 1: 3.215390309173472 3.105828541230249 # 2: 3.159659942097500 3.132628613281238 # 3: 3.146086215131435 3.139350203046867 # 4: 3.142714599645368 3.141031950890509 # 5: 3.141873049979824 3.141452472285462 # 6: 3.141662747056848 3.141557607911857 # 7: 3.141610176604689 3.141583892148318 # 8: 3.141597034321526 3.141590463228050 # 9: 3.141593748771351 3.141592105999271 # 10: 3.141592927385096 3.141592516692157 # 11: 3.141592722038614 3.141592619365384 # 12: 3.141592670701998 3.141592645033691 # 13: 3.141592657867844 3.141592651450767 # 14: 3.141592654659306 3.141592653055036 # 15: 3.141592653857171 3.141592653456104 # 16: 3.141592653656637 3.141592653556371 # 17: 3.141592653606504 3.141592653581437 # 18: 3.141592653593970 3.141592653587704 # 19: 3.141592653590837 3.141592653589270 # 20: 3.141592653590053 3.141592653589662 # 21: 3.141592653589858 3.141592653589759 # 22: 3.141592653589808 3.141592653589784 # 23: 3.141592653589796 3.141592653589790 Final Answer: 3.141592653589793 |
| Inner product | Rest of code and run |
|---|---|
#include <stdio.h> /* for input/output */
void print_array(char name, int x[], int n);
int inner_product(int x[], int y[], int n);
int main() {
/* initialize vectors using {} initializer */
/* first pair of test vectors */
int a[] = {1, 2, 3};
int b[] = {6, 7, 8};
/* second pair of test vectors */
int c[] = {5, 4, 3, 2, 1};
int d[] = {6, 7, 8, 9, 10};
int inn_prod;
print_array('a', a, 3);
print_array('b', b, 3);
inn_prod = inner_product(a, b, 3);
printf("Inner product is: %i\n\n", inn_prod);
print_array('c', c, 5);
print_array('d', d, 5);
inn_prod = inner_product(c, d, 5);
printf("Inner product is: %i\n\n", inn_prod);
}
| /* calcuate inner product of two vectors */
int inner_product(int x[], int y[], int n) {
int i;
int result = 0;
for (i = 0; i < n; i++) {
result = result + x[i]*y[i];
}
return result;
}
/* print_array: print a 1-dimensional array */
void print_array(char name, int x[], int n) {
int i;
printf("Array %c: ", name);
for (i = 0; i < n; i++) {
printf("%3i", x[i]);
if (i < n - 1) printf(", ");
else printf("\n");
}
}
Results of a run:
Array a: 1, 2, 3
Array b: 6, 7, 8
Inner product is: 44
Array c: 5, 4, 3, 2, 1
Array d: 6, 7, 8, 9, 10
Inner product is: 110
|
char *months[] = {"January",
"February", "March", "April", "May",
"June", "July", "August", "September",
"October", "November", "December"};
In this case, months[1], months[2], ... are each strings that can be printed with a %s format. First use a for loop to print them.
Now create a array of random numbers of the same size as months:
#include <stdlib.h> #include <time.h> double ran[12]; int i; for (i = 0; i < 12; i++) ran[i] = rand()/(double)RAND_MAX;
Finally, sort the array ran into order, carrying the array months along with it. The result is to randomize the order of the months.
| Randomize the order of an array | Execution of the program |
|---|---|
#include <stdlib.h>
#include <time.h>
int main() {
// array of strings, each string an array of char
char *months[] = {"January",
"February", "March", "April", "May",
"June", "July", "August", "September",
"October", "November", "December"};
// array of random doubles
double ran[12];
int i, dum;
srand((long)time(NULL));
// print the strings, using %s for string
printf("Months:\n");
for(i = 0; i < 12; i++)
printf(" %s\n", months[i]);
// load and print the doubles, random order
printf("\nRandom doubles (unsorted):\n");
for (i = 0; i < 12; i++)
ran[i] = rand()/(double)RAND_MAX;
for(i = 0; i < 12; i++)
printf("%20.15f\n", ran[i]);
// sort doubles into decreasing order
// use bubblesort, that interchanges elements
// interchange months array exactly the same way
for (dum = 0; dum < 11; dum++)
for (i = 0; i < 11; i++)
if (ran[i] < ran[i+1]) {
char *tempm;
// interchange ran array elts
double temp = ran[i];
ran[i] = ran[i+1];
ran[i+1] = temp;
// interchange months array elts
tempm = months[i];
months[i] = months[i+1];
months[i+1] = tempm;
}
// print the doubles, now decreasing order
printf("\nRandom doubles (sorted):\n");
for(i = 0; i < 12; i++)
printf("%20.15f\n", ran[i]);
// print the months, now randomized order
printf("\nMonths (randomized)\n");
for(i = 0; i < 12; i++)
printf(" %s\n", months[i]);
printf("\n");
} | Months: January February March April May June July August September October November December Random doubles (unsorted): 0.141427402916098 0.607038361303060 0.389743974613838 0.405499067811993 0.256928145539448 0.091640621932056 0.825441365514622 0.986272125498518 0.176089033566457 0.313722355437336 0.632441553116050 0.371727083051450 Random doubles (sorted): 0.986272125498518 0.825441365514622 0.632441553116050 0.607038361303060 0.405499067811993 0.389743974613838 0.371727083051450 0.313722355437336 0.256928145539448 0.176089033566457 0.141427402916098 0.091640621932056 Months (randomized) August July November February April March December October May September January June |