function noob need help

I am supposed to write a program where the user gets to input a desired amount of decimal numbers whereafter a function will be called that calculates the average of the chosen numbers. The function will cout the average number with 2 decimals and how many numbers there were. No return value. I have been fighting with this so long but I'm just not sure how functions work. I've been trying to read up a little bit but for no success. I would love some help with this.

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
#include <iostream>
#include <conio.h>

using namespace std;
void abc (float, int);
main()
{
      int i,k,l,o;
      float dec[40];
      char y;
      y='j';
      i=-1;
      l=0;
      cout<<"Ange dina decimaltal"<<endl;
      while (y=='j')
      {
            i++;
            cin>>dec[i];
            
            cout<<"ett tal till? (j/n)"<<endl;
            cin>>y;
            l++;
                }
            abc(dec,l);
           }
           
void abc (float dec, int l)
{
     int tot,r,i;
     tot=0;
     for (i=0;i<l;i++)
     {
         tot=tot+dec[i];
         }
         r=tot/l;
         cout<<"Medelvardet ar: "<<r<<endl;
         cout<<"Antal varden ar: "<<l<<endl;
                  }
      

      

I get these error messages:
In function `int main()':
\c++ övningar\lvl 2 uppgift 2 test.cpp 24 D:\c++ övningar\D [Error] cannot convert `float*' to `float' for argument `1' to `void abc(float, int)' line 24
\c++ övningar\lvl 2 uppgift 2 test.cpp 24 D:\c++ övningar\D In function `void abc(float, int)': line 24
\c++ övningar\lvl 2 uppgift 2 test.cpp 33 D:\c++ övningar\D [Error] invalid types `float[int]' for array subscript line 33
Last edited on
20:medelvarde (dectal[50],k);

I think you meant to take out that "[50]" like this:
medelvarde (dectal,k);

Also, dividing a sum of x numbers by two is not getting the average ;)
ops, ofc it's not the average, haha i don't know what I was thinking. :) thanks for the help.
edit: I started over with a new piece of code and I get some errors I've edited in the first post.
Last edited on
Line 5 and 27:
1
2
3
void abc (float[], int);

void abc (float dec[], int l)


Line 24 is still correct. But in the function definition and declaration use [] these, as you are passing an array.
Topic archived. No new replies allowed.