c - Code to do Tolerance Analysis -
so, i'm trying build code , crashes somewhere in second custom function. i'd print out content of file "tin.txt", eof seems crash it. i'm coding in c.
it seems junk getting arrays. need use string tok anyway, hints that? edit far. i've put in of suggested changes, , compiler (codeblocks, in case matters) has stopped screaming in pain. i'm struggling information arrays using strtok, can't figure out how make work. (i'm unsure if should ask second question @ point or edit on, edited one.)
/************************************************************************* 3/25/2015 program takes in file of format 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 tolerance analysis **************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #define buffer_size 1024 #define arraysize 100 void input(float *nom,float *tollerance,int *signs,char *v_f,float spec_minnimum,float spec_maximum); void toleracningpt1(int size, float nom[],float tollerance[],int signs[],char v_f[],float spec_minnimum,float spec_maximum); int main(){ /**decs**/ float nom[arraysize]; //holds nominal values (2.00, .975 ect) float tollerance[arraysize]; //holds third value (.05, .025, ect) int signs[arraysize]; // signifies if value goes or down char v_f[arraysize]; // f things cannot changed, v things can int size=0; float spec_minnimum, spec_maximum; /**custom functions**/ input( nom, tollerance, signs, v_f, spec_minnimum, spec_maximum); toleracningpt1(size, nom, tollerance, signs, v_f, spec_minnimum, spec_maximum); } /***********************************************************************************************************/ void input(float *nom,float *tollerance,int *signs,char *v_f,float spec_minnimum,float spec_maximum){ const char *delimiter_characters = " "; const char *filename = "tin.txt"; file *input_file = fopen( filename, "r" ); char buffer[ buffer_size ]; char *last_token; printf("file data\n"); /* usual error check*/ if( input_file == null ){ fprintf( stderr, "unable open file %s\n", filename ); }else{ while( fgets(buffer, buffer_size, input_file) != null ){// while there stuff last_token = strtok( buffer, delimiter_characters ); while( last_token != null ){//same song.. printf( "%s\n", last_token ); last_token = strtok( null, delimiter_characters );// clear out last_token } } fclose( input_file ); } } /*****************************************************************************************************************/ void toleracningpt1(int size, float nom[],float tollerance[],int signs[],char v_f[],float spec_minnimum,float spec_maximum) { int x; float act_gap, act_tollerance, maximum_gap = 0.0, minnimum_gap = 0.0; ( x=0, act_gap = 0; x<size; x++){ //does tolerance math act_gap = act_gap + (nom[x]*signs[x]); } ( x=0, act_tollerance = 0; x<size; x++){ act_tollerance = act_tollerance + (tollerance[x]); } (x= 0, maximum_gap = 0; x<size; x++){ maximum_gap = (nom[x]*signs[x]+tollerance[x])+maximum_gap; minnimum_gap = (nom[x]*signs[x]-tollerance[x])+minnimum_gap; } printf("actual gap mean: %.3f\"\n", act_gap); //printing printf("actual gap tolerance: %.3f\"\n", act_tollerance); if (maximum_gap > spec_maximum){ printf("the maximum gap (%.3f\") (greater) specified (%.3f\")\n", maximum_gap, spec_maximum); } if (maximum_gap < spec_maximum){ printf("the maximum gap (%.3f\") (less) specified (%.3f\")\n", maximum_gap, spec_maximum); } if (minnimum_gap > spec_minnimum){ printf("the minimum gap (%.3f\") (greater) specified (%.3f\")\n", minnimum_gap, spec_minnimum); } if (minnimum_gap < spec_minnimum){ printf("the minimum gap (%.3f\") (less) specified (%.3f\")\n", minnimum_gap, spec_minnimum); } }
1) see problems input
function:
loop
for (i=0; status != eof; i++)
can lead segmentation fault in case filetin.txt
have more 100 lines (nom
, other arrays have fixed size).you send
float spec_minnimum
,float spec_maximum
argumentsinput
, expect receive values after function finished... not receive. must change arguments type these parameters ,scanf
:float input(float nom[],float tollerance[],int signs[],char v_f[],float * p_spec_minnimum,float * p_spec_maximum) . . . fscanf(ftin, "gap,%f,%f\n", p_spec_minnimum, p_spec_maximum);
so call function
input
input( nom, tollerance, signs, v_f, &spec_minnimum, &spec_maximum);
you save value returned
fsacnf
status
not check status of firstfscanf
... strange coding stylefunction
input
has typefloat
, in error case returns1
(integer value), , returns nothing if no error file openingafter function
input
finishmain
function has no information how lines read file, , how elements stored in arrays.
2) before using data idea see data @ screen, need function print data read file.
so propose following functions input
, data output
:
// returns number elements stored in arrays int input(float nom[],float tollerance[],int signs[],char v_f[],float* p_spec_minnimum,float* p_spec_maximum) { int status = 0, i,c; file *ftin; ftin = fopen ("tin.txt", "r"); if (ftin == null){ //file empty/broken error printf("error\n"); return (-1); } /******/ else{ (i=0; status != eof && < 100; ){ //reads until eof, though guy on stackoverflow taught me it's bad status = fscanf(ftin,"part,%f,%d,%f,%c\n", &nom[i], &signs[i], &tollerance[i], &v_f[i]); //scans part if(status == 4) { i++; } status = fscanf(ftin, "gap,%f,%f\n", p_spec_minnimum, p_spec_maximum); //scans gap printf("reading info arrays\n"); } fclose(ftin); return i; } } // output arrays in special format void output(int size, float nom[],float tollerance[],int signs[],char v_f[],float spec_minnimum,float spec_maximum) { int i; (i=0; < size; i++) { printf("part,%f,%d,%f,%c\n", nom[i], signs[i], tollerance[i], v_f[i]); } printf("gap,%f,%f\n", spec_minnimum, spec_maximum); }
3) size of arrays must known in toleracningpt1
function, make follows:
float toleracningpt1(int size, float nom[],float tollerance[],int signs[],char v_f[],float spec_minnimum,float spec_maximum) { int x; float act_gap, act_tollerance, maximum_gap = 0.0, minnimum_gap = 0.0; ( x=0, act_gap = 0; x<size; x++){ //does tolerance math act_gap = act_gap + (nom[x]*signs[x]); } ( x=0, act_tollerance = 0; x<size; x++){ act_tollerance = act_tollerance + (tollerance[x]); } (x= 0, maximum_gap = 0; x<size; x++){ maximum_gap = (nom[x]*signs[x]+tollerance[x])+maximum_gap; minnimum_gap = (nom[x]*signs[x]-tollerance[x])+minnimum_gap; } printf("actual gap mean: %.3f\"\n", act_gap); //printing printf("actual gap tolerance: %.3f\"\n", act_tollerance); if (maximum_gap > spec_maximum){ printf("the maximum gap (%.3f\") (greater) specified (%.3f\")\n", maximum_gap, spec_maximum); } if (maximum_gap < spec_maximum){ printf("the maximum gap (%.3f\") (less) specified (%.3f\")\n", maximum_gap, spec_maximum); } if (minnimum_gap > spec_minnimum){ printf("the minimum gap (%.3f\") (greater) specified (%.3f\")\n", minnimum_gap, spec_minnimum); } if (minnimum_gap < spec_minnimum){ printf("the minimum gap (%.3f\") (less) specified (%.3f\")\n", minnimum_gap, spec_minnimum); } }
and main function be
int main(void){ /**decs**/ float nom[100], tollerance[100]; int signs[100]; char v_f[100]; //int status, i, x; float act_gap, act_tollerance, spec_minnimum, spec_maximum; /**custom functions**/ int size = 0; size = input( nom, tollerance, signs, v_f, &spec_minnimum, &spec_maximum); output( size, nom, tollerance, signs, v_f, spec_minnimum, spec_maximum); toleracningpt1(size, nom, tollerance, signs, v_f, spec_minnimum, spec_maximum); }
p.s.: not know "tolerance analysis" is, after fixes, file have provided example, program output result:
actual gap mean: 0.025" actual gap tolerance: 0.085" maximum gap (0.110") (greater) specified (0.080") minimum gap (-0.060") (less) specified (0.000")
Comments
Post a Comment