Hi! I'm new here. I have a problem. here it is: Write a program that includes two functions named calcAverage() and variance(). The calcAverage() function should calculate and return the average of the values stored in an array named testvals. The array should be declared in main() and should include the values 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73. The variance() function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in testvals, squaring the values obtained, adding them, and dividing by the number of elements in testvals. The values returned from calcAverage() and variance() should be displayed using cout statements activated from within main().
I need some help with this. I have no idea where to start from. Anyone can help me? I'm new to C++ any information would be wonderful! Thanks
if ur new u shouldnt jump into functions, do variables and stuff first.
always start with a pencil and paper. When you have a specific question on the project, then repost. Here's a freebie example of how to declare an array of int of known size:
constant int SIZE = 10;
and here's how to access the third element in myArray so you can asssign it the value of 23;
myArray = 23;
In order to know the average, you need to know the sum. To find the sum, simply go through every element of testVals, adding them to the sum. After you found the sum, store the average as the sum / number of elements.
From variance, call calcAverage() to get the average of the data. After you have the average, loop through the array again. For each item in the array, caculate the difference between the average and the item. Square the difference, and then add the difference to the totalVariance. After the end of the loop, divide totalVariance by the number of elements, and then you are done.
Are you having trouble with understanding for loops, arrays, functions?
I'm having problems with arrays.
an array is a collection of objects all of the same type. The type can be any legal type, such as the primitive types called int, long, double, char, etc. or a user defined type.
Each object in the collection is called an element.
Each element can be accessed by using it's index. The first index in the array is 0, the second is 1, the third is 2, etc.
When you declare an array using static memory, that is not using the keyword new, then you must indicate how many elements the array could hold if all elements were used up using a constant value. You do this by placing a constant integer value in the square brackets. The constant integer can be either a constant number like 10 or 3 or 5678 or a constant variable like:
const int SIZE = 10;
To declare the array first indicate the type to be stored in the array, the name of the array and then the number of elements to be used in the array placed in .
To access the first elemtent refer to it by using index 0 as outlined above.