CS 2073 Engineering Programming
|
#include <stdio.h>
#define LIM 40
void squares(int a[LIM]);
void printarray(int a[LIM]);
int addup(int a[LIM]);
int main() {
int a[LIM];
int sum;
squares(a);
printarray(a);
sum = addup(a);
printf("Sum of squares through %i = %i\n", LIM-1, sum);
printf("Formula: LIM*(LIM-1)*(2*LIM-1)/6 = %i\n",
LIM*(LIM-1)*(2*LIM-1)/6);
}
void squares(int a[LIM]) {
int i;
for (i = 0; i < LIM; i++)
a[i] = i*i;
}
void printarray(int a[LIM]) {
int i;
for (i = 0; i < LIM; i++) {
printf("%4i ", a[i]);
if (i%10 == 9) printf("\n");
}
printf("\n");
}
int addup(int a[LIM]) {
int i;
int sum = 0;
for (i = 0; i < LIM; i++)
sum = sum + a[i];
return sum;
}
Here is the output of a run:
0 1 4 9 16 25 36 49 64 81
100 121 144 169 196 225 256 289 324 361
400 441 484 529 576 625 676 729 784 841
900 961 1024 1089 1156 1225 1296 1369 1444 1521
Sum of squares through 39 = 20540
Formula: LIM*(LIM-1)*(2*LIM-1)/6 = 20540
#include <stdio.h>
#define LIM 17
void initarray(int m[LIM][LIM]);
void printarray(int m[LIM][LIM]);
int main() {
int m[LIM][LIM];
initarray(m);
printarray(m);
}
void initarray(int m[LIM][LIM]) {
int i, j;
for (i = 0; i < LIM; i++)
for (j = 0; j < LIM; j++)
m[i][j] = i*j;
}
void printarray(int m[LIM][LIM]) {
int i, j;
printf(" |");
for (j = 1; j < LIM; j++) printf("%3i ", j);
printf("\n---+");
for (j = 1; j < LIM; j++) printf("----", j);
printf("\n");
for (i = 1; i < LIM; i++) {
printf("%2i |", i);
for (j = 1; j < LIM; j++)
printf("%3i ", m[i][j]);
printf("\n");
}
}
Here is the output of a run:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
---+----------------------------------------------------------------
1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32
3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64
5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80
6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112
8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128
9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144
10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160
11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176
12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192
13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 208
14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 224
15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240
16 | 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256
int a[] = {12, 34, 21, 47, 52, 7, 41};
int asize = 7;
Pass this array and its size to a function maxmin that will calculate both the maximum and the minimum value in the array. The function should have two formal parameters max and min, each of type int *, that is, a pointer to an integer, or the address of an integer. Use these parameters to get the maximum and minimum values back to the function main, where they should be printed. (You must pass the addresses of two ordinary integers as the actual parameters.)
#include <stdio.h>
void maxmin(int a[], int asize, int *max, int *min);
int main() {
int a[] = {12, 34, 21, 47, 52, 7, 41};
int asize = 7;
int max, min;
maxmin(a, asize, &max, &min);
printf("max: %i, min: %i\n", max, min);
}
void maxmin(int a[], int asize, int *max, int *min) {
int i;
*max = a[0];
*min = a[0];
if (asize == 1) return;
for (i = 1; i < asize; i++) {
if (a[i] > *max) *max = a[i];
if (a[i] < *min) *min = a[i];
}
}
Here is the output of a run:
max: 52, min: 7
#include <stdio.h>
#define STARS(i) for(j=0;j<i;j++)printf("*") /* preprocessor macro */
int main() {
int j;
STARS(10);
printf("\n");
STARS(21);
printf("\n");
STARS(1);
printf("\n");
STARS(0);
printf("\n");
}
Here is the result after preprocessing
(normally you pay no attention to this):
int main() {
int j;
for ( j = 0 ; j < 10 ; j ++ ) printf ( "*" );
printf("\n");
for ( j = 0 ; j < 21 ; j ++ ) printf ( "*" );
printf("\n");
for ( j = 0 ; j < 1 ; j ++ ) printf ( "*" );
printf("\n");
for ( j = 0 ; j < 0 ; j ++ ) printf ( "*" );
printf("\n");
}
Here is the output of a run:
**********
*********************
*