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?