Solved programming problem

Find Total Connected Vertex for One Node And Print Them in C++.

Source code

#include <iostream>

using namespace std;

int main()

{

    int graph[100][100];

    char vertex[26];

    int i,j,node,count=0;

    char item;

    ///Create all vertex

    for(i=0; i<26; i++)

    {

        vertex[i]=65+i;

    }

    cout<<"Please Enter Total Vertex Number: ";

    cin>>node;

    ///set all value 0 for each node

    for(i=0; i<node; i++)

    {

        for(j=0; j<node; j++)

        {

            graph[i][j]=0;

        }

    }

    ///input

    cout<<"
Press 1 for yes 0 for No: "<<endl;

    for(i=0; i<node; i++)

    {

        for(j=i+1; j<node; j++)

        {

            cout<<vertex[i]<<"->"<<vertex[j]<<": ";

            cin>>graph[i][j];

            if(graph[i][j]==1) ///if input is 1

            {

                graph[j][i]; /// make the duplicate node 1  like if A->B is 1 make B->A =1

            }

        }

    }

    cout<<"Enter The Vertex Name: "<<endl;

    cin>>item;

    ///find total node the item got and those vertex it is connected with

    cout<<endl<<item<<" is connected with -> ";

    for(i=0; i<node; i++)

    {

        for(j=0; j<node; j++)

        {

            if((graph[i][j]==1 || graph[j][i]==1) && item == vertex[i])

            {

                count++;

                cout<<vertex[j]<< " ";

            }

        }

    }

    if(count==0)

    {

        cout<<" Opps !! This Vertex don't have any vertex connected with."<<endl;

    }

    else

    {

        cout<<"
Total Connected Vertex: "<<count<<endl;

    }

    return 0;

}

Output

Please Enter Total Vertex Number: 4

Press 1 for yes 0 for No:
A->B: 1
A->C: 0
A->D: 1
B->C: 1
B->D: 0
C->D: 1
Enter The Vertex Name:
C

C is connected with -> B D
Total Connected Vertex: 2

 

Join the discussion

Add your comment