Programa de Cálculo Matricial
Hola estaba interesado en que alguien me ayudara a depurar el codigo de este programa de calculo matricial, el error que me da es un:
statment missing ; in function main()
Can not convert "int" to "float" in function main()
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <alloc.h>
void main (void)
{
int i,j,k;
char tira[10];
int n,l,m;
char opera,opc;
float *mat1,*mat2;
float elemento;
clrscr();
do
{
printf ("nn ***** Operaciones con matrices **** ");
printf (" ¨ Que operacion desea realizar: (+) (-) (*) (t) ? ");
opera=getche();
clrscr();
switch (opera)
{
case '+':
printf ("nn Suma de matrices: S(n,l) = A(n,l) + B(n,l) ");
printf ("nn Introduzca las dimensiones (n,l) de las matrices.");
printf (" Indice n: ");
gets (tira); n=atoi(tira);
printf (" Indice l: ");
gets (tira); l=atoi(tira);
if ( (mat1=(float *) malloc (sizeof (float)*n*l))== NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
if ( (mat2=(float *) malloc (sizeof (float)*n*l))== NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
printf ("nn Matriz A(%d,%d): ",n,l);
for ( i=0 ; i<n ; i++)
{
for ( j=0 ; j<1 ; j++ )
{
printf ("n Elemento (%d,%d): ", i,j);
gets(tira); * ( mat1+(i*l)+j ) = atof(tira);
}
}
printf ("nn Matriz B(%d,%d): ",n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat2 + (i*l) +j ) = atof(tira);
}
}
printf ("nn Matriz S(%d,%d): ",n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<n ; j++ )
printf ("n %.1f t", *(mat1 + (i*l) + j) + *(mat2 + (i*l) + j));
printf ("n");
}
break;
case '-':
printf ("nn Resta de matrices: R(n,l) = A(n,l) - B(n,l) ");
printf ("nn Introduzca las dimensiones (n,l) de las matrices. ");
printf ("nn Indice n: ");
gets (tira); n=atoi(tira);
printf ("n Indice l: ");
gets (tira); l=atoi(tira);
if ( (mat1 = (float *) malloc (sizeof (float)*n*l) ) == NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
if ( (mat2 = (float *) malloc (sizeof (float)*n*l) ) == NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
printf ("nn Matriz A(%d,%d): ", n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("nn Elemento (%d,%d): ",i,j);
gets (tira); *(mat1 + (i*l) + j) = atof (tira);
}
}
printf ("nn Matriz B(%d,%d): ", n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("Elemento (%d,%d): ", i,j);
gets (tira); *(mat2 + (i*l) + j) = atof (tira);
}
}
printf (" Matriz R(%d,%d): n", n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ );
printf ("%.1f t", *(mat1 + (i*l) + j) - *(mat2 + (i*l) + j));
printf ("n");
}
break;
case '*':
printf ("Producto de matrices: P(n,l) = A(n,m) * B(m,l) n");
printf ("Introduzca las matricesn");
printf ("Matriz A(n,m): n");
printf ("nn Indice n: ");
gets (tira); n=atoi(tira);
printf ("nn Indice m: ");
gets (tira); m=atoi(tira);
if ( (mat1 = (float *) malloc (sizeof (float)*n*m) ) == NULL)
{
printf ("nn Error, no hay suficiente memoria ! ");
exit (1);
}
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<m ; j++ )
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat1 + (i*m) + j) = atof(tira);
}
}
printf ("nn Matriz B(m,l): ");
printf ("nn Indice l: ");
gets (tira); l=atoi(tira);
if ( (mat2 = (float *) malloc ( sizeof (float)*m*l) ) == NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
for ( i=0 ; i<m ; i++ );
{
for ( j=0 ; j<l ; j++ );
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat2+(i*l) + j) = atof(tira);
}
}
printf ("nn Matriz P(%d,%d): n",n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
for ( k=0,elemento=0 ; k<m ; k++ )
elemento += *(mat1+(i*m)+k)*(*(mat2+(k*l)+j));
printf ("%.1f t", elemento);
}
printf ("n");
}
break;
case 't':
printf ("nn Matriz transpuesta: T(l,n) = A(n,l)n ");
printf ("nn Introduzca la matriz A(n,l): ");
printf ("nn Indice n: ");
gets (tira); n=atoi(tira);
printf ("nn Indice l: ");
gets (tira); l=atoi(tira);
if ((mat1=(float *)malloc((sizeof(float)*n*l))== NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat1 + (i*l) + j) = atof(tira);
}
}
printf ("nn Matriz T(%d,%d): n", l,n);
for ( i=0 ; i<l ; i++ )
{
for ( j=0 ; j<n ; j++ )
printf ("%.1f t", *(mat1 + (i*n) + j));
printf ("n");
}
break;
default:
printf ("nn Esa operaci¢n no es posible nn");
}
printf ("nn ¨ Quiere realizar otra operacion ? (s/n) ");
opc=getche();
} while ( (opc == 's') || (opc == 'S') );
free (mat1);
free (mat2);
}
statment missing ; in function main()
Can not convert "int" to "float" in function main()
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <alloc.h>
void main (void)
{
int i,j,k;
char tira[10];
int n,l,m;
char opera,opc;
float *mat1,*mat2;
float elemento;
clrscr();
do
{
printf ("nn ***** Operaciones con matrices **** ");
printf (" ¨ Que operacion desea realizar: (+) (-) (*) (t) ? ");
opera=getche();
clrscr();
switch (opera)
{
case '+':
printf ("nn Suma de matrices: S(n,l) = A(n,l) + B(n,l) ");
printf ("nn Introduzca las dimensiones (n,l) de las matrices.");
printf (" Indice n: ");
gets (tira); n=atoi(tira);
printf (" Indice l: ");
gets (tira); l=atoi(tira);
if ( (mat1=(float *) malloc (sizeof (float)*n*l))== NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
if ( (mat2=(float *) malloc (sizeof (float)*n*l))== NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
printf ("nn Matriz A(%d,%d): ",n,l);
for ( i=0 ; i<n ; i++)
{
for ( j=0 ; j<1 ; j++ )
{
printf ("n Elemento (%d,%d): ", i,j);
gets(tira); * ( mat1+(i*l)+j ) = atof(tira);
}
}
printf ("nn Matriz B(%d,%d): ",n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat2 + (i*l) +j ) = atof(tira);
}
}
printf ("nn Matriz S(%d,%d): ",n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<n ; j++ )
printf ("n %.1f t", *(mat1 + (i*l) + j) + *(mat2 + (i*l) + j));
printf ("n");
}
break;
case '-':
printf ("nn Resta de matrices: R(n,l) = A(n,l) - B(n,l) ");
printf ("nn Introduzca las dimensiones (n,l) de las matrices. ");
printf ("nn Indice n: ");
gets (tira); n=atoi(tira);
printf ("n Indice l: ");
gets (tira); l=atoi(tira);
if ( (mat1 = (float *) malloc (sizeof (float)*n*l) ) == NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
if ( (mat2 = (float *) malloc (sizeof (float)*n*l) ) == NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
printf ("nn Matriz A(%d,%d): ", n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("nn Elemento (%d,%d): ",i,j);
gets (tira); *(mat1 + (i*l) + j) = atof (tira);
}
}
printf ("nn Matriz B(%d,%d): ", n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("Elemento (%d,%d): ", i,j);
gets (tira); *(mat2 + (i*l) + j) = atof (tira);
}
}
printf (" Matriz R(%d,%d): n", n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ );
printf ("%.1f t", *(mat1 + (i*l) + j) - *(mat2 + (i*l) + j));
printf ("n");
}
break;
case '*':
printf ("Producto de matrices: P(n,l) = A(n,m) * B(m,l) n");
printf ("Introduzca las matricesn");
printf ("Matriz A(n,m): n");
printf ("nn Indice n: ");
gets (tira); n=atoi(tira);
printf ("nn Indice m: ");
gets (tira); m=atoi(tira);
if ( (mat1 = (float *) malloc (sizeof (float)*n*m) ) == NULL)
{
printf ("nn Error, no hay suficiente memoria ! ");
exit (1);
}
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<m ; j++ )
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat1 + (i*m) + j) = atof(tira);
}
}
printf ("nn Matriz B(m,l): ");
printf ("nn Indice l: ");
gets (tira); l=atoi(tira);
if ( (mat2 = (float *) malloc ( sizeof (float)*m*l) ) == NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
for ( i=0 ; i<m ; i++ );
{
for ( j=0 ; j<l ; j++ );
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat2+(i*l) + j) = atof(tira);
}
}
printf ("nn Matriz P(%d,%d): n",n,l);
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
for ( k=0,elemento=0 ; k<m ; k++ )
elemento += *(mat1+(i*m)+k)*(*(mat2+(k*l)+j));
printf ("%.1f t", elemento);
}
printf ("n");
}
break;
case 't':
printf ("nn Matriz transpuesta: T(l,n) = A(n,l)n ");
printf ("nn Introduzca la matriz A(n,l): ");
printf ("nn Indice n: ");
gets (tira); n=atoi(tira);
printf ("nn Indice l: ");
gets (tira); l=atoi(tira);
if ((mat1=(float *)malloc((sizeof(float)*n*l))== NULL)
{
printf ("nn Error, no hay memoria suficiente ! ");
exit (1);
}
for ( i=0 ; i<n ; i++ )
{
for ( j=0 ; j<l ; j++ )
{
printf ("nn Elemento (%d,%d): ", i,j);
gets (tira); *(mat1 + (i*l) + j) = atof(tira);
}
}
printf ("nn Matriz T(%d,%d): n", l,n);
for ( i=0 ; i<l ; i++ )
{
for ( j=0 ; j<n ; j++ )
printf ("%.1f t", *(mat1 + (i*n) + j));
printf ("n");
}
break;
default:
printf ("nn Esa operaci¢n no es posible nn");
}
printf ("nn ¨ Quiere realizar otra operacion ? (s/n) ");
opc=getche();
} while ( (opc == 's') || (opc == 'S') );
free (mat1);
free (mat2);
}
-El error está en case 't', se trata del uso de los paréntesis, esta es la línea corregida:
if ((mat1=(float*)(malloc(sizeof(float)*n*l)))== NULL)
if ((mat1=(float*)(malloc(sizeof(float)*n*l)))== NULL)