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