locked
How to return multiple arrays from functions in C (C++) using stdio.h,stdlib.h (without iostream.h)? RRS feed

  • Question

  • How to return multiple arrays from functions in C (C++) using stdio.h,stdlib.h (without iostream.h) without errors ?How to use pointers (*,**,&) ? For example :

    main.c

    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>


    #include "parser.h"

    void main(void)
    {

    printf ("\n 2015");
    input:
    datasort();
     
     
    goto input;

    }

    parser.h

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include "inputmodule.h"

    int datasort(  void )
    {

    int i1;
    int N1;
    char mode;
    float Ug0,Um,f,psi;
     
     float arrayf[17];
     float arrayUm[17];
     float arrayPsi[17];


    datainput (&mode, &N1, &Ug0, &arrayUm, &arrayf, &arrayPsi );

     printf ( "\n N =%d",N1);
     printf ( "\n Mode: %c",mode);
    for (i1=0;i1<N1;i1++)
     {
     printf ("\n i=%d " ,i1+1);
    fprintf ("  Um[%d]= %f V "  ,i1+1, arrayUm[i1]);
    fprintf ("  f[%d]=%f  Hz  " ,i1+1, arrayf[i1]);
    fprintf ("  psi[%d]=%f deg ",i1+1, arrayPsi[i1]);
    printf ("\n Ug=%f " ,Ug0);
     }

    getch();

    return 0;
    }

    inputmodule.h

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>

    void datainput (char *mod, int *n , float *Ubias, float **vectoru[17], float **vectorf[17], float **vectorph[17] )
    {


     float U[17]  , freq[17], Psi[17];   
     float U0 ,Umax;

    int i;
    int N;

    input1:

    printf ("\n ");
    printf ("\n Input order N (1...17) ");
    scanf ("%d",&N);
    printf ("\n  N=%d",N);
    if ((N>17)||(N<1)) goto input1;

    Inputmodule:

    printf ("\n a - Monoharmonic signal with bias ");
    scanf  ("%s",&*mod);
     if ( *mod=='a')  { goto ModuleA; }

    printf ("\n Reenter mode ");


    ModuleA:

    printf ("\n Entering of monoharmonic signal with bias ");
    printf ("\n Input amplitude of signal Um, V ");
    scanf ("%f", &U[0]);
    printf (" Input frequency of signal f,   Hz ");
    scanf ("%f", &freq[0]);
    printf (" Input phase     of signal psi,deg ");
    scanf ("%f", &Psi[0]);
    printf (" Input bias Ug0, V ");
    scanf ("%f", &U0);


     
    N=1;


    printf ("N=%d \n ",N);

    goto Outputmodule;


    Outputmodule:
    *n=N;
    *Ubias=U0;

    **vectoru[i]  =U  ;
    **vectorf[i]  =freq  ;
    **vectorph[i]  =Psi  ;

     
    printf ("\n Bias Ug0=%e V " ,*Ubias);
     
    for (i=0;i<N;i++)
    {
    printf ("\n i=%d " ,i+1);
    printf ("\n  Um[%d]= %f V "  ,i+1, **vectoru[i]);
    printf ("\n  f[%d]=%f  Hz  " ,i+1, **vectorf[i]);
    printf ("\n  psi[%d]=%f deg ",i+1, **vectorph[i]);
    }
     
    printf("\n");

    getch();

    return;
    }

    Saturday, January 24, 2015 6:15 PM

All replies

  • Simple example from Internet

    pointer.c

    #include <stdio.h>


     void swap(int *x, int *y) {
      int aux = *x;
      *x = *y;
      *y = aux;
    }
    int main() {
      int a = 2;
      int b = 3;
      printf("%d, %d\n", a, b);
      swap(&a, &b);
      printf("%d, %d\n", a, b);
    }

    Saturday, January 24, 2015 6:17 PM
  • How to return arrays from function?
    Saturday, January 24, 2015 6:18 PM
  • How to print to console  and how to ":=" without  cycle  **vectoru[i]  =U  ;
    **vectorf[i]  =freq  ;   **vectorph[i]  =Psi  ;   how to use ** in   **vectoru[i]  =U   and  for printf("", );   &
    Saturday, January 24, 2015 6:22 PM
  • How to do it without malloc , using subroutines without "class", "member", "malloc"?
    Saturday, January 24, 2015 6:23 PM
  • Working example :

    #include <stdio.h>
    int (*(Matrix_sum)(int matrix1[][3], int matrix2[][3]))[3]{
       int i, j;
        for(i = 0; i < 3; i++){
            for(j = 0; j < 3; j++){
                matrix1[i][j] = matrix1[i][j] + matrix2[i][j];
            }    }
       return matrix1;
    }
    int main(){
        int x[3][3], y[3][3];
        int (*a)[3];
         int i,j;
        printf("Enter the matrix1: \n");
        for(i = 0; i < 3; i++){
           for(j = 0; j < 3; j++){
                scanf("%d",&x[i][j]);
            }    }
       printf("Enter the matrix2: \n"); 
      for(i = 0; i < 3; i++){
           for(j = 0; j < 3; j++){ 
              scanf("%d",&y[i][j]);
            }    }
       a = Matrix_sum(x,y);
     ("The sum of the matrix is: \n");
       for(i = 0; i < 3; i++){ 
          for(j = 0; j < 3; j++){
                printf("%d",a[i][j]);
               printf("\t");
           }    
       printf("\n");
        } 
      return 0;
    }

        return matrix1      as    array   17*4 with N, Ug0  ,return as    a =  datainput( );  and try to decode as a[i][j]  , code return as  in swap example (type char  , *).


    • Edited by USERPC01 Sunday, January 25, 2015 1:39 AM
    Sunday, January 25, 2015 1:38 AM
  • Пример для размышления

     
    #include <iostream>
    using namespace std;

    int addition (int a, int b)
    { return (a+b); }

    int subtraction (int a, int b)
    { return (a-b); }

    int operation (int x, int y, int (*functocall)(int,int))
    {
      int g;
      g = (*functocall)(x,y);
      return (g);
    }

    int main ()
    {
      int m,n;
      int (*minus)(int,int) = subtraction;

      m = operation (7, 5, addition);
      n = operation (20, m, minus);
      cout <<n;
      return 0;
    }

    #include <stdlib.h>

    void function(float & x, float & y)
    {
    x= 900;

    y = x + 85;

    }

    int main(void)
    {
       
    float x,y;
    x = 10;
    y=20;


    function(x, y);
     
    printf ("%f   %f ", x,y);
    }
    return 0;
    }

    • Edited by USERPC01 Sunday, January 25, 2015 1:41 AM
    Sunday, January 25, 2015 1:40 AM
  • #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>

    void endgetch(void)
    {
    getch();
    return;    
    }

    typedef struct {
    int n;
    int Count;
    long double U[100];
    long double I[100];
    long double Z[100];
    long double f[100];
    long double fi[100];
    long double U0;
    long double Ubias[100];
    long double Ucc;
    char mode;

    } Signals;

     void swapdata( long double *x, long double *y)
    {
     long double temp;
    temp=*x;
    *x=*y;
    *y=temp;

    return;
    }

    void  InputSignal(Signals *s1  )
    {
    int i, N;
    long double U,f,fi,Ubias ;
    char mode;

    printf ("\n a - Monoharmonic signal with bias ");
    printf ("\n b - Polyharmonic monoamplitude signal with bias ");
    printf ("\n c - Polyharmonic polyamplitude signal with bias ");
    printf ("\n Select mode (a,b,c ) \n");
    printf("\n Input mode ");
    scanf("%ch",&mode);
    s1->mode=mode;
    printf("\n Input number of harmonics ");
    scanf("%d",&N);
     s1->n=N;
     
    printf("\n Input Ub   " );
    scanf("%lf",&Ubias);
    s1->U0=Ubias;
     
    printf("\n N=%d  \n",N );
    int flag1,flag2;
    if (mode=='a') { N=1; flag1=0; flag2=0; }
    if (mode=='b') { flag1=0;flag2=0; }
    if (mode=='c') { flag1=1;flag2=0;}
    if (mode=='d') { flag1=1; flag2=1; }


    if (flag1==0)
    {
    printf(" Input U ,V ");
    scanf("%lf",&U);       
    }  
    if (flag2==0)
    {       
     for (  i=0; i<N; i++)
     {


     if (flag1==1)
     {   
     printf(" Input U [%d],V ",i+1);
     scanf("%lf",&U);
     }
    s1->U[i]=U;
    printf(" Input f [%d],Hz ",i+1);
    scanf("%lf",&f);
    s1->f[i]=f;
    printf(" Input fi [%d],deg ",i+1);
    scanf("%lf",&fi);
    s1->fi[i]=fi;
    }
    }
     
    else
    {
    /*InputFunct(&s1);*/
    }


    return;
    }


    void SortByF(Signals *s1)
    {
    int i;
    char mode =s1->mode;
    int N=s1->n;   
    if ((mode=='b')||(mode=='c')||(mode=='d'))
    {
    for (i=0;i<N-1;i++)
    {
    if (s1->f[i]>s1->f[i+1])
    {
     swapdata(&s1->U[i], &s1->U[i+1]);
     swapdata(&s1->f[i], &s1->f[i+1]);
     swapdata(&s1->fi[i], &s1->fi[i+1]);
    }
    }
    }

    printf ("\nOptimizing  Ok");

      return;  
    }

    void  PrintSignal(Signals *s1  )
    {
    int i;
    long double U,f,fi,Ubias ;

    printf("\n Number of harmonics=%d ",s1->n);
    printf("\n   Ub =%lf V  " , s1->U0);

    for (  i=0; i<(s1->n); i++)
    {
    printf("\n U [%d]=%lf  V  ",i+1,s1->U[i]);
    printf("\n f [%d]=%lf  Hz ",i+1,s1->f[i]);
    printf("\n fi [%d]=%lf deg ",i+1,s1->fi[i]);
    }

    return;
    }

    int main (void){
      
    Signals Sig;
    InputSignal(&Sig); 
    PrintSignal(&Sig);
    SortByF(&Sig);
    endgetch();
    PrintSignal(&Sig);

    endgetch();
    return 0;
    }

    Saturday, February 7, 2015 2:16 PM