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

Can someone convert this c++ code to C. I am stuck on the vectors and on the whi

ID: 3875296 • Letter: C

Question

Can someone convert this c++ code to C. I am stuck on the vectors and on the while (ss >> x). I have been trying for 5 days to solve this and im hitting a dead end.

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <string>

#include <sstream>

using namespace std;

const int Maxn = 200;

vector<int> d[Maxn];

int par[Maxn];

int rev[Maxn];

bool vs[Maxn];

bool dfs(int i) {

if (i < 0) return true;

if (vs[i]) return false;

vs[i] = true;

for (int u : d[i])

if (dfs(rev[u])) {

par[i] = u;

rev[u] = i;

return true;

}

return false;

}

int main() {

ios_base::sync_with_stdio(false);

int n;

cin >> n;

string s;

getline(cin, s);

for (int i = 0; i < n; i++) {

getline(cin, s);

stringstream ss(s);

vector<int> mk(n, 1);

mk[i] = 0;

int x;

while (ss >> x)

mk[x] = 0;

cout << "value of x is" << x;

for (int x = 0; x < n; x++)

if (mk[x])

d[i].push_back(x);

}

memset(par, -1, sizeof par);

memset(rev, -1, sizeof rev);

for (bool ok = true; ok; ) {

ok = false;

memset(vs, 0, sizeof vs);

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

if (par[i] < 0) {

ok |= dfs(i);

}

}

int ans = 0;

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

ans += (par[i] < 0);

cout << ans;

system("pause");

}

Explanation / Answer

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#define true 1

#define false 0

int Maxn = 200;

int d[200];

int par[200];

int rev[200];

int vs[200];

int dfs(int i)

{

if (i < 0) return true;

if (vs[i]) return false;

vs[i] = true;

for (int u ; d[i];)

if (dfs(rev[u])) {

par[i] = u;

rev[u] = i;

return true;

}

return false;

}

int main() {

/*ios_base::sync_with_stdio(false); */

int n;

scanf("%d", &n);

char *s;

scanf("%s", s );

size_t size;

size = n;

FILE *stream;

stream = open_memstream (&s, &size);

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

{

scanf("%s", s );

fprintf (stream, s);

fflush (stream);

int mk[n];

mk[i] = 0;

int x;

while (x=s[i])

mk[x] = 0;

printf( "value of x is %d", x );

for (int x = 0; x < n; x++)

if (mk[x])

d[i] = x;

}

memset(par, -1, sizeof par);

memset(rev, -1, sizeof rev);

for ( int ok = true; ok; )

{

ok = false;

memset(vs, 0, sizeof vs);

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

if (par[i] < 0) {

ok |= dfs(i);

}

}

int ans = 0;

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

ans += (par[i] < 0);

printf( "%d ", ans);

system("pause");

}