Why does my program return inf when I try to calculate the Mean Absolute Deviation?

user5858639

When I call the getDeviation function all it returns is 'inf' and I am not sure why. And I think the problem is in the for-loop in the getDeviation function, but I am not sure whats wrong with it. I am also a beginner to c++ so i'm sorry if there is a lot of stuff I screwed up on.

#include <iostream>
#include <cmath>

using namespace std;

double getAverage(int amount, int numbers[])
{
    // Declare the variables
    double total = 0;
    double avg = 0;
    //Find each number in the array then add it to the total
    for (int i = 0; i < amount; ++i) {
        total += numbers[i];
    }
    //Divide the total by the amount to get the average
    avg = total / amount;
    cout << "The Average is: " << avg << "\n";
    //Return back to the functions original state
    return 0;
}

double getDeviation(int amount, int numbers[], double avg)
{
    //Declare the variables and arrays
    int absoluteNums[1000] = {};
    double tempNum = 0;
    double absoNum = 0;
    double total = 0;
    double deviation = 0;

    //Search for each number, subtract the mean, make it an absolute value, then store it in another array
    for (int i = 0; i < amount; ++i) {
        tempNum += numbers[i];
        absoNum = abs(tempNum);
        numbers[i] = absoNum;
        numbers[i] -= avg;
        total += numbers[i];
    }

    //Divide the total by the average to get the deviation
    if (avg == 0) {
        return 0;
    }
    else {
        deviation = total / avg;
        cout << "The Mean Absolute Deviation is: " << deviation << "\n";
    }
    //Return back to the functions original state
    return 0;
}

int main()
{
    // Declare the variables and arrays
    int varNum = 1;
    int totVar = 0;
    int userNums[1000] = {};
    double avg;
    //Ask user for how many variables they want then record it
    cout << "How many variables would you like to have? (Max 999) ";
    cin >> totVar;
    //Check if totVar is to big
    if (totVar >= 1000) {
        cout << "To big. Nice try though!\n";
        return 0;
    }

    //Ask the user for each variable, then record it into the array
    for (int i = 0; i < totVar; ++i) {
        cout << "Please input variable " << varNum << ": ";
        cin >> userNums[i];
        varNum++;
    }
    //Make the avg value the average of the numbers using the getAverage function
    avg = getAverage(totVar, userNums);
    //Call the average function
    getAverage(totVar, userNums);
    //Call the deviation function
    getDeviation(totVar, userNums, avg);
    //Return back to the functions original state
    return 0;
}
zhm

1.

total is used before initialized in getDeviation().

You should always initialize variable before using them.

double absoNum = 0;
double total = 0;
double deviation = 0;

2.

getAverage() returns 0 at the last line. It should return avg.

3.

getDeviation() returns 0 at the last line. It should return deviation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my program break when I try to refresh the usestate?

Why does my C program print new blank lines when I try to print a string in the reverse order?

Why does my program say the ArrayList element exists when I try to add it even though it's new?

Why does my program throws SIGABRT when I try to delete[] array?

Why does my assembly program (x86) freezes in Dosbox when I try to run it?

Why does my C program crash when I try to realloc() a pointer array of structs?

Why does my program return "Segmentation fault (core dumped)" when I remove a conditional in "else if"?

Why does this return -1 when I try to find .IndexOf(a) in the list?

Why does my activity crash when I try to get a number?

Calculating mean absolute deviation

Why does my program crash when I increment a pointer and then delete it?

Why does my program not respond when I run it

Why does my program stop when I call a function?

Why does my program return the same character?

Why does my program return inaccurate result?

Why does my program return nothing?

Why does my program return a memory address?

Why does my program return zeros only?

When I use [[]] as my index number, why does it return this?

Why does my iex return a '-C' or a '-A' when I run this function

Why does my code return wrong sum when i concatenate it?

Why does my custom Binomial Function give me an $Inf$ in return?

Where can I find mad (mean absolute deviation) in scipy?

Where can I find mad (mean absolute deviation) in scipy?

Why is my program seg faulting when I try to read from a directory?

Why my program stops when i try to give values in an array of a struct?

Not sure why my program keeps prompting error when I try to close it?

Why is my program accessing values of the binaryString array when I try to print "value"?

Why does my program return an error stating the file doesn't exist when it does?