-
Notifications
You must be signed in to change notification settings - Fork 0
/
2d-arr-table-gen-and-assert.c
93 lines (80 loc) · 2.03 KB
/
2d-arr-table-gen-and-assert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*********************************************
* Write a program that enables the user to input a and b where a > b
* Generate a table dimensions rXk, r = 4, k = 10
* Elements are pseudo-random numbers, (0 <= d <= 5).
* Check if the user input is correct
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#define r 4
#define k 10
#define max_d 5
void mySwap(double *x, double *y) {
double c = *x;
*x = *y;
*y = c;
}
void inputDouble(double *x, char c) {
printf("%c = ", c); scanf("%lf", x);
}
void inputAB(double *a, double *b) {
inputDouble(a, 'a');
inputDouble(b, 'b');
if (a > b) {
mySwap(a, b);
}
}
void inputInt(int *x, char c) {
printf("%c = ", c); scanf("%d", x);
}
void inputD(int *d) {
inputInt(d, 'd');
if (*d < 0) { *d = 0; }
if (*d > max_d) { *d = max_d; }
}
double generateElement(double a, double b, int d) {
double s = pow(10, d);
return round(a + (b - a) * ((double)rand() / RAND_MAX) * s) / s;
}
void showTable(double table[][k]) {
int i = 0, j = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < k; j++) {
printf("%.6lf", table[i][j]);
}
printf("\n");
}
}
void generateTable(double table[][k], double a, double b, int d) {
int i = 0, j = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
table[i][j] = generateElement(a, b, d);
}
}
}
void exchangeColumns(double table[][k], int m, int n) {
int i = 0;
for (i = 0; i < r; i++) {
mySwap(&table[i][m - 1], &table[i][n - 1]);
}
}
void inputValidInt(int *x, char c) {
inputInt(x, c);
assert((*x > 0 && *x <= k) && "ERROR");
}
int main() {
int m = 0, n = 0, d = 0;
double a = 0, b = 0, table[r][k] = {0};
inputAB(&a, &b);
inputD(&d);
generateTable(table, a, b, d);
showTable(table);
inputValidInt(&m, 'm');
inputValidInt(&n, 'n');
exchangeColumns(table, m, n);
showTable(table);
return EXIT_SUCCESS;
}