-1

Hi guys I have a question

How do I pass a Multi-dimensional Array from a Function to the main function and from there we want to pass it to another function.

float array2[][3] = get_sizes(numbers);

The compiler says that the string above is an error because getsizes is an invalid initaliser. Can somebody tell us what we are doing wrong? Thank you in advance.

EDIT

int main() 

{  

float anzahl = get_number();

if(feof(stdin))
{
return 0;
}
int anzahl2 = anzahl;
float array2[][3] = get_sizes(anzahl);
if (feof(stdin)) 
{
  return 0;
}
triangulation(array2, anzahl2);  

return 0;
}

int get_number(void)
{
printf("Please enter the number of triangles to check: ");
int anzahl = 0;
char check = 0;
int ret = 0;
while(!(ret == 2 && check == '\n'))
{
ret = scanf("%d%c", &anzahl, &check);
if (feof(stdin))
{
  return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
printf("Please enter the number of triangles to check: ");
my_flush();  
}
if((check == '\n' && anzahl > UCHAR_MAX) || (check == '\n' && anzahl <= 0))
{
printf("[ERR] Invalid number of triangles.\n");
printf("Please enter the number of triangles to check: ");
ret = 3;
}
}
return anzahl;

void my_flush(void) 
{
while(getchar() != '\n')
{
}
}

int vgl(const void *a, const void *b)
{
int aa, bb;
aa = *(int *)a;
bb = *(int *)b; 
return (aa - bb);   

float get_sizes(int anzahl)
{
float array [3] = {0,0,0};
float array2[100][3];
while(anzahl>0)
{
char check = 0;
int ret = 0;    
while(!(ret == 2 && check == '\n'))
{
  printf("Please enter the first number of the triplet: ");
  ret = scanf("%f%c",&array[0], &check);
  if (feof(stdin))
  {
    return 0;
  }
  if (check != '\n')
  {
    printf("[ERR] Invalid number of triangles.\n");
    my_flush();  
   }
   if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >= 
   FLT_MAX))
   {
    printf("[ERR] Invalid number of triangles.\n");
    ret = 3;
   }
   }
   check = 0;
   ret = 0;
   while(!(ret == 2 && check == '\n'))
   {
  printf("Please enter the second number of the triplet: ");
  ret = scanf("%f%c",&array[1], &check);
  if (feof(stdin))
  {
    return 0;
  }
  if (check != '\n')
  {
  printf("[ERR] Invalid number of triangles.\n");
  my_flush();  
  }
  if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >= 
  FLT_MAX))
  {
  printf("[ERR] Invalid number of triangles.\n");
  ret = 3;
  }
  }
  check = 0;
  ret = 0;
  while(!(ret == 2 && check == '\n'))
  {
  printf("Please enter the third number of the triplet: ");
  ret = scanf("%f%c",&array[2], &check);
  if (feof(stdin))
  {
    return 0;
  }
  if (check != '\n')
  {
  printf("[ERR] Invalid number of triangles.\n");
  my_flush();  
  }
  if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >= 
  FLT_MAX))
  {
  printf("[ERR] Invalid number of triangles.\n");
  ret = 3;
  }
  }
  qsort(array, 3, sizeof(array[3]), vgl);
  array2[anzahl][0]=array[0];
  array2[anzahl][1]=array[1];
  array2[anzahl][2]=array[2];
  anzahl--;
  } 
  return array2;

  float triangulation(float array2[100][3], int anzahl2)
  {
  int number = 1;
  while(anzahl2>0)
  {
  if(array2[anzahl2][0] == 1 && array2[anzahl2][1] == 1 && array2[anzahl2]
  [2] == 2)
  {
  printf("Tripelt %d (a=%f, b=%f, c=%f)is NO triangle.\n", number, 
  array2[anzahl2][0], array2[anzahl2][1], array2[anzahl2][2]);
  }
  else
  {
  printf("Tripelt %d (a=%f, b=%f, c=%f)is a triangle.\n", number, 
  array2[anzahl2][0], array2[anzahl2][1], array2[anzahl2][2]);
  if(array2[anzahl2][0] == array2[anzahl2][1] && array2[anzahl2][0] == 
  array2[anzahl2][2] && array2[anzahl2][1]== array2[anzahl2][2])
  {
  printf("It is an equilateral triangle.\n");
  printf("It is a isosceles triangle.\n");
  }
  else if(array2[anzahl2][0] == array2[anzahl2][1] || array2[anzahl2][0] == 
  array2[anzahl2][2] || array2[anzahl2][1]== array2[anzahl2][2])
  {
    printf("It is a isosceles triangle.\n");
  }
  else if(((array2[anzahl2][0])*(array2[anzahl2][0])) + ((array2[anzahl2]
  [1])*(array2[anzahl2][1])) == ((array2[anzahl2][2])*(array2[anzahl2][2])))
  {
    printf("It is a right triangle.\n");
  }
  } 
   anzahl2--;
   number++;
   }        
   }

This is the whole code i'ts long

and we aren't allowed to use global variables

makoto
  • 45
  • 6

2 Answers2

0

Here is a demonstrative program that shows how in general it can be done.

#include <stdio.h>
#include <stdlib.h>

#define N   3

float ( * get_sizes( size_t n ) )[N]
{
    float ( *a )[N] = malloc( n * sizeof( float[N] ) );

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            a[i][j] = i * N + j;
        }
    }

    return a;
}

void display( float( *a )[N], size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            printf( "%4.1f ", a[i][j] );
        }
        putchar( '\n' );
    }
}

int main(void) 
{
    size_t n = 4;
    float ( *a )[N] = get_sizes( 4 );

    display( a, n );

    free( a );

    return 0;
}

The program output is

 0.0  1.0  2.0 
 3.0  4.0  5.0 
 6.0  7.0  8.0 
 9.0 10.0 11.0 

That is you need to allocate the array dynamically.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

This simple code indicates you how to send a bi-dimensional array to a function. The function func1 retrieves the value in float a pointed by int x and int y. The same should be done also for more than two dimensions.

#include <stdio.h>

float func1(float *,int x, int y,int mx, int my);

float func1(float * a,int x, int y,int mx, int my)
{
    if (x>=mx)
        return 0;

    if (y>=my)
        return 0;

    return a[x*my+y];
}

int main(void)
{
#define _MX 4
#define _MY 3
    int x, y;

    float a[_MY][_MX] = {
        {0.0,0.1,0.2,0.3},
        {1.0,1.1,1.2,1.3},
        {2.0,2.1,2.2,2.3}
    };

    for(x=0;x<_MX;x++) {
        for(y=0;y<_MY;y++){
            // With cast
            printf("x =%2d y =%2d => v = %f\n",x,y,func1((float *)a,x,y,_MX,_MY));
        }
    }

    puts("---------------------------------");

    for(x=0;x<_MX;x++) {
        for(y=0;y<_MY;y++){
            // Without cast
            printf("x =%2d y =%2d => v = %f\n",x,y,func1(a[0],x,y,_MX,_MY));
        }
    }

    return 0;
}
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22