Points for each problem: 1-15, 2-15, 3-20, 4-20, 5-20, 6-20, 7-20, 8-20.
#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
/* 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 (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);
}
}
Here are three runs:
1
1 out of range (version 1)
1 out of range (version 2)
1 out of range (version 3)
2
6
10
11
11 out of range (version 1)
11 out of range (version 2)
11 out of range (version 3)
int k;
for (k = 5; k >= 1; k--) {
printf("%i", k);
if (k != 1) printf(", ");
}
printf("\n");
#include <stdio.h>
int main() {
int k;
for (k = 5; k >= 1; k--) {
printf("%i", k);
if (k != 1) printf(", ");
}
printf("\n");
k = 5;
while (k >= 1) {
printf("%i", k);
if (k != 1) printf(", ");
k--;
}
printf("\n");
}
Output of a run
5, 4, 3, 2, 1
5, 4, 3, 2, 1
1 - 1/22 + 1/32 - 1/42 + 1/52 + ... - 1/n2
Notice that the signs are alternating. Start numbering the terms with 1 and assume that n is even, so that the sign of the last (nth) term is negative. Your program segment must use a loop to calculate the sum. It must print the sum at the end.
#include <stdio.h>
#define PI 3.141592653589793
int main() {
int n = 1000000;
int i;
double sum = 0, sign = 1;
for (i = 1; i <= n; i++) {
sum = sum + sign*(1.0/i/i);
sign = -sign;
}
printf("Sum: %20.16f\n", sum);
printf("pi*pi/12: %20.16f\n", PI*PI/12.0);
}
Output of a run:
Sum: 0.8224670334235856
pi*pi/12: 0.8224670334241132
#include <stdio.h>
/* prototype here */
int circle(double a, double b);
int main() {
double x, y;
int unitcircle;
while (1) {
scanf("%lf%lf", &x, &y);
if (x == 0 && y == 0) break;
unitcircle = circle(x, y);
printf("(%5.2f, %5.2f)", x, y);
if (unitcircle == 0)
printf(" is outside the unit circle\n");
else
printf(" is inside the unit circle\n");
}
}
/* definition of the function below */
int circle(double a, double b) {
if (a*a + b*b < 1) return 1;
else return 0;
}
Here is a run
2 2
( 2.00, 2.00) is outside the unit circle
1 1
( 1.00, 1.00) is outside the unit circle
0.5 0.5
( 0.50, 0.50) is inside the unit circle
0.7 0.7
( 0.70, 0.70) is inside the unit circle
0.71 0.71
( 0.71, 0.71) is outside the unit circle
0 0
#include <stdio.h>
int main() {
int a[] = {2, 6, 4, 7, 3, 9};
int b[] = {5, 1, 3, 2, 4, 0};
int c[6];
/* give code that will let c be the coordinate-wise sum of a and b */
int i;
for (i = 0; i < 6; i++)
c[i] = a[i] + b[i];
/* give code to print the elements of c on one line, spaces between them */
for (i = 0; i < 6; i++)
printf("%i ", c[i]);
printf("\n");
}
Here is a run:
7 7 7 9 7 9
#include <stdio.h>
/* prototype for function maximum here */
int maximum(int a[], int n);
int main() {
int n = 4;
int a[] = {90, 98, 80, 87};
/* complete the call below */
double max = maximum(a, n);
printf("Maximum: %8.2f\n", max);
}
/* definition of maximum function here */
int maximum(int a[], int n) {
int i;
int maxval = a[0];
for (i = 1; i < n; i++)
if (maxval < a[i]) maxval = a[i];
return maxval;
}
Result of a run:
Maximum: 98.00
/* 1 */ #include <stdio.h>
/* 2 */ int main() {
/* 3 */ FILE *textfile;
/* 4 */ int score;
/* 5 */ int s[10];
/* 6 */ int n = 0;
/* 7 */ int i;
/* 8 */ textfile = fopen("source.txt", "r");
/* 9 */ if (textfile == NULL) {
/* 10 */ printf("Can't open scores.txt\n");
/* 11 */ exit(1);
/* 12 */ }
/* 13 */ for (;;) {
/* 14 */ fscanf(textfile, "%i", &score);
/* 15 */ if (score == -1) break;
/* 16 */ s[n] = score;
/* 17 */ n++;
/* 18 */ }
/* 19 */ close(textfile);
/* 20 */
/* 21 */ s[n] = 99;
/* 22 */ n++;
/* 23 */
/* 24 */ textfile = fopen("source.txt", "w");
/* 25 */ for (i = 0; i < n; i++) {
/* 26 */ fprintf(textfile, "%i ", s[i]);
/* 27 */ }
/* 28 */ fprintf(textfile, "-1\n");
/* 29 */ }
90 87 56 78 -1
what will it look like after the program has run?
90 87 56 78 99 -1
90 87 56 78 99 99 -1