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

I\'m completely lost with this C++ assignment; we are supposed to give the outpu

ID: 3938838 • Letter: I

Question

 I'm completely lost with this C++ assignment; we are supposed to give the output of the following program as well as giving the values of all the requested variables. Suppose my ID number is 2766753.   #include<stdio.h> #include<string.h>  void createFunction(FILE *file, char *name, char *parameters, char *returnType, int *indentLevel) {   int i, len = strlen(parameters);   (*indentLevel)++;   fprintf(file, "%s %s(%s) { ", returnType, name, parameters); }  void endBlock(FILE *file, int *indentLevel) {   int i;   (*indentLevel)--;   for (i = 0; i < *indentLevel; i++)   {     fprintf(file,"  ");   }      fprintf(file, "}  "); }  void AddLine(FILE *file, char *line, int indentLevel) {   int i;   for (i = 0; i < indentLevel; i++)   {     fprintf(file,"  ");   }   fprintf(file,"%s ", line); }  void createForLoop(FILE *file, char* loop,int initial, int conditionValue, char* check, int increment, int *indentLevel) {   int i;   for (i = 0; i < *indentLevel; i++)   {     fprintf(file,"  ");   }   fprintf(file, "for (%s = %d; %s %s %d; %s += %d) ", loop, initial, loop, check, conditionValue, loop, increment);   for (i = 0; i < *indentLevel; i++)   {     fprintf(file,"  ");   }   fprintf(file,"{ ");   (*indentLevel)++; }  int main() {   char buf[1000];   int indentLevel = 0;   char student_id[8];   FILE *output;      printf("Enter your studentID ");   scanf("%s", buf);      if (strlen(buf)!= 7)   {     printf("not a valid student id ");   }   strcpy(student_id, buf);   sprintf(buf, "quiz10_%s.txt", student_id);   output = fopen(buf, "w");      fprintf(output, "#include<stdio.h>  ");   createFunction(output, "func1", "int a, int b, int c", "void", &indentLevel);      sprintf(buf, "//Track all values of a, b, and c ");   AddLine(output, buf, indentLevel);   sprintf(buf, "printf("%%d %%d %%d ",a,b,c);");   AddLine(output, buf, indentLevel);   sprintf(buf, "a = %c + b;", (student_id[6] % 3) + 'a' );   AddLine(output, buf, indentLevel);   sprintf(buf, "printf("%%d %%d %%d ",a,b,c);");   AddLine(output, buf, indentLevel);   endBlock(output, &indentLevel);      createFunction(output, "func2", "int x", "int", &indentLevel);   sprintf(buf, "//Track all values of x ");   AddLine(output, buf, indentLevel);   AddLine(output, "printf("%d ",x);",indentLevel);   //(FILE *file, char* loop,int initial, int conditionValue, char* check, int increment, int *indentLevel)   createForLoop(output, "x", 0 + (student_id[6] - '0'), 5 + (student_id[6] - '0'), "<", 1,&indentLevel);   sprintf(buf, "printf("%%d", x + %d);", student_id[6] - '0' + student_id[5] - '0');   AddLine(output, buf, indentLevel);   endBlock(output, &indentLevel);   AddLine(output, "return(x);", indentLevel);   endBlock(output, &indentLevel);      createFunction(output, "func3", "int x", "int", &indentLevel);   sprintf(buf, "x = x + %d;", student_id[5]);   AddLine(output, buf, indentLevel);   AddLine(output, "return(x);", indentLevel);   endBlock(output, &indentLevel);            createFunction(output, "main", "", "int", &indentLevel);   AddLine(output, "int a,b,c;",indentLevel);   AddLine(output, "//Track each array index value", indentLevel);   AddLine(output, "int arr[5];", indentLevel);   sprintf(buf, "a = %d;", student_id[6] - '0');   AddLine(output, buf, indentLevel);      sprintf(buf, "b = %d;", student_id[5] - '0');   AddLine(output, buf, indentLevel);      sprintf(buf, "c = %d;", student_id[4] - '0');   AddLine(output, buf, indentLevel);         sprintf(buf, "func1(%d, %d, %d);", student_id[3] - '0', student_id[2] - '0', student_id[1] - '0');   AddLine(output, buf, indentLevel);      sprintf(buf, "func2(%c);", student_id[4] % 2 == 0 ? 'b' : 'c');   AddLine(output, buf, indentLevel);         sprintf(buf, "%c = func2(%c);", student_id[6] % 2 == 0 ? 'a' : 'b', student_id[5] % 2 == 0 ? 'b' : 'c');   AddLine(output, buf, indentLevel);      //(FILE *file, char* loop,int initial, int conditionValue, char* check, int increment, int *indentLevel)         createForLoop(output, "c", 0,5, "<", 1, &indentLevel);   AddLine(output, "arr[c] = c + 2;", indentLevel);   endBlock(output,&indentLevel);        char var = 'a' + (student_id[4] + student_id[5] +  student_id[6]) % 3;   sprintf(buf, "%c", var);   createForLoop(output, buf, 11 + (student_id[1] - '0') +  (student_id[3] - '0'), (student_id[6] - '0'), ">", -1,&indentLevel);   sprintf(buf, "arr[%c + func3(a)] = a + b + c;", var);   AddLine(output, buf, indentLevel);   sprintf(buf, "printf("%%d ", %c);", var);   AddLine(output, buf, indentLevel);   endBlock(output,&indentLevel);         createForLoop(output, "b", 0,5, "<", 1, &indentLevel);   AddLine(output, "printf("%d ", arr[b]);", indentLevel);   endBlock(output,&indentLevel);         AddLine(output, "return(0);", indentLevel);   endBlock(output, &indentLevel);         fclose(output);   return(0); } 

Explanation / Answer

Hi there seems to be problem with your fprintf which expects string to be null terminated in order to copy it.

Since you're doing fprintf multiple times, I would suggest you to keep large char arrray to store everything or C++ string and at last print it into the file. That would be easier because there's no workaround of fprintf printing non-null terminating strings and it'll give runtime error always.
thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote