I know nothing
I know nothing

Reputation: 1

Output of Code is all Zeros

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

float tolGap(float Tol[], int size);
float meanGap(float Nom[], float Imp[], int size);
void parsePart(char input[], float *pNom, int *pImp, float *pTol, char* pFoV);
void parseGap(char input[], float *max, float *min);

int main()
{
    FILE *pIN;
    int i;
    int Impact[10];
    float Nominal[10], Tolerance[10];
    char FoV[10];
    char input_str[30];
    float max, min, tol;
    int Num_of_Parts;
    float curr_max, curr_min, mean;

    pIN=fopen("testingcode.txt", "r");

    for(i=0; i<11; i++)
    {
        fgets(input_str,30, pIN);
        if(input_str[0] == 'p')
        {
            parsePart(input_str, &Nominal[i], &Impact[i], &Tolerance[i], &FoV[i]);
        }
        else
        {
            parseGap(input_str, &min, &max);
            Num_of_Parts = i;
            break;
        }
    }

    mean = meanGap(Nominal, Impact, Num_of_Parts);
    tol = tolGap(Tolerance, Num_of_Parts);
    curr_max = mean+tol;
    curr_min = mean - tol;

    if (fabs(max - curr_max) < 0.00001) //they are equal
    {
        printf("The Maximum Gap (%fin) is equal to specified (%fin)\n", max, curr_max);
    }

    if ((max - curr_max) > 0.00001)
    {
        printf("The Maximum Gap (%fin) is Greater than specified (%fin)\n", max, curr_max);
    }
    else
    {
        printf("The Maximum Gap (%fin) is within specified (%fin)\n", max, curr_max);
    }

    if (fabs(min - curr_min) < 0.00001) //they are equal
    {
        printf("The Minimum Gap (%fin) is equal to specified (%fin)\n", min, curr_min);
    }

    if ((min - curr_min) > 0.00001)
    {
        printf("The Minimum Gap (%fin) is Greater than specified (%fin)\n", min, curr_min);
    }
    else
    {
        printf("The Minimum Gap (%fin) is Less than specified (%fin)\n", min, curr_min);
    }

    return 0;
}

float tolGap(float Tol[], int size)
{
    int i;
    float sum=0;

    for (i=0; i<size; i++)
    {
        sum+=Tol[i];
    }
    printf("Actual Gap Tolerance: %fin\n", sum);
    return sum;
}

float meanGap(float Nom[], float Imp[], int size)
{
    int i;
    float sum=0;

    for(i=0; i<size; i++)
    {
        sum+= Nom[i]*Imp[i];
    }
    printf("Actual Gap Mean: %fin\n", sum);
    return sum;
}

void parsePart(char input[], float *pNom, int *pImp, float *pTol, char* pFoV)
{
    int i;
    char *elements[5];

    elements[0]=strtok(input, ",");

    for(i=1;i<5;i++)
    {
        elements[i] = strtok(NULL, ",");
    }

    *pNom = atof(elements[1]);
    *pImp = atoi(elements[2]);
    *pTol = atof(elements[3]);
    *pFoV = *elements[4];

}

void parseGap(char input[], float *max, float *min)
{
    int i;
    char *elements[5];

    elements[0]=strtok(input, ",");

    for(i=1;i<5;i++)
    {
        elements[i] = strtok(NULL, ",");
    }

    *max = atof(elements[1]);
    *min = atof(elements[2]);
}

My File has this information contained within it:

PART,2.000,-1,0.050,V

PART,0.975,-1,0.025,V

PART,3.000,+1,0.010,F

GAP,0.000,0.080

In the file there are no blank lines, I added them for clarity. The issue is that none of my calculations are being done. All of my outputs come to be 0.000000. I want it to actually calculate it but can't find the problem. Any help would be greatly appreciated!

Upvotes: 0

Views: 98

Answers (2)

David Ranieri
David Ranieri

Reputation: 41017

You are passing an array of ints

int Impact[10];

...

mean = meanGap(Nominal, Impact, Num_of_Parts);

to a function expecting an array of floats

float meanGap(float Nom[], float Imp[], int size)

You are wriitng outside the bounds of arrays:

int Impact[10];
float Nominal[10], Tolerance[10];
char FoV[10];
...

for(i=0; i<11; i++)

should be

for(i=0; i<10; i++)

And as pointed out by @user2340048, PART is in uppercase and you are checking for p in if(input_str[0] == 'p')

Upvotes: 1

user2340048
user2340048

Reputation: 187

The following line: if(input_str[0] == 'p') should be replaced with: if(input_str[0] == 'P') since in the data file, the 'PART' is in uppercase but you are checking for lowercase 'p'.

Upvotes: 0

Related Questions