Data Structures in C++, Graphs and breadth-first search **Edited You just gotta
ID: 3838538 • Letter: D
Question
Data Structures in C++, Graphs and breadth-first search
**Edited
You just gotta use my register machine and do the assignment.
Part two asks you to change the commands in the register machine and part one you gotta build a code for adjacency lish graph representation, and using that, implement the BFS algorithm.
-------------------------------------------
Hello there, I'm a computer science student enrolled in C++ data structures class.
I am currently having some trouble doing an assignment for Graphs and breadth-first search
So basically you would need to use my register machine (unless you want to make one yourself), and change it with graphs as the register values.
I desperately need your help!!
----------------------------------------------------------------
Here is the assignment:
--------------------------------------------------------
And code below is my register machine!!
#include
#include
#include
#include
using std::string;
using std::cout;
void store(int arr[], char reg, int val){
arr[reg - 'a'] = val;
}
void print(int arr[], char reg){
cout << arr[reg - 'a'] << " ";
}
// check if it is a register or a immediate value
int isRegister(char reg) {
if(reg=='a' || reg=='b' || reg=='c' || reg=='d')
return 1;
else
return 0;
}
// get the value of register if it is a register
// or immediate value otherwise
int getValue(int arr[], string reg) {
using std::stringstream;
if(isRegister(reg[0])) {
return arr[reg[0] - 'a'];
} else {
int temp;
stringstream convert(reg);
convert >> temp;
return temp;
}
}
void add(int arr[], int val1, int val2, char result){
arr[result - 'a'] = val1 + val2;
}
void sub(int arr[], int val1, int val2, char result){
arr[result - 'a'] = val1 - val2;
}
void mul(int arr[], int val1, int val2, char result){
arr[result - 'a'] = val1 * val2;
}
void cmp(int arr[], int val1, int val2, char result){
arr[result - 'a'] = val1 - val2;
if(arr[result - 'a'] > 0) arr[result - 'a'] = 1;
if(arr[result - 'a'] < 0) arr[result - 'a'] = -1;
}
int main(){
using std::cin;
using std::stringstream;
string input, c;
string command;
int values[4];
string reg[3];
while(true){
cout << "> ";
std::getline(cin, input);
std::istringstream split_string(input);
split_string >> command;
//iss = split_string
// line = input
if(command.compare("store") == 0){
split_string >> reg[0] >> reg[1];
int temp;
stringstream convert(reg[0]);
convert >> temp;
store(values, reg[1][0], temp);
}
else if(command.compare("print") == 0){
split_string >> reg[0];
print(values, reg[0][0]);
}
else if(command.compare("add") == 0){
split_string >> reg[0] >> reg[1] >> reg[2];
add(values, getValue(values, reg[0]), getValue(values, reg[1]), reg[2][0]);
}
else if(command.compare("sub") == 0){
split_string >> reg[0] >> reg[1][0] >> reg[2][0];
sub(values, getValue(values, reg[0]), getValue(values, reg[1]), reg[2][0]);
}
else if(command.compare("mul") == 0){
split_string >> reg[0] >> reg[1] >> reg[2];
mul(values, getValue(values, reg[0]), getValue(values, reg[1]), reg[2][0]);
}
else if(command.compare("cmp") == 0){
split_string >> reg[0] >> reg[1] >> reg[2];
cmp(values, getValue(values, reg[0]), getValue(values, reg[1]), reg[2][0]);
}
else if(command.compare("stop") == 0) break;
else cout << "No command with that name exists ";
}
}
-------------------------------------------------------------
Thank you in advance!!
You're going to reuse your register machine, this time with graphs as the register values. Part 1: Adj. list graphs Write an implementation of the adjacency listgraph representation. You may assume that nodes are integers which start at 0. Using your implementation, implement the breadth-first-search algorithm, as a function which takes a graph, a starting node and a target node, and searches for a path between them. Your BFS function should return the shortest distance from the source node to the target node, or -1 if no path existsExplanation / Answer
# Store input numbers num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.