Passing an array to a function

I have been trying pass the array to the function but i cant seem to get it right. every time i compile my code it is unsuccessful. can someone help me out please? This is what i have so far. The goal of this code is to get the 3 temperatures of the cities from the user, calculate the average and then display all temperatures as well as the average.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  /* 
 * File:   main.cpp
 * Author: gears
 *
 * Created on September 7, 2014, 8:41 PM
 */

#include <cstdlib>
#include <iostream>


void getTemps(float temp[SIZE]);
float calcAvg(float temp[SIZE]);
void displayAvg( float temp[SIZE]);


using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    
    const int SIZE = 2;
    float temp[SIZE];
    
    float average =0;
    
    getTemps( temp[SIZE]);
    average = calcAvg();
    displayAvg();

    return 0;
}

void getTemps(float temp[SIZE])
{
    
    cout << "what are the three temperatures in each city"<< endl;
    cout << "City #1" << endl;
    cin >> temp[0]
    cout << "City #2" << endl;
    cin >> temp[1];
    cout << "City #3 "<< endl;
    cin >> temp[2];
}
float calcAvg(float temp[SIZE])
{
    float average;
    for ( int i; i < temp[SIZE]; i++)
    {
        average += temp[i];
        return average;
    }
}
void displayAvg(float temp[SIZE])
{
    cout << "here are the temperatures"<< endl;
    
    for (int i; i < temp[SIZE]; i++)
    {
        cout << i << endl;
    }
    
}
The first thing I see is that SIZE is not defined before your first attempt to use it in the function prototype, and since the variable is defined inside main() it is not available in the function implementations as well. The solution to this problem would be to define the variable outside main() before the function prototypes.

If this doesn't solve your problem, post the error messages you're receiving, exactly as they appear in your development environment.
@jlb these are the errors im getting.


main.cpp:6: error: size of array `temp' has non-integral type `const float'
main.cpp:7: error: size of array `temp' has non-integral type `const float'
main.cpp:8: error: size of array `temp' has non-integral type `const float'
main.cpp: In function `int main(int, char**)':
main.cpp:19: error: size of array `temp' has non-integral type `const float'
main.cpp:23: error: invalid types `float[1][const float]' for array subscript
main.cpp:7: error: too few arguments to function `float calcAvg(float*)'
main.cpp:24: error: at this point in file
main.cpp:8: error: too few arguments to function `void displayAvg(float*)'
main.cpp:25: error: at this point in file
main.cpp: At global scope:
main.cpp:31: error: size of array `temp' has non-integral type `const float'
main.cpp: In function `void getTemps(float*)':
main.cpp:36: error: expected `;' before "cout"
main.cpp: At global scope:
main.cpp:42: error: size of array `temp' has non-integral type `const float'
main.cpp: In function `float calcAvg(float*)':
main.cpp:44: error: invalid types `float*[const float]' for array subscript
main.cpp: At global scope:
main.cpp:51: error: size of array `temp' has non-integral type `const float'
main.cpp: In function `void displayAvg(float*)':
main.cpp:54: error: invalid types `float*[const float]' for array subscript
main.cpp:59:2: warning: no newline at end of file
make[2]: *** [build/Debug/Cygwin-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
I fixed your code :) When you call a function, the argument cannot be empty. Like your code
average = calcAvg();
displayAvg();
is illegal.
You also miss a semicolon in line 41.
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  /*  
 * File:   main.cpp
 * Author: gears
 *
 * Created on September 7, 2014, 8:41 PM
 */

#include <cstdlib>
#include <iostream>

using namespace std;
const int SIZE = 2;
void getTemps(float temp[SIZE]);
float calcAvg(float temp[SIZE]);
void displayAvg( float temp[SIZE]);
   
int main(int argc, char** argv) {
    
    
    float temp[SIZE];
    getTemps( temp);
    displayAvg(temp);

    return 0;
}

void getTemps(float temp[SIZE])
{
    
    cout << "what are the three temperatures in each city"<< endl;
    cout << "City #1" << endl;
    cin >> temp[0];
    cout << "City #2" << endl;
    cin >> temp[1];
    cout << "City #3 "<< endl;
    cin >> temp[2];
}
float calcAvg(float temp[SIZE])
{
    float average=0,sum=0;
    for ( int i=0; i <=SIZE; i++)
    {
        sum += temp[i];
    }
    average=sum/(SIZE+1);
    return average;
}
void displayAvg(float temp[SIZE])
{
    cout << "here are the temperatures"<< endl;
    
    for (int i=0; i <=SIZE; i++)
    {
        cout << temp[i] << endl;
    }
    cout<<"The average is "<<calcAvg(temp)<<'\n';
    
}
Last edited on
i changed my code completely, i think i almost got it down. Now it says the variable "
average" in the calcAvg shadows the parameter....


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <cstdlib>
#include <iostream>


const int SIZE = 3;
void getTemps(float temp[],int SIZE);
float calcAvg(float temp[],int SIZE, float sum);
void displayAvg(float temp[],int SIZE, float average);

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    
    
    float temp[SIZE];
    
    float average =0;
    
    getTemps( temp,SIZE);
    average = calcAvg(temp,SIZE, average);
    displayAvg(temp,SIZE,average);

    return 0;
}

void getTemps(float temp[],int SIZE)
{
    cout << "enter the temperatures for each of the three cities"<< endl;
    for (int i = 0; i < SIZE; i ++)
    {
        cin >> temp[i];
    }
}
float calcAvg(float temp[],int SIZE, float average)
{
    float average;
    
    for ( int i; i < SIZE; i++)
    {
        average += temp[i];
    }
    return average;
}
void displayAvg(float temp[],int SIZE, float average)
{
    cout << "here are the temperatures"<< endl;
    
    for (int i; i < SIZE; i++)
    {
        cout << temp[i] << endl;
    }
    cout << "here is the average"<<average<< endl;
    
}

@jackiej ha i rewrote my code cause i thought maybe if i start from scratch i could get it right but its still wrong but It seems you fixed my old code, thank you so much! :))))))
closed account (48T7M4Gy)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <cstdlib>
#include <iostream>


const int SIZE = 3;
void getTemps(float temp[],int SIZE);
float calcAvg(float temp[],int SIZE, float sum);
void displayAvg(float temp[],int SIZE, float average);

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    
    
    float temp[SIZE];
    
    float average =0;
    
    getTemps( temp,SIZE);
    average = calcAvg(temp,SIZE, average);
    displayAvg(temp,SIZE,average);

    return 0;
}

void getTemps(float *temp,int SIZE) ////////////////////////////////////////////////////////////////////
{
    cout << "enter the temperatures for each of the three cities"<< endl;
    for (int i = 0; i < SIZE; i ++) //////////////////////////////////////////////////////////////////
    {
        cin >> temp[i];
    }
}
float calcAvg(float *temp,int SIZE, float average) ///////////////////////////////////////////////////
{
    //float average; /////////////////////////////////////////////////////////////////////////////////
    
    for ( int i = 0; i < SIZE; i++)
    {
        average += temp[i];
    }
    return average;
}
void displayAvg(float *temp,int SIZE, float average) //////////////////////////////////////////////////
{
    cout << "here are the temperatures"<< endl;
    
    for (int i= 0; i < SIZE; i++) ////////////////////////////////////////////////////////////////////
    {
        cout << temp[i] << endl;
    }
    cout << "here is the average "<<average/SIZE<< endl;  ///////////////////////////////////////////////
    
}



I decided to repair your first coding. Slashes show where the changed lines are.

a) to pass the array you use float *temp
b) Loop int i must be initialised. ie int i =0
c) Delete float average in the function.
Last edited on
Topic archived. No new replies allowed.