Code not working Directions Each input le will include on th

Code not working. Directions:


Each input le will include, on the rst line, the number of elements for this relationship family. Each subsequent linewill contain exactly two numbers, representing an implicative relationship from the rst to the second element. (Forprogramming convenience, the elements are described by integers rather than letters: A=0, B=1, C=2, etc.)
For example, the le:

8

0 1

0 2

0 5

1 2

1 3

1 6

2 3

3 5

3 6

5 6

6 7

describes 8 data elements (A, B, C, D, E, F, G and H), with the following implicative relationships:

A B,A C,A F,B C,B D,B G,C D,D F,D G,F G,G H.

For simplication of this problem in all cases < .
For this assignment, you may assume \"friendly\" input, that is, the input data les are guaranteed to be in the formatstated above. You are not required to perform professional-grade error checking on your input items.
Note that your program should output the letter names of the elements, not their numerical equivalents from the inputle.
For this sample input le, you program should output:

element A has connectivity to end vertex H.element F has connectivity to end vertex H.
use stdio and command line re-direction to access your input data les specically, do not explicitly open any les in your program specically, do not pass les names as parameters to your program

More example output


trans_8       8 elements   11 relations  
               element A has connectivity to end vertex H in 3 hops
               element F has connectivity to end vertex H in 2 hops

trans_8_empty   8 elements   7 relations
               element A does not have connectivity to end vertex H
               element F does not have connectivity to end vertex H

trans_26_1   26 elements   51 relations
               element A has connectivity to end vertex Z in 3 hops
               element F has connectivity to end vertex Z in 4 hops
               element K has connectivity to end vertex Z in 3 hops
               element P has connectivity to end vertex Z in 2 hops
               element U has connectivity to end vertex Z in 1 hops
               element Z does not have connectivity to end vertex Z

trans_26_2   26 elements   39 relations
               element A does not have connectivity to end vertex Z
               element F has connectivity to end vertex Z in 2 hops
               element K has connectivity to end vertex Z in 3 hops
               element P has connectivity to end vertex Z in 2 hops
               element U has connectivity to end vertex Z in 2 hops
               element Z does not have connectivity to end vertex Z

trans_26_3   26 elements   60 relations
               element A has connectivity to end vertex Z in 3 hops
               element F has connectivity to end vertex Z in 4 hops
               element K has connectivity to end vertex Z in 3 hops
               element P has connectivity to end vertex Z in 4 hops
               element U does not have connectivity to end vertex Z
               element Z does not have connectivity to end vertex Z

The code I have that\'s wrong:

#include<iostream>
#include <vector>

using namespace std;
class node {
public:
int id;
int out_list[26];
};
node arr[26];
int start, e;
bool search(int i) {
if (i >= 26)
return false;
if (arr[i].out_list[e] == 1) {
return true;
} else {
for (int j = 0; j < 26; j++) {
if (arr[i].out_list[j] == 1) {
return search(j);
}
}

}
return false;
}
int main(int argc, char **argv) {
int num_of_input;
cin >> num_of_input;
int first, second;
for (int i = 0; (cin >> first >> second); i++) {

if (i == 0)
start = first;

e = second;
//cout << first << \" \" << e << \" \" << num_of_input << endl;

arr[first].out_list[second] = 1;

}

for (int i = start; i < e; i = i + 5) {
char s=(char)( start + 65);
char ee=(char) (e + 65);
if (search(i))
cout << \"Element \" << s
<< \" does have connectivity to end vertex \" << ee
<< endl;
else
cout << \"Element \" << s
<< \" does not have connectivity to end vertex \"
<< ee << endl;

}
}

Solution

#include <iostream>
#include <fstream>
#include <vector>


//We will be using Adjacency Matrix to Compute Hops Between Nodes
//We are considering connections to be Directed.

//Input to Program is ./program_name <data_file>

//outer vector represents a matrix
//matrix is square
std::vector<std::vector<int>> multiply(const std::vector<std::vector<int>>& l,
const std::vector<std::vector<int>>& r){
auto N = l.size();

if(N == 0 ){
return l;
}

std::vector<std::vector<int>> result(N, std::vector<int>(N, 0));
typedef std::vector<std::vector<int>>::size_type size_type;

for(size_type i = 0 ; i < N; ++i){
for(size_type j = 0 ;j < N; ++j){
for(size_type k = 0; k < N; ++k){
result[i][j] += l[i][k] * r[k][j];
}
}
}

return std::move(result);
}

std::vector<std::vector<int>> connectivity(const std::vector<std::vector<int>>& iv){
auto C = iv;
auto N = iv.size();
std::vector<std::vector<int>> result(N, std::vector<int>(N, 0));
typedef std::vector<std::vector<int>>::size_type size_type;
  
for(int k = 0; k < N; ++k){
bool flag = false;
for(size_type i = 0 ; i < N; ++i){
for(size_type j = 0 ;j < N; ++j){
if(C[i][j]== 0){
flag = true;
}
else{
if(result[i][j]==0){
result[i][j]= k + 1;
}
}
}
}
if(flag == true){
C = multiply(C, iv);
}
}

return result;
}

void print_result(const std::vector<std::vector<int>>& res){
typedef std::vector<std::vector<int>>::size_type size_type;
auto N = res.size();
for(size_type i = 0 ; i < N; ++i){
for(size_type j = 0 ;j < N; ++j){
if(res[i][j]==0){
std::cout << \"element \" << (char)(i+\'A\') << \" does not have connectivity to end vertex \"
<< (char)(j+\'A\') << std::endl;
}
else{
std::cout << \"element \" << (char)(i+\'A\') << \" has connectivity to end vertex \"
<< (char)(j+\'A\') << \" in \" << res[i][j] << \" hops\" << std::endl;
  
}
}
}
}
  
int main(int argc, char* argv[]){
if(argc!=2){
std::cerr << \"./a.out <data_file>\" << std::endl;
return 0;
}

std::ifstream ifs(argv[1]);
int N;
ifs >> N;

std::vector<std::vector<int>> initial_matrix(N, std::vector<int>(N, 0));

int K = 0;
while(!ifs.eof()){
int P, Q;
ifs >> P;
ifs >> Q;
initial_matrix[P][Q] = 1;
++K;
}

std::cout << argv[1] << \"\\t\" << N << \" elements\" << K << \" relations\" << std::endl;
print_result(connectivity(initial_matrix));
  
}

Code not working. Directions: Each input le will include, on the rst line, the number of elements for this relationship family. Each subsequent linewill contain
Code not working. Directions: Each input le will include, on the rst line, the number of elements for this relationship family. Each subsequent linewill contain
Code not working. Directions: Each input le will include, on the rst line, the number of elements for this relationship family. Each subsequent linewill contain
Code not working. Directions: Each input le will include, on the rst line, the number of elements for this relationship family. Each subsequent linewill contain

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site