Jump to content

Matrix

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
Incompatible types in assigment, in lines 31 and 32

In add function have not yet add up operation, I just want to see if the array is correct


#include <stdio.h>

#include <stdlib.h>


int tam_l, tam_c;

float fill(){

      int i,j;

      printf("Enter the number of lines \n");

      scanf("%d",&tam_l);

      printf("Enter the number of columns \n");

      scanf("%d",&tam_c);

      float M[tam_l][tam_c];

      return **M;        

}

float add(){

      float** M1;

      M1 = (float**)malloc(sizeof(int)* tam_l * tam_c);

      float** M2;

      M2 = (float**)malloc(sizeof(int)* tam_l * tam_c);

      M1 = fill();

      M2 = fill();

      int i,j;

      for(i=0;i<tam_l;i++){

          for(j=0;j<tam_c;j++){

                     printf("M1[i][j] = %d \n",i,j,M1[i][j]);

                     }

                     }

      for(i=0;i<tam_l;i++){

          for(j=0;j<tam_c;j++){

                     printf("M2[i][j] = %d \n",i,j,M2[i][j]);

                                      }

                                      }    

}


int main()

{

  add();

  system("PAUSE");    

  return 0;

}



#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Your printf calls are wrong. Change it to this:
printf("M1[%d][%d] = %f\n",i,j,M1[i][j]);

Everything with your matrices is wrong. I don't have the time right now, but I'll be back in like an hour and point out specifically what. Meantime, please look up some pointer tutorials on Google and review.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Okay, I'm back. Several things:

1) Global variables (variables declared outside a function, i.e. tam_l and tam_c) are bad. Avoid them if you can.
2) Two arrays are implemented as arrays of pointers to arrays. You should initialize them like so:

size_t i;
float **matrix = (float **)malloc(sizeof(float *) * NUM_ROWS);
for(i = 0; i < NUM_COLS; ++i)
	matrix[i] = (float *)malloc(sizeof(float) * NUM_COLS);

Notice carefully what I pass in to malloc as the size.