Error in converting argument from 'const_Ty' to vector<int[]> - c++

I have to develop a program which checks if some undirected graph is bipartite in C++ as a project exam. I have created all necessary functions but I faced issue I do not clearly understand.
I have some custom struct:
struct slistEl
{
slistEl* next;
int v;
};
Then I have all logic which checks if the graph previously taken from the .txt file is bipartite:
bool isBipartite(int n, slistEl** A)
{
queue Q;
int* C;
int v, u, i;
slistEl* p;
C = new int[n];
for (i = 0; i < n; i++) C[i] = 0;
for (i = 0; i < n; i++)
if (!C[i])
{
C[i] = 1;
Q.push(i);
while (!Q.empty())
{
v = Q.front();
Q.pop();
for (p = A[v]; p; p = p->next)
{
u = p->v;
if (C[u] == C[v])
{
delete[] C;
return false;
}
if (!C[u])
{
C[u] = -C[v];
Q.push(u);
}
}
}
}
delete[] C;
return true;
}
void continueWork(vector<int[2]> verticles) {
slistEl* p, * r, ** A;
A = new slistEl * [getVerticlesCount(verticles)];
for (int i = 0; i < getVerticlesCount(verticles); i++) A[i] = NULL;
for (int i = 0; i < verticles.size(); i++)
{
slistEl v1;
v1.v = verticles[i][0];
slistEl v2;
v2.v = verticles[i][1];
p = new slistEl;
p->v = v2.v;
p->next = A[v1.v];
A[v1.v] = p;
p = new slistEl;
p->v = v1.v;
p->next = A[v2.v];
A[v2.v] = p;
}
}
Function getVerticalCounts has no impact for this issue- it counts verticals from the input file.
Then, I receive following issue which I tried to resolve like here in this issue but It did not help me in any way: Error in converting argument from 'const_Ty' to const custom struct
Errors I receive:
'==': no conversion from 'const _Ty' to 'int [2]'
an array cannot be initialized with a parenthesized initializer
Could someone explain to me specifically what this problem is and potentially how to fix it? I would be so grateful.

Related

How to use a queue to count the number of graph components?

I have the queue functions and I have to use them to count the number of graph components. These are my functions:
struct TQueue {
int value;
TQueue * next;
};
void QueueInit(TQueue * & a_head, TQueue * & a_tail) {
a_head = NULL;
a_tail = NULL;
};
bool IsQueueEmpty(TQueue * a_head) {
return !a_head;
};
void Enqueue(TQueue * & a_head, TQueue * & a_tail, int a_val) {
TQueue * l_hlp = new TQueue;
l_hlp->value = a_val;
l_hlp->next = NULL;
if (a_tail) a_tail->next = l_hlp;
if (!a_head) a_head = l_hlp;
a_tail = l_hlp;
}
int Dequeue(TQueue * & a_head, TQueue * & a_tail) {
int l_val = a_head->value;
TQueue * l_hlp = a_head;
a_head = a_head->next;
if (!a_head) a_tail = NULL;
delete l_hlp;
return l_val;
};
void EmptyQueue(TQueue * & a_head, TQueue * & a_tail) {
TQueue * l_hlp;
while (a_head) {
l_hlp = a_head;
a_head = a_head->next;
delete l_hlp;
}
a_tail = NULL;
};
And this is my main() function and some variables:
// constant - number of vertexes
const int n = 9;
// queue declaration
TQueue *phead, *ptail;
QueueInit(phead, ptail);
// array of distanc
int dist[n];
// distanc array initialization
for (int i = 0; i < n; i++) dist[i] = -1;
// current distance
int d = 0;
// processingvertex
int v;
// number of components
int c = 0;
I prefer to use stack and DFS but it's a school projec so I must to use queue and BFS algorithm.
The approach is same as you would do in a dfs based approach. Here is a general bfs implementation using queue which you can modify easily for your purpose(i.e.using a custom queue)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void bfs(int src, vector<bool>& vis, vector<vector<int>>& adj) {
queue<int> q;
q.push(src);
vis[src] = true;
while(!q.empty()) {
int u = q.front(); q.pop();
vis[u] = true;
for(int v : adj[u]) {
if(!vis[v]) q.push(v);
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int count = 0;
vector<bool> vis(n);
for(int i = 0; i < n; i++) {
if(!vis[i]) {
bfs(i, vis, adj);
count++;
}
}
cout << count << '\n';
}
Variables reference
n : number of nodes in graph
m : number of edges in graph
adj : adjacency list representation of graph
vis : array to store if node is visited or not
count : number of connected components in graph
Example Test Case
Input:
6 3
1 5
0 2
2 4
Output:
3
You can refer to this post on mathematics stack exchange for understanding the algorithm: Finding connected components in a graph using BFS
This is my attempt to code compute components and assign vertices to components:
for (int i = 0; i <= n - 1; i++) visited[i] = false;
c = 0;
while (1) {
c++;
all = true;
for (int i = 0; i < n; i++)
if (!visited[i]) {
all = false;
visited[i] = c;
fronta::Enqueue(phead, ptail, i);
break;
}
if (all) {
c--;
break;
}
fronta::Enqueue(phead, ptail, 0);
visited[0] = true;
while (!fronta::IsQueueEmpty(phead)) {
v = fronta::Dequeue(phead, ptail);
//cout << "processing vertex: " << v << endl;
for (int j = 0; j < n; j++) {
if (gr3[v][j] && !visited[j]) {
fronta::Enqueue(phead, ptail, j);
visited[j] = true;
}
}
}
}
But it doesn't work very well...

How do I write a moving Average to an array list class?

I'm trying to make an arraylist class where I make a moving Average function in my arraylist class while outputting the moving average of my arraylist class.
I've tried various research and examples online and I've officially hit a wall. Can someone please help me fix my problem. I really need to get this fixed as soon as possible. Code was provided by my professor.
#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include<iostream>
using namespace std;
class arrayList {
int size;
int capacity;
double * p;
void resize() {
capacity *= 2;
double * temp = new double[capacity];
for (int i = 0; i < size; i++) {
temp[i] = p[i];
}
delete[] p;
p = temp;
}
}
public:
arrayList(): size(0), capacity(1) {
p = new double[capacity];
}
arrayList(int cap): size(0), capacity(cap) {
p = new double[capacity];
}
//copy constructor
arrayList(const arrayList & copy) {
size = copy.size;
capacity = copy.capacity;
p = new double[capacity];
for (int i = 0; i < size; ++i)
p[i] = copy.p[i];
}
//move constructor
arrayList(arrayList && move) {
size = move.size;
capacity = move.capacity;
p = move.p;
move.size = 0;
move.capacity = 0;
move.p = nullptr;
}
//copy assignment operator
arrayList & operator = (const arrayList & copyA) {
if (this != & copyA) {
size = copyA.size;
capacity = copyA.capacity;
p = new double[capacity];
for (int i = 0; i < copyA.size; ++i)
p[i] = copyA.p[i];
delete[] p;
}
return *this;
}
// move assignment operator
arrayList & operator = (arrayList moveA) {
if (this != & moveA) {
size = moveA.size;
capacity = moveA.capacity;
delete[] p;
p = moveA.p;
moveA.p = nullptr;
}
return *this;
}
//destructor
~arrayList() {
delete[] p;
}
void insert(int index, int value) {
if (index >= capacity) {
cout << "OUT OF BOUNDS!";
}
if (index < size && index >= 0) {
for (int i = size; i > index; --i) {
p[i] = p[i - 1];
}
p[index] = value;
size++;
} else {
p[index] = value;
size++;
}
}
void append(int val) {
if (size == capacity)
resize();
p[size] = val;
size++;
}
void movingAvg(const arrayList & val, int kernel) {
for (int i = 0; i < val.size; ++i) {
kernel = val.p[i];
val.p[i] = kernel[val.size - 1 - i];
kernel[size - 1 - i] = kernel;
cout << "average of the array is: " << val.p;
}
friend ostream & operator << (ostream & os, arrayList & val) {
for (int i = 0; i < val.size; ++i)
os << val.p[i] << " ";
os << endl << endl;
return os;
}
};
// main.cpp
int main() {
arrayList a;
a.append(45);
cout << a;
a.append(14);
cout << a;
a.insert(2, 76);
cout << a;
//CRASHES AT THIS POINT!
a.insert(3, 45);
cout << a;
a.insert(5, 23);
cout << a;
return 0;
}
OUTPUT:
45
45 14
45 14 76 0
You are deleting p more than once in your resize function. That's likely the source of your crash.
Instead of this:
void resize(){
capacity *= 2; //THIS IS WHAT'S CRASHING THE CODE
double *temp = new double[capacity];
for(int i = 0; i < size; i++){
temp[i] = p[i];
delete []p;
p = temp;
temp = nullptr;
}
}
Implement this:
void resize() {
capacity *= 2;
double *temp = new double[capacity];
for (int i = 0; i < size; i++) {
temp[i] = p[i];
}
delete [] p;
p = temp;
}
And if you want to be more efficient with the copy loop:
void resize() {
capacity *= 2;
double *temp = new double[capacity];
memcpy(temp, p, sizeof(double)*size);
delete [] p;
p = temp;
}

Access violation reading location and error reading character of string

Who can explain why access violation reading location erorr is thrown and why in a[] i get "erorr reading characters of string"? I have two strings and must to remove all words from first string that containing other string. What i do wrong?
#include "stdafx.h"
#include<iostream>
#include<cstring>
using namespace std;
char s1[100] = {};
char s2[100] = {};
void Words(char s1[], char s2[]) {
int k = 0;
char*p1 = nullptr;
char*np1 = nullptr;
char*p2 = nullptr;
char*np2 = nullptr;
char *m[20];
char *a[20];
char s3[100] = {};
for (int i = 0; i < 20; i++) {
char n[50] = {};
char l[50] = {};
m[i] = n;
a[i] = l;
}
char delimeter[] = " ,.!?;:";
p2 = strtok_s(s2, delimeter, &np2);
while (p2 != nullptr) {
strcpy(a[k], p2);
k++;
p2 = strtok_s(nullptr, delimeter, &np2);
}
k = 0;
p1 = strtok_s(s1, delimeter, &np1);
while (p1 != nullptr) {
strcpy(m[k], p1);
k++;
p1 = strtok_s(nullptr, delimeter, &np1);
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (strcmp(m[i], a[j]) != 0 && m[i] != 0 && a[j] != 0) {
strcat(s3, m[i]);
}
}
}
puts(s3);
for (int i = 0; i < 20; i++) {
delete m[i];
delete a[i];
}
}
Main function:
int main()
{
gets_s(s1);
gets_s(s2);
Words(s1, s2);
return 0;
}
There is at least one problem here:
for (int i = 0; i < 20; i++) {
char n[50] = {};
char l[50] = {};
m[i] = n;
a[i] = l;
}
After that loop all elements of m and a point to variables that have gone out of scope. The variables n and l cease to exist once the scope between the {} of the for loop has been left.
You have many misconceptions about pointers and you should probably read some good book about the C language (the code you wrote is actually more C than C++).
There are certainly more errors.
There is so much evil in here I don't know where to start.
You're calling delete on a statically allocated array
p1, p2, np1, and np2 are all unallocated and you're writing to them
Please use string:
const regex re{ "([^ ,.!?;:]+)" };
vector<string> s1Tokens{ sregex_token_iterator(cbegin(s1), cend(s1), re, 1), sregex_token_iterator() };
vector<string> s2Tokens{ sregex_token_iterator(cbegin(s2), cend(s2), re, 1), sregex_token_iterator() };
sort(begin(s1Tokens), end(s1Tokens));
sort(begin(s2Tokens), end(s2Tokens));
set_difference(cbegin(s1Tokens), cend(s1Tokens), cbegin(s2Tokens), cend(s2Tokens), ostream_iterator<string>(cout, "\n"));
Live Example
This example could easily be fleshed out further if after understanding the above example you find yourself interested in studying further I'd start here: https://stackoverflow.com/a/38595708/2642059

Error in running an implementation of list

This is the part of the code where I'm facing the problem:
#include<iostream>
using namespace std;
struct ID{
int value;
ID *nxtElement;
};
struct SqLand{
ID *id;
SqLand *next;
};
int main(){
for(int p, i = 0; i < 3; i++){
SqLand *nptr = new SqLand;
cin >> p;
ID *buff = new ID;
buff = nptr->id;
for(int j = 0; j < p; j++){
cin >> buff->value;
//cout << "Garbage";
buff = buff->nxtElement;
}
buff = NULL;
//cout << "WILL";
delete nptr;
delete buff;
}
return 0;
}
The problem is that on running this program and inserting the value of p more than 1, the program exits after 2 more inputs.
For example, starting like this:
2
1
3
This is where the program exits
If both the cout statements are un-commented here are the outputs:
2
1
Garbage3
GarbageWILL
And another:
3
1
Garbage2
Garbage
All the programs exit after their respective last lines. What is the error in my program? It's a part of another program so that don't expect this snippet to make any sense. I only want to know where it goes wrong.
What i can understand from your code this ... (with fix)
struct ID {
ID(int val = -1, ID* _nxtElement = NULL) :
value(val), nxtElement(_nxtElement) {
}
int value;
ID *nxtElement;
};
struct SqLand {
SqLand(ID* _id = NULL, SqLand* _next = NULL) :
id(_id), next(_next) {
}
ID *id;
SqLand *next;
};
const int size = 3;
int main() {
SqLand* head = new SqLand;
SqLand* tmp = head;
for (int p, i = 0; i < size; i++) {
cin >> p;
tmp->id = new ID;
ID* tmpID = tmp->id;
for (int j = 0; j < p; j++) {
cin >> tmpID->value;
// avoid unnecessary allocate
if (j < p - 1) {
tmpID->nxtElement = new ID;
tmpID = tmpID->nxtElement;
}
}
// avoid unnecessary allocate
if(i < size - 1) {
tmp->next = new SqLand;
tmp = tmp->next;
}
}
return 0;
}
in your code you miss allocate for nptr->id and you miss the most important the head of list (SqLand *nptr = new SqLand;).

What is the meaning of `A vec*[5] = new B*[5]`

Array memory Allocation doesn't work
I saw the following code and found that it doesn't compile.
Is the code in the OP correct?
Thank you
class A {
};
class B : public A {
int num;
};
int main() {
/* Original Post
error: expected initializer before ‘*’ token
A vec*[5] = new B*[5];
A vec*[5] = new B*[5]; // <<< I don't understand this line
for(int i = 0; i < 5; i++)
{
vec[i] = new B();
}
*/
// My modified version
A* vec[5];
for(int i = 0; i < 5; i++)
{
vec[i] = new B();
}
return 0;
}