floating array passing to function

The main function shoud pass to another function a float array and an integer indicating the size of the array and return the average of all the numbers in the array. How would I pass the array to my division function, add the sum of the numbers in the array and get the average. I can get this to work all when I add the sum in main and just divide the sum/size in the division function, but when I try doing it in the function I cant get it to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>

using namespace std;
float division(float,int);
int main()
{
      int size;              //Array size
      float array[size];     //Declaring array        
      cout << "How many numbers do you want to input? ";
      cin >> size;        
      for(int i=0;i<size;i++)   //Loop which inputs arrays data 
                                
      {
              cout << "Number " << i+1 << " is: ";
              cin>>array[i];
              ;
      }
      //Now calling division function to find the sum...
      cout<<"Average of array elements is "<<array,size)<< endl;
      system("pause");
      return 0;
}
float division(float,int)
{
    int size;
    float sum;
    float average=0;
    for(int i=0;i<size;i++)
        sum +=array[i];
    return sum/size;
}


closed account (zb0S216C)
When declaring array in main( ), size is uninitialized, which results in undefined behavior. You need to ask for the size before declaring array.

You can pass an array as an argument like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void vFunction( float fArray[ ], int iLength )
{
    //...
} 

int main( )
{
    float fMyArray[ ] = { 1.0f, 1.5f, 2.0f };

    vFunction( fMyArray, 3 );

    std::cin.get( );
    return 0;
}

Wazzak
Last edited on
closed account (yUq2Nwbp)
in declaration you must write

float division(float [],int);

when implemeting you must write

float division ( float x[] , int k )
{
}

.....but does this code work correct??.....aren't there errors while compiling??
David,

If I added the sums of the array in the main function then called the division function and just sum/size.... it worked. When passing the float array to the division function then adding their sums and dividing by size. It has not worked. I made a few changes to it as seen below and now it is working but this is the way off. Say I want to enter 3 numbers and find their average. I enter 3, 3, 3... which obviously the average is 3. I now get 1. #QNAN instead of 3.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
float division(float[],int);
int main()
{
      int size, sum;             //Array size
      float array[size];     //Declaring array        
      cout << "How many numbers do you want to input? ";
      cin >> size;        
      for(int i=0;i<size;i++)   //Loop which inputs arrays data 
                                
      {
              cout << "Number " << i+1 << " is: ";
              cin>>array[i];
              ;
      }
      //Now calling division function to find the sum...
      cout<<"Average of array elements is "<<division(array, size)<< endl;
      system("pause");
      return 0;
}
float division(float array[],int size)
{
    
    float sum;
    float average=0;
    for(int i=0;i<size;i++)
        sum +=array[i];
        average= sum/size;
    return sum;
}
In what compiler will this even compile in the first place? It is not legal to have a non-constant value for an array size...line 7 should cause a compiler error. And even if there was a compiler plugin/extension, size is still undefined when the array is created, so the array could be any size, even large enough to throw an exception.
closed account (yUq2Nwbp)
first of all......even if compiler doesn't give error while compiling this code , your code is incorrect.....and i tell you why.....

you have float array[size]......if you work on linux its all right but on windows it must be give error because on linux compiler takes memory from dynamic memory.......

second) your size isn't initialized so it contains garbage......so you had better do this....

float array[30] and then input your size.....30 for reserve.....

and finally in your division function you write

float sum;

and you didn't initialize it...........write float sum=0;


closed account (zb0S216C)
1) In division( ), sum is used without initialization( potential undefined behavior when used ).
2) sum in main( ) is unused( compiler warning ), and uninitialized.
3) size in main( ) is used before initialization( potential undefined behavior ).
4) size isn't checked for a negative numerical value before declaring array with it( potential undefined behavior ).
5) Use of system( ) causes the program to become OS dependent. Use std::cin.get( ) instead.

Wazzak
Last edited on
Thank you for all your replies but I ended up getting it to work afterall before seeing all these replies. The code works now.

1. Mainly I was getting the garbage answer after compiling because sum was not initialized. So when I tried to return it... it got garbage.
2. The other problem was that I was trying to return the sum instead of the average.
3. On Windows it is possible to have a non-constant value for the array size. I could have done... array[100] or any number but I want to make the array only as big as the number the user wanted to store for. I had to swap the location of where I declared the array putting it after a size was cin.

Try the code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>

using namespace std;

float division(float[],int);
int main()
{
      int size;             //Array size      
      cout << "How many numbers do you want to input? ";
      cin >> size; 
      float array[size];          
      for(int i=0;i<size;i++)   //Loop which inputs arrays data 
                                
      {
              cout << "Number " << i+1 << " is: ";
              cin>>array[i];
              
      }
      //Now calling division function to find the sum...
      cout<<"Average of array elements is "<<division(array, size)<< endl;
      system("pause");
      return 0;
}
float division(float array[],int size)
{
    
    float sum=0;
    float average=0;
    for(int i=0;i<size;i++)
        sum +=array[i];
        average= sum/size;
    return average;
}
Topic archived. No new replies allowed.