I tried to make a program for solving the matrix using gauss elimination, but I don't know, how in line 9. replace "cals" where should be a count of columns but it is local in main.
So, how can I fix it?
Is there any way to put the number of columns in the matrix?
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <conio.h>
#include <time.h>
void gauss(double (*matrix)[cals], int cals, int rows)
{
int x = 1;
int y = 0;
int z = 0;
for(int check = 0; check < rows-1;check++)
{
for(int j = x;j<rows;j++)
{
if(matrix[j][z] == 0)
{
break;
}
float help = (matrix[j][z] / matrix[y][z])*-1;
for(int i = y; i < cals;i++)
{
matrix[j][i] = help*matrix[y][i]+matrix[j][i];
}
}
y++;
x++;
z++;
}
for(int i = 0; i < rows;i++)
{
for(int j = 0; j < cals; j++)
{
if(matrix[i][j] > 9 || matrix[i][j] < -9)
{
printf("%.0f ", matrix[i][j]);
}
else
{
printf(" %.0f ", matrix[i][j]);
}
}
printf("\n");
}
}
int main()
{
int rows = 3;
int cals = 3;
srand(time(NULL));
double matrix[rows][cals];
for(int i = 0; i < rows;i++)
{
for(int j = 0;j < cals;j++)
{
matrix[i][j] = rand() %9 + 1;
}
}
gauss(matrix, cals, rows);
return 0;
}
Related
I would like to change the numbers on the first and the last column and i can't do it. I also want to change the numabers at the corners of the table to the half -sum of the elemends above and below them.
Please be nice it's my second time writing code .
#include <stdio.h>
#define N 10
#define M 20
int load(double x[N][M]);
int print(double x[N][M]);
void printthearray(double x[N][M]);
int calculatetemperature(void);
int main(void) {
double data[N][M];
print((double(*)[M])data);
return 0;
}
int print(double x[N][M]) {
int i, j;
for (i = 0; i < M - 1; i++) {
printf("2.00 ", x[i][0]);
}
printf("\n");
for (i = 0; i < 8; i++) {
for (j = 0; j < 19; j++) {
printf("1.00 ", x[i][j]);
}
printf("\n");
}
for (i = 0; i < 19; i++) {
printf("3.00 ", x[i][9]);
}
return 0;
}
Insert in a vector B, from its beginning, the even numbers of vector A and, from its end, the
odd numbers of vector A (this item must be solved with only a single structure of
repetition);
i don't know the conditions i have tu put in do while loop
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int i, j, A[25], B[25], testeNumeroIgual = 0, aux;
srand(time(NULL));
i = 0;
do {
A[i] = rand() % 40;
testeNumeroIgual = 0;
for (j = 0; j < i; j++) {
if (A[j] == A[i]) {
testeNumeroIgual = 1;
}
}
if (testeNumeroIgual == 0)
i++;
} while(i < 25);
i = 0;
do {
if (A[i] % 2 == 0) {
B[i] = A[i];
}
else {
B[25 - i] = A[i];
}
i++;
} while(i < 25);
for (i = 0; i < 25; i++) {
printf("%d\n", B[i]);
}
return 0;
}
Generate the random number into a specific loop to isolate the algorithm.
Then use two indexes tail and head to put it on the right place.
Increase head and decrease tail when you insert.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 25
int main()
{
int i, A[SIZE], B[SIZE], tail, head ;
srand(time(NULL));
/* generate random number for A */
for (i = 0; i < SIZE; i++) {
A[i] = rand() % 40;
}
tail = SIZE - 1;
head = 0;
for (i = 0; i < SIZE; i++) {
if (A[i] % 2) {
B[tail--] = A[i];
} else {
B[head++] = A[i];
}
}
printf("A: \n");
for (i = 0; i < SIZE; i++) {
printf("%d %d\n",i, A[i]);
}
printf("\nB: \n");
for (i = 0; i < SIZE; i++) {
printf("%i %d\n", i, B[i]);
}
return 0;
}
Could someone help me please?
I need to perform the sum of two matrices that the data will be sent by the user and print the result in a new matrix.
I managed to capture the data from the two arrays, but when I try to add the two, the code does not print the sum, where is the error?
Thanks
#include <stdio.h>
#include <stdlib.h>
void sum(int *mat_A, int *mat_B, int *mat_C);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
calc_soma(*mat_A, *mat_B, *mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int *mat_A, int *mat_B, int *mat_C) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = *mat_A + *mat_B;
*mat_C = value;
}
}
}
You need to declare the parameters to sum() as 2-dimensional arrays, not int *, which is a pointer to a 1-dimensional array. Then use i and j as array indexes.
You're also calling the function with the wrong name calc_soma.
#include <stdio.h>
#include <stdlib.h>
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
sum(mat_A, mat_B, mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_A[i][j] + mat_B[i][j];
mat_C[i][j] = value;
}
}
}
I need to write a program that iterates through all possible combinations for a base-2 (binary) vector. If the size of this vector is 3 you can do this with three nested loops, like this:
bool array[3];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
array[0] = i;
array[1] = j;
array[2] = k;
}
}
}
But the problem is that in my application, the array size is variable and can basically be any number. If I'm looking to find all values of a 12-bit vector, I don't want to write 12 nested loops and so it is not maintainable to use the code above. Instead I have come up with the following solution:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define SIZE 12
int main(void)
{
bool array[SIZE];
for(int i = 0; i < SIZE; i++) array[i] = 1;
int max_num = pow(2, SIZE);
for(int i = 0; i < max_num; i++)
{
if(array[0] == 0) array[0]++;
else
{
array[0] = 0;
for(int j = 1; j < SIZE; j++)
{
if(array[j] == 1) array[j] = 0;
else
{
array[j] = 1;
break;
}
}
}
for(int j = 0; j < SIZE; j++)
{
printf("%d", array[j]);
if(j != SIZE - 1) printf(", ");
else printf("\n");
}
}
}
This still seems as a lot of code to me for such a relatively simple thing. My question is: is there a more efficient way to do this?
What you are doing with the array is effectively incrementing (adding one) to the number represented by the array.
Let's leave the incrementing to the compiler and use bits from the integer.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 12
int main(void)
{
bool array[SIZE];
int max_num = 1 << SIZE;
for(int i = 0; i < max_num; i++)
{
for(int j = 0; j < SIZE; j++)
{
array[j] = (i >> j) & 1;
}
for(int j = 0; j < SIZE; j++)
{
printf("%d", array[j]);
if(j != SIZE - 1) printf(", ");
else printf("\n");
}
}
}
As pointed out by others, it is essentially incrementing a binary number. However, in keeping with the spirit of the original code, I decided not to "cheat" by using native addition/increment operators to increment the vector, and came up with the following:
#include <stddef.h>
#include <stdbool.h>
bool first(size_t size, bool array[size])
{
size_t i;
for (i = 0; i < size; i++)
{
array[i] = 0;
}
return i > 0;
}
bool next(size_t size, bool array[size])
{
size_t i;
for (i = 0; i < size && array[i]; i++)
{
array[i] = 0;
}
if (i < size)
{
array[i] = 1;
return 1;
}
return 0;
}
#include <stdio.h>
int main(void)
{
enum { SIZE = 12 };
bool array[SIZE];
bool going;
for (going = first(SIZE, array); going; going = next(SIZE, array))
{
size_t i;
for (i = 0; i < SIZE - 1; i++)
{
printf("%d, ", array[i]);
}
printf("%d\n", array[i]);
}
return 0;
}
It could be adapted to work in other bases easily:
#include <stddef.h>
#include <stdbool.h>
bool first(size_t size, unsigned int array[size])
{
size_t i;
for (i = 0; i < size; i++)
{
array[i] = 0;
}
return i > 0;
}
bool next(size_t size, unsigned int array[size], unsigned int base)
{
size_t i;
for (i = 0; i < size && array[i] == base - 1; i++)
{
array[i] = 0;
}
if (i < size)
{
array[i]++;
return 1;
}
return 0;
}
#include <stdio.h>
int main(void)
{
enum { SIZE = 5 };
enum { BASE = 3 };
unsigned int array[SIZE];
bool going;
for (going = first(SIZE, array); going; going = next(SIZE, array, BASE))
{
size_t i;
for (i = 0; i < SIZE - 1; i++)
{
printf("%u, ", array[i]);
}
printf("%u\n", array[i]);
}
return 0;
}
I have to populate a 2D array using random numbers between 3 and 19.
The array is 4 x 3, the first two columns will represent two sides to a right triangle and the third column is the hypotenuse.
I'm pretty new to C, so I'm not sure where I'm going wrong. my output gives me the headers just fine but just one single vertical line of numbers instead of a 4 x 3 grid.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define ROW 4
#define COL 3
void printChart(double array[ROW][COL]);
int main(void)
{
double chart[ROW][COL];
double *ptrchart = &chart[0][0];
srand(time(NULL));
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
chart[i][j] = 3 + (rand() % 19);
}
}
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = 0;
}
printChart(ptrchart);
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = (double)sqrt(pow(chart[i][0], 2) + pow(chart[i][1], 2));
}
puts(" ");
printChart(ptrchart);
return 0;
}
void printChart(double array[ROW][COL])
{
printf("%s", "Side A\tSide B\tHypotenuse(Side C)\n");
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
printf("%.3f\t", array[i][j]);
if (j = 2)
{
puts(" ");
}
}
}
}
Any help would be greatly appreciated, and if I need to clarify anything please let me know.
you should just put (j==2) instead of (j=2) in your function since it changes j to always be 2 the way you wrote it
I ran the below code and it works
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define ROW 4
#define COL 3
void printChart(double array[ROW][COL])
{
printf("%s", "Side A\tSide B\tHypotenuse(Side C)\n");
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
printf("%.3f\t", array[i][j]);
if (j == 2)
{
puts(" ");
}
}
}
}
int main(void)
{
double chart[ROW][COL];
double *ptrchart = &chart[0][0];
srand(time(NULL));
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
chart[i][j] = 3 + (rand() % 19);
}
}
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = 0;
}
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = (double)sqrt(pow(chart[i][0], 2) + pow(chart[i][1], 2));
}
puts(" ");
printChart(ptrchart);
return 0;
}