Click here to Skip to main content
15,036,363 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
initialized array A of numbers with floating point and integer variable V are given as local variable in main(). array consists of 9 elements value of integer variable V should be entred from the kaybord.

Write the progrem with function prototype, calling and definition, function should return the average of the elemnets of array A which have position index that is greater than V

function must take arguments print result on the screen from main() function.

What I have tried:

C++
int main()
{
    int V;
    float Array_A[9] = { 4.6, 2.9, 3.6, 1.0 ,2.3 ,7.8 ,4.4 ,5.7 ,1.4 };
    
    cout << "please enter a Variable V " << endl;
    cin>> V ;
    
    cout << "Your variable is : "<<V << endl;
}

float average(float Array_A[], int sizeOfarray)
{

    float sum = 0;
    
    for (int x = 0; x < sizeOfarray; x++)
    {
        sum += Array_A[x];
    }
    
    return sum / (float)sizeOfarray;

}
Posted
Updated 2-Jun-21 11:23am
v2
Comments
Richard MacCutchan 2-Jun-21 16:56pm
   
You need to use the value of V to get the average, as specified in paragraph 2 above.
jeron1 2-Jun-21 16:57pm
   
Do you have a specific question?

1 solution

A function prototype is the signature of the function - the return type, name, and parameters list - terminated with a semicolon instead of having the function body.
It is written before the first call to the function so that the system "knows" what the correct signature is and so that the actual function body can be anywhere in the file. Without it, you would get an "undefined function" error when you tried to call it before the actual definition and it isn't always possible to move it above all calls, especially when two mutually recursive functions are created.

Very often, they are stored in a #include file.

Then you need to read your instructions carefully - they are pretty explicit that V needs to be passed to the function so that it only calculates the average for elements with an index higher than the value passed.
   
Comments
CPallini 3-Jun-21 2:18am
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900