Danny Mahoney
Danny Mahoney

Reputation: 1255

Array data not producing correct calculations into output

So I have a Weight Calculator assignment that I am doing for Uni and my Teachers cannot solve my issue. I have spent about three days trying to figure this out. I have executed the code by hand also and traced the steps and cannot figure it out.

I receive no errors but the calculations do not produce logical outcomes.

Oddly it is a very small program and produces the same result on multiple devices so it isn't just my PC or Tablet. I think the issue might be in the storing of the weight data in the first place, rather than the calculation itself.

I will try not to put too much code in but at the same time I want you guys to understand how I have built the program. I believe that nothing is being stored correctly into result.weight

I am fairly new to C++ and I have transitioned to Pascal. If there is something wrong with my logic in the code please help me out as it should work in my eyes.

part_data read_part()
{
    int option;
    part_data result;
    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");
}

float weight_of_kind(part_array &data, partkind kind)
{
    int i = 0;
    float result = 0.0;

    for (i = 0; i< data.num; i++);
    {
        if (data.array[i].kind == kind)
        {
            result += data.array[i].weight;
        }
    }
    return result;
}

float total_weight(part_array &data)
{
    float result = 0.0;
    int i;
    for (i = 0; i < data.num; i++)
    {
        result += data.array[i].weight;
    }
    return result;
}

void calculate_weight(part_array &data)
{
    int i;
    printf("Total weight: %4.2f\n", total_weight(data));
    printf("Total CRITICAL: %4.2f\n", weight_of_kind(data, CRITICAL));
    printf("Total DESIRED: %4.2f\n", weight_of_kind(data, DESIRED));
    printf("Total OPTIONAL: %4.2f\n", weight_of_kind(data, OPTIONAL));
}

Also, here are the types for reference

enum partkind {
    CRITICAL, DESIRED, OPTIONAL
};

typedef struct
{
    my_string name;//array
    partkind kind;
    long weight;
} part_data;

typedef struct 
{
    part_data *array; // pointer to a part
    int num;          // number of parts
} part_array;

This is the output I get when I try to produce a calculation of multiple persons' weights, categorized by kind (Critical, Desired or Optional)

enter image description here


UPDATE: Here are other related functions that are in the program that might help us debug. I appreciate all of the help I am getting and that you guys have refrained from 'downvoting' I know how tempting it is for some people

partkind read_part_kind()
{
        printf("1 - Critical\n");
        printf("2 - Desired\n");
        printf("3 - Optional\n");
    switch( read_integer_range("Please select a part kind: ", 1, 3) )
    {
        case 1: return CRITICAL;
        case 2: return DESIRED;
        default: return OPTIONAL;
    }
}

part_data read_part()
{
    int option;
    part_data result;

    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");

    return result;
}

void add_part(part_array &data)
{
    data.num++;
    data.array = (part_data*) realloc(data.array, sizeof(part_data)*data.num);
    data.array[data.num - 1] = read_part();
}

I hope this code helps make a little more sense out of why the input isn't getting assigned correctly and why Total weight is producing (seemingly) random memory.

Upvotes: 0

Views: 86

Answers (3)

Kabulan0lak
Kabulan0lak

Reputation: 2136

Use pointer instead of references. I would have done something like :

    part_data read_part()
    {
        int option;
        part_data result;
        result.name = read_string("Please Enter a name: ");
        result.kind = read_part_kind();
        result.weight = read_float("Please Enter weight: ");
        return result;
    }

    float weight_of_kind(part_array* data, partkind kind)
    {
        int i = 0;
        float result = 0.0;

        for (i = 0; i< data->num; i++);
        {
            if (data->array[i].kind == kind)
            {
                result += data->array[i].weight;
            }
        }
        return result;
    }

    float total_weight(part_array* data)
    {
        float result = 0.0;
        int i;
        for (i = 0; i < data->num; i++)
        {
            result += data->array[i].weight;
        }
        return result;
    }

    void calculate_weight(part_array* data)
    {
        int i;
        printf("Total weight: %4.2f\n", total_weight(data));
        printf("Total CRITICAL: %4.2f\n", weight_of_kind(data, CRITICAL));
        printf("Total DESIRED: %4.2f\n", weight_of_kind(data, DESIRED));
        printf("Total OPTIONAL: %4.2f\n", weight_of_kind(data, OPTIONAL));
    }

partkind read_part_kind()
{
        printf("1 - Critical\n");
        printf("2 - Desired\n");
        printf("3 - Optional\n");
    switch( read_integer_range("Please select a part kind: ", 1, 3) )
    {
        case 1: return CRITICAL;
        case 2: return DESIRED;
        default: return OPTIONAL;
    }
}

part_data read_part()
{
    int option;
    part_data result;

    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");

    return result;
}

void add_part(part_array* data)
{
    data->num++;
    data->array = (part_data*) realloc(data->array, sizeof(part_data)*data->num);
    data->array[data.num - 1] = read_part();
}

Upvotes: 0

Kabulan0lak
Kabulan0lak

Reputation: 2136

Your readPart() function doesn't return anything but it should return a part_data. And try to change the variable weight in float not long because your calculus returns a float.

typedef struct {
    my_string name;//array
    partkind kind;
    float weight; // <<<<<<<
} part_data;

AND return a part_data in read_part() :

part_data read_part()
{
    int option;
    part_data result;
    result.name = read_string("Please Enter a name: ");
    result.kind = read_part_kind();
    result.weight = read_float("Please Enter weight: ");
    return result; // <<<<<<<
}

Upvotes: 2

KjMag
KjMag

Reputation: 2780

You are trying to use a pointer part_data* array in functions calculate_weight() and total_weight() without initializing it, which causes it to contain random garbage. Before you can use that array, you should allocate a memory for it like this:

array = new part_data[n]

where n is the number of elements in the array. In your case you probably want n to equal data.num.

Also, you declare that readPart() function would return part_data, but it doesn't return anything.

Upvotes: 3

Related Questions