Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
This is<pre>#include "bits/stdc++.h"
using namespace std;
 
vector<int> *adjlist, *revlist;
bool *vis;
stack<int> S;
string scc="";
 
void addedges(vector<int> *(&v), int x, int y)
{
    v[x].push_back(y);
}
 
void dfs_2nd(int r)
{
    vis[r]=true;
    scc+=to_string(r);
    for(auto x:revlist[r])
    {
        if(!vis[x])
        {
            dfs_2nd(x);
        }
    }
    scc+=" ";
}
 
void dfs(int r)
{
    vis[r] = true;
 
    for (auto x : adjlist[r])
    {
        if (!vis[x])
        {
            dfs(x);
        }
    }
    S.push(r);
}
 
int main()
{
    //-------------------------TOPOLOGICAL SORT---------------------
    puts("------TOPOLOGICAL SORT------");
    int node, edge;
    cout << "Number of vertices: ";
    cin >> node;
    cout << "Number of edges: ";
    cin >> edge;
    adjlist = new vector<int>[node + 1]();
    vis = new bool[node + 1];
    int p, q;
    for (int i = 0; i < edge; i++)
    {
        cin >> p >> q;
        addedges(adjlist, p, q);
    }
 
    int root;
    cout << "Enter root vertex: ";
    cin >> root;
 
    for (int i = 1; i < node + 1; i++)
    {
        if (!vis[i])
        {
            dfs(i);
        }
    }
    while (!S.empty())
    {
        int s = S.top();
        cout << s << " ";
        S.pop();
    }
    cout << endl;
    delete [] adjlist;
    delete vis;
    //-----------------------STRONGLY CONNECTED COMPONENT------------------
    puts("------STRONGLY CONNECTED COMPONENT------");
    cout << "Number of vertices: ";
    cin >> node;
    cout << "Number of edges: ";
    cin >> edge;
    adjlist = new vector<int>[node + 1];
    revlist = new vector<int>[node + 1];
    vis = new bool[node + 1];
 
    for (int i = 0; i < edge; i++)
    {
        cin >> p >> q;
        addedges(adjlist, p, q);
        addedges(revlist, q, p);
    }
 
    for (int i = 1; i < node+1; i++)
    {
        if(!vis[i])
        {
            dfs(i);
        }
    }
    memset(vis, false, node+1);
    while(!S.empty())
    {
        int s=S.top();
        if(!vis[s])
            dfs_2nd(s);
        S.pop();
    }
    cout<<"Strongly Connected Components are: "<<scc<<endl;
}
my Code C++ for my assignment. Please help me to convert it to C code. Thank you!

What I have tried:

I have tried it. But I can't convert it from c++ to c programming.
Posted
Updated 8-Jun-21 9:13am
Comments
jeron1 8-Jun-21 12:12pm    
Is it urgent?
[no name] 8-Jun-21 12:14pm    
Yes, please..
jeron1 8-Jun-21 12:16pm    
"I have tried it."
What did you try, can you show us your attempt?

Lots of C++ things in there. I see string, vector, stack, cin, cout, new, delete, and delete[]. You need to understand what these do and find C equivalents for them (or create your own, if need be). Good luck.
 
Share this answer
 
v2
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And to be honest, if you had written that C++ code, then you would know how much work "converting it to C" would involve - C++ is a superset of C and the main features that code relies upon aren't available at all in C ...

Write your own code for this in C: that way you get something you can actually hand in that should meet your homework assignment.

Oh, and by the way, Akter Hosen Atik: you do realize that your teacher is well aware of sites like this and will probably be looking for plagiarism. So your name is now firmly linked with trying to cheat ... Writing your own code and being able to prove that you did could be ... um ... fairly important in your near future. Just a thought.
 
Share this answer
 
My best guess is that it isnt your C++ code, because you dont understand it. But I will give you some hints.
As my pre-writers wrote you need to replace the C++ with C function. So visit some Learn C tutorial to learn the language and especially read carefully all details about the printf(), scanf(), malloc() and free() functions.

With this knowledge you can kickstart your coding task.
 
Share this answer
 
That's not "your code". That's code you lifted off the web somewhere. Since it has to be written in C, you think you're saving yourself work by just converting this code to C and you'll be all set.

W R O N G

You actually made more work for yourself. Now instead of writing the code from scratch, you now have to learn C++ to understand what the code does, then you have to write new code from scratch.

In either case, if you write the C code from scratch, or "convert" the code, YOU are the one who is going to have to do the work.

If you're going to get the grade for it, or get paid for it, YOU are the one has to do the work. Nobody else is going to do it for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900