Need help!

This is a discussion on Need help! within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm new here. I have a problem. here it is: Write a program that includes two functions named calcAverage() ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Need help!

    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

  2. #2
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    if ur new u shouldnt jump into functions, do variables and stuff first.
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  3. #3
    Unregistered
    Guest
    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;
    int myArray[SIZE];


    and here's how to access the third element in myArray so you can asssign it the value of 23;

    myArray[2] = 23;

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Unhappy

    I'm having problems with arrays.

  6. #6
    Unregistered
    Guest
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21