C++ Programming Exercise: Implement a function bool** aListToMatrix(vector<int>
ID: 661392 • Letter: C
Question
C++ Programming Exercise:
Implement a function
bool** aListToMatrix(vector<int> aLists[], int size);
that returns an adjacent matrix equivalent to aLists(adjacent lists)
Here is my solution:
#include<iostream>
#include<vector>
using namespace std;
bool **aListToMatrix(vector<int> aList[], int size)
{
bool** matrix = new bool*[size];
for(int i = 0; i < size; i++)
{
matrix[i] = new bool[size];
}
for(int i = 0; i < size; i++)
{
for(int j = 0; k < (aList+j)->size(); j++)
{
for(int k = 0; k < size; k++)
{
if((aList+i)->at(j) == k)
{
*(*(arr+k)+i) = true;
}
else
{
*(*(arr+k)+i) = false;
}
}
}
}
return matrix;
}
int main()
{
int i, j;
bool **matrix;
vector<int> aList(5);
for(i = 1; i <= 5; i++)
{
aList.push_back(i);
}
matrix = aListToMatrix(&aList,5);
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
cout << " " << matrix[i][j];
}
}
return 0;
}
The code is not working because the line "if((aList+i)->at(j) == k)" is causing a runtime error. Can someone fix this line?
Explanation / Answer
Inside the function in the for loop of statement:
Incorrect statement: for(int j = 0; k < (aList+j)->size(); j++)
Correct statement: for(int j = 0; k < (aList+j)->size; j++)
Now run and you will get the desired output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.