R
robix
Hi all. i'm developing a simple program for matrix add/multiplication but i
don't know if i'm going the right path.
I'm supposed to receive doubles in the output of my program but instead i
receive: 6, 8, etc... Is this right or not? Shouldn't i receive 6.00000,
8.0000?
This is my first c program... i think it crashes at the end, though i'm not
quite sure(i'm using Dev c++/MingW). i know i need to release the memory but
i haven't learned that (yet). Maybe it's because of that? I guess it is...
Anyway, fell free to add more input about the program... what should i
change, etc.
Thanks
Here is the source code:
#include <stdio.h>
#include <stdlib.h>
/* matrix structure */
typedef struct {
int col;
int lin;
double *element;
} matrix;
/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));
M.lin = lin;
M.col = col;
return M;
}
/* add two matrix */
matrix add_matrix(matrix mat1, matrix mat2)
{
matrix M;
int a, b;
M = init_matrix(mat1.lin, mat1.col);
printf("Insert the matrix number 1\n");
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
scanf("%e", &mat1.element[a]);
}
printf("Insert the matrix number 2\n");
for (b = 0; b< (mat1.lin * mat1.col); b++)
{
scanf("%e", &mat2.element);
}
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
M.element[a] = mat1.element[a] + mat2.element[a];
}
M.lin = mat1.lin;
M.col = mat1.col;
return M;
}
/*mul two matrix */
matrix mul_matrix(matrix mat1, matrix mat2)
{
/* in development*/
}
/* prints the resulting matrix in screen*/
void show_matrix(matrix mat1)
{
int a, b;
printf("Matrix = \n");
for (a = 0; a< mat1.lin; a++)
{
for (b = 0; b< mat1.col; b++)
{
if (b == mat1.col - 1)
printf("%e\n", mat1.element[a * mat1.col + b]);
else
printf("%e, ", mat1.element[a * mat1.col + b]);
}
}
}
int main()
{
matrix m1, m2, result;
int esc = 0, tam1 = 0, tam2 = 0, tam3 = 0, tam4 = 0;
system("CLS");
do {
printf("1 - Add Matrix\n");
printf("2 - Mul matrix\n");
printf("3 - Exit\n");
scanf("%i", &esc);
fflush (stdin) ;
if ((esc == 1) || (esc == 2)){
printf("insert the number of lines for the first matrix?\n");
scanf("%i", &tam1);
printf("insert the number of colowns for the first matrix?\n");
scanf("%d", &tam2);
printf("insert the number of lines for the second matrix?\n");
scanf("%d", &tam3);
printf("insert the number of colowns for the second matrix??\n");
scanf("%d", &tam4);
if (esc == 1) {
/* codigo adição */
if ((tam1 ==tam3) && (tam2 == tam4)) {
m1 = init_matrix(tam1, tam2);
m2 = init_matrix(tam3, tam4);
result = init_matrix(tam1, tam2);
result = add_matrix(m1, m2);
show_matrix(result);
} else {
printf("can't add matrix!\n");
}
} else {
/* codigo multiplicação */
if (tam1 == tam4) {
} else {
printf("can't multiply matrix!\n");
}
};
};
} while (esc != 3);
system("PAUSE");
return 0;
}
don't know if i'm going the right path.
I'm supposed to receive doubles in the output of my program but instead i
receive: 6, 8, etc... Is this right or not? Shouldn't i receive 6.00000,
8.0000?
This is my first c program... i think it crashes at the end, though i'm not
quite sure(i'm using Dev c++/MingW). i know i need to release the memory but
i haven't learned that (yet). Maybe it's because of that? I guess it is...
Anyway, fell free to add more input about the program... what should i
change, etc.
Thanks
Here is the source code:
#include <stdio.h>
#include <stdlib.h>
/* matrix structure */
typedef struct {
int col;
int lin;
double *element;
} matrix;
/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));
M.lin = lin;
M.col = col;
return M;
}
/* add two matrix */
matrix add_matrix(matrix mat1, matrix mat2)
{
matrix M;
int a, b;
M = init_matrix(mat1.lin, mat1.col);
printf("Insert the matrix number 1\n");
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
scanf("%e", &mat1.element[a]);
}
printf("Insert the matrix number 2\n");
for (b = 0; b< (mat1.lin * mat1.col); b++)
{
scanf("%e", &mat2.element);
}
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
M.element[a] = mat1.element[a] + mat2.element[a];
}
M.lin = mat1.lin;
M.col = mat1.col;
return M;
}
/*mul two matrix */
matrix mul_matrix(matrix mat1, matrix mat2)
{
/* in development*/
}
/* prints the resulting matrix in screen*/
void show_matrix(matrix mat1)
{
int a, b;
printf("Matrix = \n");
for (a = 0; a< mat1.lin; a++)
{
for (b = 0; b< mat1.col; b++)
{
if (b == mat1.col - 1)
printf("%e\n", mat1.element[a * mat1.col + b]);
else
printf("%e, ", mat1.element[a * mat1.col + b]);
}
}
}
int main()
{
matrix m1, m2, result;
int esc = 0, tam1 = 0, tam2 = 0, tam3 = 0, tam4 = 0;
system("CLS");
do {
printf("1 - Add Matrix\n");
printf("2 - Mul matrix\n");
printf("3 - Exit\n");
scanf("%i", &esc);
fflush (stdin) ;
if ((esc == 1) || (esc == 2)){
printf("insert the number of lines for the first matrix?\n");
scanf("%i", &tam1);
printf("insert the number of colowns for the first matrix?\n");
scanf("%d", &tam2);
printf("insert the number of lines for the second matrix?\n");
scanf("%d", &tam3);
printf("insert the number of colowns for the second matrix??\n");
scanf("%d", &tam4);
if (esc == 1) {
/* codigo adição */
if ((tam1 ==tam3) && (tam2 == tam4)) {
m1 = init_matrix(tam1, tam2);
m2 = init_matrix(tam3, tam4);
result = init_matrix(tam1, tam2);
result = add_matrix(m1, m2);
show_matrix(result);
} else {
printf("can't add matrix!\n");
}
} else {
/* codigo multiplicação */
if (tam1 == tam4) {
} else {
printf("can't multiply matrix!\n");
}
};
};
} while (esc != 3);
system("PAUSE");
return 0;
}