C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 03:44 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 8
Passing an array to a function

Im trying to pass the array A into the function 'determinant' in order to calculate its determinant. However I keep getting the following error: cannot convert `float (*)[((unsigned int)((int)x))]' to `float (*)[2]' for argument `1' to `float determinant(float (*)[2])'

Here is my code:

#include <stdio.h>

float determinant(float a[2][2]);

main ()
{
int col, row, x=2, y=2;
float A[y][x], B[y],detA;


printf("Enter Matrix A: ");

for(row=0 ; row<y; row++)
{
for(col=0 ; col<x ; col++)
scanf("%f", &A[row][col]);
}

printf("You entered:\n");

for(row=0 ; row<y; row++)
{
for(col=0 ; col<x ; col++)
printf("[%f]", A[row][col]);
putchar('\n');
}

printf("Enter Matrix B: ");

for(row=0 ; row<x; row++)
scanf("%f", &B[row]);


printf("You entered:\n");

for(row=0 ; row<y; row++)
printf("[%f]\n", B[row]);

detA = determinant(A);
printf("%f", detA);

}


/* Calculate determinant of matrix A */

float determinant(float a[2][2])
{
float det;
det = a[0][0]*a[1][1] - a[0][1]*a[1][0];
return det;
}


Im editing and compiling using Dev-C++ 4.9.9.2
Any help would be great, Thanks.
will of fortune is offline   Reply With Quote
Old 02-09-2010, 03:53 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,315
The problem could be that you are inadvertently using variable length arrays. Examine:
Code:
int col, row, x=2, y=2;
float A[y][x], B[y],detA;
x and y are not constants, yet you use them to create A and B. Therefore, A and B are variable length arrays. As a quick fix, you could write:
Code:
float A[2][2], B[2], detA;
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-09-2010, 04:13 AM   #3
Registered User
 
Join Date: Feb 2010
Posts: 8
Ah you're right, it works now when I replace x and y with 2.
Thanks
will of fortune is offline   Reply With Quote
Old 02-09-2010, 04:20 AM   #4
Registered User
 
Join Date: Feb 2010
Posts: 6
i would of used a pointer for it and then passed the length of the x and y.
kille6525 is offline   Reply With Quote
Old 02-09-2010, 05:26 AM   #5
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,782
<< !! Posting Code? Read this First !! >>
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-09-2010, 03:35 PM   #6
Registered User
 
Join Date: Feb 2010
Posts: 8
Mostly the same code as before but I'm trying to use functions and calling by reference to input the matrices. But I'm still getting a similar error as before, and I can't see where I'm going wrong. Thanks.

12 cannot convert `float (*)[2][2]' to `float* (*)[2]' for argument `1' to `void getmatrixA(float* (*)[2])'

13 cannot convert `float (*)[2]' to `float**' for argument `1' to `void getmatrixB(float**)'

Code:
#include <stdio.h>

void getmatrixA(float *A[2][2]);
void getmatrixB(float *B[2]);
float determinant(float a[2][2]);

main ()
{
     int col, row, x=2, y=2;
     float A[2][2], B[2], Ak[2][2], detA; 
     
     getmatrixA(&A);
     getmatrixB(&B);
     
     detA = determinant(A);
     printf("Determinant of A: %f\n", detA);
     
     for(row=0 ; row<y; row++)
     {
      for(col=0 ; col<x ; col++)
       {
        if(col=0)
        Ak[row][col]=B[row];
        else
        Ak[row][col]=A[row][col];
       }
     }

     printf("Ak:\n");
  
     for(row=0 ; row<y; row++)
     {
      for(col=0 ; col<x ; col++)
        printf("[%f]", Ak[row][col]);
      putchar('\n');
     }
}

void getmatrixA(float *A[2][2])
{    
     int col, row;
     
     printf("Enter Matrix A: ");
     
     for(row=0 ; row<2; row++)
     { 
      for(col=0 ; col<2 ; col++)
        scanf("%f", &A[row][col]);
     }

     printf("You entered:\n");
  
     for(row=0 ; row<2; row++)
     {
      for(col=0 ; col<2 ; col++)
        printf("[%f]", A[row][col]);
      putchar('\n');
     }
}

void getmatrixB(float *B[2])
{
     int col, row;
     
     printf("Enter Matrix B: ");
     
     for(row=0 ; row<2; row++)
      scanf("%f", &B[row]);
     

     printf("You entered:\n");
  
     for(row=0 ; row<2; row++)
        printf("[%f]\n", B[row]);
}
      

/* Calculate determinant of matrix A */ 

float determinant(float a[2][2])
{
  float det;          
  det = a[0][0]*a[1][1] - a[0][1]*a[1][0];
  return det;
}
will of fortune is offline   Reply With Quote
Old 02-09-2010, 03:49 PM   #7
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,782
The types are wrong. You are trying to send x by y arrays to the functions, whose type should be float [x][y], not float* [x][y]. Furthermore, don't pass the address of the array. That would create a pointer to an array, float (*)[x][y], which is not the same as a pointer to the first element.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
Question on passing array arguments to function, and not printing Matus C Programming 7 11-21-2008 04:27 PM
function passing argument..array ? jochen C Programming 2 09-30-2007 11:53 AM
Problem with Visual C++ Object-Oriented Programming Book. GameGenie C++ Programming 9 08-29-2005 11:21 PM
Interface Question smog890 C Programming 11 06-03-2002 05:06 PM


All times are GMT -6. The time now is 08:54 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22