How to find the number of elements in an array and pass it to a function in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to find the number of elements in an array and pass it to a function in this code?

#include <iostream> using namespace std; int aver(int data[]) { float add=0; int n; for(int i = 0; i <=my_num-2; ++i) //I need to pass the number of elements on the array in the main function// { cout<<"value of i "<<i<<" "; cout<<num<<" "; cout<< data[i] << ' '<<"Array No. "<<i+1<<endl; int x=sizeof(data); add=add+data[i]; cout<<endl; n=i+1; } cout<<"The addition is = "<<add<<endl; float ave=add/n; cout<<"The Average of the six numbers is = "<<ave<<endl; } int main() { int num=0; cout<<"This Program Adds the elements of an Array"<<endl; cout<<" And calculates their Average"<<endl<<endl; cout<<"how many elements do you want in the Array? "; cin>>num; int data[num]; for(int j=0;j<=(num-1); ++j){ data[num]; cout<<"enter "<<j<<" Element "; cin>>data[j]; } int my_num=num aver(data); }

19th May 2018, 12:31 AM
Fabio
Fabio - avatar
2 Answers
0
Were should I plug the code in the main or the int aver function? Thnx
19th May 2018, 12:42 AM
Fabio
Fabio - avatar
0
Thnx
19th May 2018, 12:48 AM
Fabio
Fabio - avatar