Matthew C.
asked 11/20/21How do I get my C program to display the correct output? My program either doesn't compile or shows a bunch of zeros. I am unable to figure out what is wrong with my code.
*I am unable to post the code that I have written because it's over 10,000 characters. I'll show my code in snippets using comments under this post.
Description
Write a program that does 2 things: calculate velocity vectors and durations for an industrial robot to move its welding attachment between a set of weld points and calculating how long the entire set of movements will take.
Requirements
- Read in the set of points from a file named points.txt (I provided that file in the Programming Assignments module on Canvas)
- Calculate and save a velocity vector and a duration for moving from each point to the next point
- Write the velocity vectors and durations to an output file named moveinfo.txt
- Print out the total duration of all the moves required
Structs and Functions
void getPointFromString(char string[], Vector *point);
The getPointFromString function processes the comma-separated x, y, and z values in the given string and populates the x, y, and z fields in the point parameter.
void getMoveInfoBetweenPoints(MoveInfo *moveInfo,Vector firstPoint, Vector secondPoint);
The getMoveInfoBetweenPoints function calculates the direction vector and the duration for a move from firstPoint to secondPoint and populates the direction and duration fields in the moveInfo parameter.
void writeMoveInfoToFile(MoveInfo moveInfo[], int count);
The writeMoveInfoToFile function writes the first count elements of the moveInfo array to the move info file.
Additional details
The welding attachment on the robot moves at exactly 1 foot per second.
An example line from the input file:
2.267537,3.647459,-2.233978
The 3 values are the x, y, and z coordinates (in feet) of the point.
An example line from the required output file is:
0.4684,0.7534,-0.4615,4.8411
The first 3 values are the x, y, and z components of the velocity vector (in feet per second) and the final value is the duration in seconds the welding attachment will move along that velocity vector. All values MUST be to 4 decimal places. The total duration you output (to the console, not to the file) should be output to 4 decimal places. The total duration should be in seconds.
1 Expert Answer
I'm answering your question via comments in your original code. Please let me know if anything is unclear. These are my comments to main().

Sid M.
02/11/22

Sid M.
02/11/22
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Matthew C.
#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #define MAX_LENGTH 100 typedef struct Vector Vector; struct Vector { float x; float y; float z; }; typedef struct MoveInfo MoveInfo; struct MoveInfo { Vector direction; float duration; }; // functions used void getPointFromString(char string[], Vector *point); void getMoveInfoBetweenPoints(MoveInfo *moveInfo, Vector firstPoint, Vector secondPoint); void writeMoveInfoToFile(MoveInfo moveInfo[], int count); // automated grader use only void printFileContents(char* fileName, int numLines); /* * Programming Assignment 5 */ int main(int argc, char** argv) { // create variables MoveInfo moveInfo[MAX_LENGTH]; char currentLine[100]; int numLines = 0; Vector temp[MAX_LENGTH]; // open points.txt file for reading FILE *pointsFile = fopen("points.txt", "r"); if (pointsFile == NULL) { printf("Unable to open file!\n"); return (EXIT_FAILURE); } // count the lines while (!feof(pointsFile)) { fgets(currentLine, sizeof(currentLine), pointsFile); // call getPointFromString function getPointFromString(currentLine, &temp[numLines]); numLines++; } // rewind points.txt file fclose(pointsFile); fopen("points.txt", "r"); // call getMoveInfoBetweenPoints function getMoveInfoBetweenPoints(&moveInfo[0], temp[0], temp[1]); getMoveInfoBetweenPoints(&moveInfo[1], temp[1], temp[2]); // write info to file for (int i = 0; i < MAX_LENGTH; i++) { // call writeMoveInfoToFile function writeMoveInfoToFile(&moveInfo[i], numLines); } // automated grader use only printFileContents("moveinfo.txt", numLines - 1); return(EXIT_SUCCESS); } // main // functions // process x, y, and z values void getPointFromString(char string[], Vector* point) { int comma = -1; // find the first comma char* findComma = NULL; findComma = strchr(string, ','); char* stringArr = &string[0]; comma = findComma - stringArr; // get string x string = &string[0] + comma + 1; point -> x = atof(string); comma = findComma - string[0] + 1; char* stringX = malloc(sizeof(char) * comma); // find the second comma findComma = strchr(string, ','); stringArr = &string[0]; comma = findComma - stringArr; // get string y string = &string[0] + comma + 1; point -> y = atof(string); comma = findComma - string[0] + 1; char* stringY = malloc(sizeof(char) * comma); // get string z string = &string[0] + comma + 1; point -> z = atof(string); } // getPointFromString // calculate direction vector and duration void getMoveInfoBetweenPoints(MoveInfo* moveInfo, Vector firstPoint, Vector secondPoint) { // calculate delta x, y, and z float deltaX = secondPoint.x - firstPoint.x; float deltaY = secondPoint.y - firstPoint.y; float deltaZ = secondPoint.z - firstPoint.z; float length = sqrtf((deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ)); moveInfo -> duration = length; // calculate direction vector moveInfo -> direction.x = length; moveInfo -> direction.y = length; moveInfo -> direction.z = length; } // getMoveInfoBetweenPoints // write moveinfo.txt file void writeMoveInfoToFile(MoveInfo moveInfo[], int count) { // open file FILE* fp = fopen("moveinfo.txt", "w"); // write file for (int i = 0; i < count; i++) { fprintf(fp, "%.4f,", moveInfo[i].direction.x); fprintf(fp, "%.4f,", moveInfo[i].direction.y); fprintf(fp, "%.4f,", moveInfo[i].direction.z); fprintf(fp, "%.4f,\n", moveInfo[i].duration); } // close file fclose(fp); } // writeMoveInfoToFile // Don't add or modify any code below this comment /* * Prints the first numLines of the given file */ void printFileContents(char* fileName, int numLines) { char line[100]; // open file for reading FILE* inputFile = fopen(fileName, "r"); if (inputFile == NULL) { printf("Unable to open file!\n"); exit (EXIT_FAILURE); } // print lines in file for (int i = 0; i < numLines; i++) { fgets(line, sizeof(line), inputFile); printf("%s", line); } // close file fclose(inputFile); }11/20/21