Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / questions and answers / codebloc

ID: 3682433 • Letter: H

Question

home / study / engineering / computer science / questions and answers / codeblocks, c code, the input to the program will ...

Question

Codeblocks, C code,

The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is ‘GAP’ the following values are the minimum and maximum sizes. (Note: assume all units are inches.)

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

These values will be processed using the method taught in class. A sample output for the first stage is given.

Actual Gap Mean: 0.025”

Actual Gap Tolerance: 0.085”

The Maximum Gap (0.110”) is (Greater) than specified (0.080”)

The Minimum Gap (-0.060”) is (Less) than the specified (0.000”)

Explanation / Answer

/*** C code for  tolerance analysis ***/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100
// returns the 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 ("filename", "r");

if (FTIN == NULL){ //file empty/broken error
printf("ERROR ");
return (-1);
}
/******/
else{
for (i=0; status != EOF && i < 100; ){ //reads until EoF, even though some guy on stackoverflow taught me it's bad
status = fscanf(FTIN,"PART,%f,%d,%f,%c ", &nom[i], &SIGNS[i], &tollerance[i], &V_F[i]); //scans for a part
if(status == 4)
{
i++;
}
status = fscanf(FTIN, "GAP,%f,%f ", p_Spec_Minnimum, p_Spec_Maximum); //scans for a gap
//printf("Reading Info Into Arrays ");
}
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;
for (i=0; i < size; i++)
{
printf("PART,%f,%d,%f,%c ", nom[i], SIGNS[i], tollerance[i], V_F[i]);
}
printf("GAP,%f,%f ", Spec_Minnimum, Spec_Maximum);
}


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;
for ( x=0, Act_Gap = 0; x<size; x++){ //does tolerance math
Act_Gap = Act_Gap + (nom[x]*SIGNS[x]);
}
for ( x=0, Act_Tollerance = 0; x<size; x++){
Act_Tollerance = Act_Tollerance + (tollerance[x]);
}
for (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" ", Act_Gap); //printing
printf("Actual Gap Tolerance: %.3f" ", Act_Tollerance);
if (Maximum_Gap > Spec_Maximum){
printf("The maximum gap (%.3f") is (Greater) than specified (%.3f") ", Maximum_Gap, Spec_Maximum);
}
if (Maximum_Gap < Spec_Maximum){
printf("The maximum gap (%.3f") is (Less) than specified (%.3f") ", Maximum_Gap, Spec_Maximum);
}
if (Minnimum_Gap > Spec_Minnimum){
printf("The minimum gap (%.3f") is (Greater) than specified (%.3f") ", Minnimum_Gap, Spec_Minnimum);
}
if (Minnimum_Gap < Spec_Minnimum){
printf("The minimum gap (%.3f") is (Less) than specified (%.3f") ", Minnimum_Gap, Spec_Minnimum);
}
}

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);
}