trying to implement graph(data structure)

This is a discussion on trying to implement graph(data structure) within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> struct node { struct node *edge[10]; int wt[10],id; } ver[10]; int makegraph() { int i=0,j=0,nov=0; char ch; printf("Enter ...

  1. #1
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    trying to implement graph(data structure)

    Code:
    #include<stdio.h>
    
    struct node
    {
    
    	struct node *edge[10];
    	int wt[10],id;
    } ver[10];
    
    int makegraph()
    {
    	int i=0,j=0,nov=0;
    	char ch;
    	printf("Enter number of vertices in the graph");
    	scanf("%d",&nov);
    	for(i=0;i<nov;i++)
    	{
    		ver[i].id=i;
    		for(j=0;j<nov;j++)
    		{
    			printf("Is there any edge between vertex %d and %d(y/n)\n",i,j);
    			scanf("%c",&ch);
    			if(ch=='y')
    			{
    				ver[i].edge[j]=&ver[j];
    				printf("Enter the weight of the edge between %d and %d");
    				scanf("%d",&ver[i].wt[j]);
    			}
    		}
    	}
    	return nov;
    }
    
    void showgraph(int nov)
    {
    	int i,j;
    	for(i=0;i<nov;i++)
    	{
    		printf("ver[%d]:-\nEdges:-",i);			
    		for(j=0;j<nov;j++)
    		{
    			printf("(%d,%d),",ver[i].edge[j]->id,ver[i].wt[j]);
    		}
    	}
    }
    
    int main()
    {
    	showgraph(makegraph());
    	return 0;
    }
    it compiles in gcc 4.3 but when i run the program it skips some of the scanf statements, for example

    after it asks enter the number of vertices
    it asks
    "Is there any edge between vertex 0 and 0(y/n)"
    but does not let me give any input it simply skips to the next question
    "Is there any edge between vertex 0 and 1(y/n)"
    and waits for my input

    why is this happening? how to fix this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    enter-key is a character, just like y and n. If you want your scanf statements to not "see" whitespace, you have to have the format skip whitespace, as in " %c" (instead of just "%c").

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 06:46 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21