Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Forum » x-Forum » Question

recursive function-CppBuzz-Forum

using namespace std;

float avg(float *t,int n)
{float x;

	return t[n-1] ;
else{x= (t[0]+avg(t+1,n-1));

		return x/6;

int main()
{int n;
cout<<"enter the table size n=";
    float *t=new float[n];
	for(int i=0;i<n;i++)
    {cout<<"enter the element t["<<i+1<<"]:";
	cout<<"the average of the table's elements is:"<<avg(t,n);
	return 0;

i wrote this code that should calculate the average of n elements 

x is the sum of the elements and n the array's length 

but for example for an x of 12 and an n of 6 
x/n is return 0.4228 instead of 2

Posted by citrix 2021-01-11 13:53:27

No Ans Posted

Please login to comment

Log In

Ask New Question