please help me to check the code..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ballpen1019
    New Member
    • Mar 2010
    • 7

    please help me to check the code..

    my problem occur when run the program is i cant enter the value that i want to in the function(float read(float x[10],y[10]).
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
      
     
     main ()
     {
      float read(float x[10],float y[10]);
      float calculate_A(float x0,float y0);
      
      
      
          float a,b;
          float x[10];
          float y[10];
          float x0,x1,x2,y0,y1,y2;
          a=x0,b=y0;
          
          printf("the mean value for x is %f",a);
          printf("the mean value for y id %f",b);
          
          
          getch();
          }
          
          loat read(float x[10],float y[10])
          { int n,i;
           float x0,y0;       
           float x[10],y[10];
          
          
          puts("enter the number of data(n):\n");
          scanf(" %d",&n);
          puts("enter the values for x:\n");
          for(i=0;i<=9;i++)
          {scanf(" %f",&x[10]);}
          puts("enter the values for y:\n");
          for(i=0;i<=9;i++)
          {scanf(" %f",&y[10]);}
          return ;
           //system("pause");
          }
          float calculate_A(float x0,float y0)
          {
                int n,sum1,sum2,sum;
                
                float x[10],y[10];
                sum1=sum+x[10];
                x0=sum1/n;
                sum2=sum+y[10];
                y0=sum2/n;
                printf("%f",x0);
                printf("%f",y0);
                return(x0,y0);
                }
                
          //system("pause");
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like a confusion on the two arrays and and y. You have them defined in every function. I would expect you define them once in main and then call your functions using the addresses of the arrays:

    Code:
    float x[10];
    float y[10];
    
    
    MyFunc(x, y, 10, 10);
    where:

    Code:
    void MyFunc(float* a, float* b, int numelements_x, numelements_y)
    {
         Here a[1] is the same as x[1] in main.
    }
    You need to pass in the number of elements since the name of an array is the address of element 0 and from inside the function all you can see is this address. The number of elements in the array hhas been lost. This is called
    Code:
    decay of arra
    y and is just a thing to live with.

    Be sure to call your functions after the arrays are defined in main().

    Comment

    • ballpen1019
      New Member
      • Mar 2010
      • 7

      #3
      thank u weaknessforcats ..i have one question to ask u...how i suppose to send a value from a function to a function???i cannot send the values that i enter from the first function to another function...can you help me to check the code...thank you...
      Code:
      #include<stdlib.h>
      #include<stdio.h>
      #include<math.h>
      float read(void);
      void calculate(float*sum1,float*sum2);
      main ()
      
      {     float a,b,sum1,sum2;
          read();
           
           printf("%f",&sum1);
           printf("%f",&sum2);
           system("pause");
        }
           float read(void)
           {
                 int n,i;
                 float x[10],y[10];
                 puts("please enter a value of n:\n");
                 scanf("%d",&n);
                 puts("please enter the values for x:\n");
                 for(i=0;i<n;i++)
                 {scanf("%f",&x[i]);}
                 
                 puts("please enter the values for y:\n");
                 for(i=0;i<n;i++)
                 {scanf("%f",&y[i]);}
                 return((x[i],y[i]));
                 }
                 void calculate(float *sum1,float*sum2)
                 {
                      
                      int n,i;
                      float x[i],y[i];
                      float sum=0;
                      read();
                      *sum1=(sum+x[i]/n);
                      *sum2=(sum+y[i]/n);
                      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Please remember that a function when has completed executing, that is, when it returns, is an instance of its return type. That is,

        Code:
        int MyFunc();
        MyFunc() becomes an int if you execute it. SO this code:

        Code:
        int y = MyFunc();
        is prefectly OK becuase the compiler knows that by calling MyFunc() it will get the correct int value to assign to y. So this code:

        Code:
        void AnotherFunc(int arg);
        
        AnotherFunc(MyFunc());
        is also OK. The compiler knows that by calling MyFunc() it eill get the int value to supply to the arg argument of AnotherFunc.

        This is NOT OK:

        Code:
        void Func();
        
        int y = Func();
        void means "no type". Here the compiler knows that by calling Func() that nothing will be returned so there will be nothing to initialize y. In this case ytou get a compile-time error.

        Comment

        Working...