Thread: What is my mistake ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Question What is my mistake ?

    i have coursework and i wrote the program but its not working ! can you help me about this

    Assignment

    Write a program for

    -Creating a graph with n verticles and m edges.
    -Counting the all isolated verticles in the graph

    The Values of n and m are set by user.

    The result should be shown appropriately on the screen of the computer.


    And My Codes
    Code:
    // silinecek void edge_add code error
    #include <iostream.h>
    #include <stdlib.h>
    
    const int v_n=10;
    int i;
    struct dgraph{ 
    	char vertex;
        dgraph *gec;
    }*gr[v_n];
    
    
    int vertex_check(dgraph *gr[v_n], char ver){
    	i=0;
    	while(gr[i]){
    		if(gr[i]->vertex==ver)
    			return 1;
    		i++;
    	}
    	return 0;
    }
    
    void vertex_add(dgraph *gr[v_n], char ver){
    	if(vertex_check(gr,ver)==0){
    		int y=0;
    		while(gr[y] && (y<v_n))
    			y++;
    		gr[y]=new dgraph;
    		gr[y]->vertex=ver;
    		gr[y]->gec=NULL;
    		cout << "\nVertex " << ver << " added.\n";
    	}
    	else
    		cout << "\nError.";
    }
    
    bool edge_check(dgraph *gr[v_n], char ch1, char ch2){
    	if((vertex_check(gr, ch1) && vertex_check(gr, ch2))==1){
    		int say=0;
    		while(gr[say]->vertex!=ch1)
    			say++;
    		dgraph *point=gr[say];
    		while(point->vertex!=ch2 && point->gec!=NULL){
    			point=point->gec;
    			if(point->vertex==ch2)
    				return true;
    		}
    	}
    	return false;
    }
    
    void edge_add(dgraph *gr[v_n], char ch1, char ch2){
    	if(edge_check(gr,ch1,ch2)==true)
    		cout << "\nEdge exists.";
    	else{
    		if(vertex_check(gr,ch1)==0)
    			vertex_add(gr,ch1);
    		if(vertex_check(gr,ch2)==0)
    			vertex_add(gr,ch2);
    		dgraph *p;
    		int gez=0;
    		while(gr[gez]->vertex!=ch1)
    			gez++;
    		p=new dgraph;
    		p->vertex=ch2;
    		p->gec=gr[gez]->gec;
    		gr[gez]->gec=p;
    		cout << "\n\nEdge added: " << ch1 << "->" << ch2 << "\n";
    		}
    }
    
    void yunus(){
    	int a=0;
    	while(gr[a]!=NULL){
    		cout << "\n1\n";
    		a++;
    	}
    }
    
    
    void main(){
    	int choice,vertices,edges,fa;
    	int sayac=0;
    	char vertex_name,edge_name1,edge_name2;
    	cout << "Number of vertices:";
    	cin >> vertices;
    	cout << "\nNumber of edges:";
    	cin >> edges;
    	do{
    		do{
    		cout << "      \n\n[MENU]\n";
    		cout << "1-> Add vertex\n";
    		cout << "2-> Add edge between Vertex 1 & Vertex 2\n";
    		//cout << "3-> List vertices with 3 out-degrees\n";
    		cout << "4-> Exit\n";
    		cout << "    Choice:";
    		cin >> choice;
    	}
    	while(choice<1 && choice>4);
    	switch(choice){
    	case 1:
    		for(fa=0;fa<vertices;fa++){
    			cout << "\nVertex:";
    	    	cin >> vertex_name;
    	    	vertex_add(gr,vertex_name);
    		}
    			break;
    	case 2:
    		for(fa=0;fa<edges;fa++){
    			cout << "\nVertex 1:";
    			cin >> edge_name1;
    			cout << "Vertex 2:";
    			cin >> edge_name2;
    			while(gr[sayac]->vertex!=edge_name1)
    				sayac++;
    			edge_add(&gr[sayac],edge_name1,edge_name2);
    			sayac=0;
    		}
    			break;
    
    	/*case 3:
    		yunus();
    		break;*/
    	case 4: 
    		exit(1);
    	}
    	}
    	while(choice!=4);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would be good if you describe "it's not working" in some better terms, e.g. "When I use choice X, with the following data ... I expect to get ... but I get ...." or "When I do the following steps .... it crashes with ...."

    The .... bits are obviously also necessary to fill in with whatever is the relevant facts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    its working but after the compiling when i start to add edges adn vertices i have an error

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you can't explain to yourself what the problem is, you can't program it. If you can't explain to us what the problem is, you can't expect help. The phrase "have an error" could mean 100 things. Does it crash? Does it give the wrong output?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    1
    hi,
    here the problem is exactly about the function "edge_add". because:
    run the programme. then input 4 vertices : a,b,c,d. after that, begin inputting the edges:
    a->b... ok.
    b->a when you input this egde, you will see "vertex added." on the screen. now, we have 5 vertices in the array: a,b,c,d,a
    that is the problem.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Quote Originally Posted by anathex View Post
    hi,
    here the problem is exactly about the function "edge_add". because:
    run the programme. then input 4 vertices : a,b,c,d. after that, begin inputting the edges:
    a->b... ok.
    b->a when you input this egde, you will see "vertex added." on the screen. now, we have 5 vertices in the array: a,b,c,d,a
    that is the problem.
    Exactly thats a problem ! im sorry that my english is not very good! i cant translate that

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    ???? anyone can help ????

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Dude, what the heck? You waited 7 minutes and posted again. Don't spam. If you spent as much time working on your program as you're probably doing checking this topic, you might have solved it.

    I might check this out later.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main()
    main returns an int, see the FAQ

    > cout << "Number of vertices:";
    > cin >> vertices;
    > cout << "\nNumber of edges:";
    > cin >> edges;
    What were your responses to these questions?
    Anything >= 10 and your code is in trouble.

    > #include <iostream.h>
    > #include <stdlib.h>
    Does your compiler understand this?
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    If not, your compiler is old.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Thanks Salem I will try again with like that

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Erm, my suggested change won't actually fix any bugs in your program. It just tells us whether you're using a fossil, or something more modern with out-of-date programming technique.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where is my mistake following this code..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 10-18-2008, 01:44 PM
  2. Please help! - Who find mistake in the program??
    By nivek in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 01:28 AM
  3. Mistake in Tutorial
    By Happy_Reaper in forum Tech Board
    Replies: 6
    Last Post: 12-07-2006, 03:13 PM
  4. Please assistance, general protection mistake
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 10:45 PM
  5. Whom do you blame for the mistake in Pres. Bush's State of the Union speech?
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-15-2003, 07:03 AM