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

OBJECTIVE: SECTIONS: To become familiar with functions, dynamic memory, and stru

ID: 3601614 • Letter: O

Question

OBJECTIVE: SECTIONS: To become familiar with functions, dynamic memory, and structur Note that the above code is slightly different from that in the lecture notes. In some places the braces were moved and some lines merged to minimize the code so it would fit on a single page We also used mattype" as our datatype for the matrix. This will make changing our code casier for the later labs. Run this code and you should get the following output: Gauss-Jordan Elimination with Maximum Pivoting 2. Infinite Series BONUS: Infinite Series of Arcsin(x) 125 10 0 22.19999599999995 01 0-24.59999999999993 001 6.799999 99999998 using nanespace sta define 0N9 3 idetine oontents 112, 4, 6, 9), typedel doubleateype 4.263256414560601-14 1.2789T6924368180-13 7,3, 10,2), (1,2, 5,7} 13162820 The first part is the input matrix; we can see this inside our code where we defined "contents". The second part is the output matrix after the Gauss Jordan elimination. This is somewhat different from the lecture slides as we've increased the output precision to 16 digits. The last portion shows the error when we back substitute the solutions into the original equations. We can see the emor is very small but it certainly isn't zero. This is duc1o the fact that the "double" datatype has limitations and we can see the round off errors start to add up. You can see the Gauss-Jordon procedure fail if you change the "contents" line in your code above to either: idofine contents 13,d, 6,9 6,8,12,18, (1,2,S,7 tcofin contents nattypo b. *t // Add code here to do naximn pivoting ,4, 6,93. 47 , 10,21. {1,2,5,1} Eliniration The first causes a problem since the first two equations are not unique. The second has a zero in the first pivot element so it will too fail Don't forget to switch contents back to the original value before continuing. Modify the GuassJondan" function to include maximum pivoting. Use the "br variable to remember the largest row; you may optionally use the "b" variable to remember the largast value. Write ONLY the code which runs befure the nomalization below ter [1++1: (x13.0 ide im a(k)[1] aljiti) * a(k) 151: -. return 0: cout

Explanation / Answer

#include <stdio.h>

#include <string.h>

#include "phase2.h"

void breakup(char * in, char * p1, char * p2, char * p3)

{

int i = 0;

int j = 0;

  

while (in[i] != ' ' && i < strlen(in) - 1)

{

p1[j] = in[i];

i++;

j++;

}

  

j = 0;

i++;

  

while (in[i] != ' ' && i < strlen(in) - 1)

{

p2[j] = in[i];

i++;

j++;

}

  

j = 0;

i++;

  

while (in[i] != ' ' && i < strlen(in) - 1)

{

p3[j] = in[i];

i++;

j++;

}

  

}

char * inFile;

int main()

{

char input[100] = "";

char part1[10] = "";

char part2[10] = "";

char part3[10] = "";

  

  

printf("%s ", "Welcome to the coding platform. Type "help" for assistance.");

  

do

{

printf("%s", ":: ");

  

fgets(input, 60, stdin);

breakup(input, part1, part2, part3);

  

  

  

if (strcmp(part1, "help") == 0)

{

printf("-------------------------- HELP COMMANDS: load <filename> : call the load function to load the specified file. execute : execute the program that was previously loaded in memory. debug : execute in debug mode. dump start end : call the dump function, passing the values of start and end. Start and end will be hexadecimal values. help : display list of available commands. assemble <filename> : assemble an SIC assembly language program into a load module and store it in a file. directory : list the files stored in the current directory. exit : exits program. -------------------------- ");

}

  

else if (strcmp(part1, "directory") == 0)

{

system("ls");

}

  

else if(strcmp(part1, "load") == 0)

{

if(strcmp(part2,"") == 0)

{

printf("LOAD takes in 1 parameter. ");

}

  

else

{

if( access( part2, F_OK ) != -1 )

{

inFile = part2;

printf("'%s' has been loaded. ", inFile);

}

else

{

printf("File does not exist. ");

}

}

}

  

else if(strcmp(part1, "assemble") == 0)

{

if(strcmp(part2,"") == 0)

{

printf("ASSEMBLE takes in 1 parameter. ");

}

  

else

{

if( access( part2, F_OK ) != -1 )

{

assemble(part2);

}

else

{

printf("File does not exist. ");

}

}

}

  

else if (strcmp(part1, "execute") == 0)

{

printf("Under Construction. ");

}

  

else if (strcmp(part1, "debug") == 0)

{

printf("Under Construction. ");

}

  

else if (strcmp(part1, "dump") == 0)

{

if(strcmp(part2, "start") == 0)

{

if (strcmp(part3, "end") == 0)

printf("Under Construction. ");

else

printf("Takes 2 parameters. ");

}

else

printf("Takes 2 parameters. ");

}

  

else if (strcmp("exit", part1) != 0)

{

printf("Invalid command. ");

}

  

  

if (strcmp(part1, "exit") != 0)

{

memset(input, 0, 10);

memset(part1, 0, 10);

memset(part2, 0, 10);

memset(part3, 0, 10);

}

  

}

  

while (strcmp(part1, "exit") != 0);

printf("%s ", "Exited...");

  

}

--------------------------------------------------------------------------------------
// Phase 2.h

#ifndef phase2_h

#define phase2_h

#include <unistd.h>

#include <stdlib.h>

void read_line(void);

void assemble(char *inFile);

struct optab{

char code[10],objcode[10];

}

myoptab[25] = {

{"ADD", "18"},

{"AND", "58"},

{"COMP", "28"},

{"DIV", "24"},

{"J", "3C"},

{"JEQ", "30"},

{"JGT", "34"},

{"JLT", "38"},

{"JSUB", "48"},

{"LDA", "00"},

{"LDCH", "50"},

{"LDL", "08"},

{"LDX", "20"},

{"MUL", "20"},

{"OR", "44"},

{"RD", "D8"},

{"RSUB", "4C"},

{"STA", "0C"},

{"STCH", "54"},

{"STL", "14"},

{"STX", "10"},

{"SUB", "1C"},

{"TD","E0"},

{"TIX", "2C"},

{"WD", "DC"},

};

struct SYMTAB

{

char symbol[15];

int addr;

}

mysymtab[20];

int startaddr;

int locctr;

int symcount=0;

int length;

char line[20],label[8],opcode[8],operand[8],programname[10];

int dupsym=0;

int nostart=0;

int noend=0;

int manybyte=0;

int noop=0;

int validop=0;

int memover=0;

void pass_one(char *inFile)

{

int sym_there = 0;

int sym_tracker = 0;

int op_there = 0;

  

FILE *input,*inter;

input=fopen(inFile,"r");

inter=fopen("inter.txt","w");

  

fgets(line,20,input);

read_line();

  

if (strcmp(opcode, "START") == 0)

{

startaddr=atoi(operand);

locctr=startaddr;

strcpy(programname,label);

fprintf(inter,"%d %s",locctr, line);

fgets(line,20,input);

read_line();

}

else

{

programname[0]='';

startaddr=0;

locctr=0;

nostart = 1;

}

  

while (strcmp(opcode,"END") != 0 )

{

if (strcmp(&line[0],".") != 0)

{

if (strcmp(label,"/0") != 0)

{

if (strcmp(mysymtab[0].symbol, "") == 0)

{

//DO NOTHING

}

if (strcmp(mysymtab[0].symbol, label) == 0)

{

sym_there = 1;

}

else

{

for (int i = 0; strcmp(mysymtab[i].symbol, "") != 0; i++)

{

if (strcmp(label,mysymtab[i+1].symbol) == 0)

{

sym_there = 1;

break;

}

}

}

if (sym_there == 1)

{

dupsym = 1;

sym_there = 0;

}

else

{

for (int i = 0; i < 20; i++)

{

if (strcmp(mysymtab[i].symbol, "") != 0)

{

sym_tracker++;

}

}

memcpy(mysymtab[sym_tracker].symbol, label, 10);

mysymtab[sym_tracker].addr = locctr;

sym_tracker = 0;

}

  

for (int i = 1; i < 25; i++)

{

if (strcmp(myoptab[i].code, opcode) == 0)

{

op_there = 1;

break;

}

}

  

if(op_there == 1)

{

locctr += 3;

op_there = 0;

}

else if (strcmp(opcode, "WORD") == 0)

{

locctr += 3;

}

else if (strcmp(opcode, "RESW") == 0)

{

locctr += atoi(operand)*3;

}

else if (strcmp(opcode, "RESB") == 0)

{

locctr += atoi(operand);

}

else if (strcmp(opcode, "BYTE") == 0)

{

int j = 0;

for (int i = 2; strcmp(&operand[i], "") != 0; i++)

{

if (strcmp(&operand[i], "'") != 0)

{

j++;

}

}

locctr += j;

}

else

{

noop = 1;

}

}

}

fprintf(inter, "%s %d ", line, locctr);

fgets(line,20,input);

read_line();

}

fprintf(inter, "%s %d ", line, locctr);

length = locctr - startaddr;

  

fprintf(inter," Symbol Table ");

fprintf(inter,"_____________________________________ ");

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

{

if (strcmp(mysymtab[i].symbol, "") != 0)

fprintf(inter, "%s %d", mysymtab[i].symbol, mysymtab[i].addr);

fprintf(inter, " ");

}

/*

fprintf(inter, " Op Table ");

fprintf(inter, "_____________________________________ ");

for (int i = 0; strcmp(myoptab[i].code,"") != 0; i++)

{

fprintf(inter, "%s", myoptab[i].code);

fprintf(inter, " ");

}

   */

  

fclose(inter);

fclose(input);

}

void read_line()

{

char buff[8],word1[8],word2[8],word3[8];

int i,j=0,count=0;

label[0]=opcode[0]=operand[0]=word1[0]=word2[0]=word3[0]='';

  

for(i=0;line[i]!='';i++)

{

if(line[i]!=' ')

buff[j++]=line[i];

else

{

buff[j]='';

strcpy(word3,word2);

strcpy(word2,word1);

strcpy(word1,buff);

j=0;

count++;

}

}

  

buff[j-1]='';

  

strcpy(word3,word2);

strcpy(word2,word1);

strcpy(word1,buff);

  

switch(count)

{

case 0:

strcpy(opcode,word1);

break;

case 1:

{

strcpy(opcode,word2);

strcpy(operand,word1);

  

}

break;

case 2:{

strcpy(label,word3);

strcpy(opcode,word2);

strcpy(operand,word1);

  

}

break;

}

}

void assemble(char *inFile)

{

pass_one(inFile);

}