Hi !
I am newbie c programmer trying to understand some concepts. Here is a program i wrote which connects two pairs on integers in an array
for eg if u enter 2 3 i will link the two nodes
lets say u enter 3 4 next then i will link 3-4 and also 2-4 since 2 is already linked to 3.
PROBLEM :
I am confused over the break in line 33, when i enter a choice n the program should exit the while loop and display the changed array but infact it exits main() ??????????![]()
maybe my concepts on break are wrong can anyone help me on this ????
Here is the code :
Code:#include<stdio.h> /* ******************************************************************************************************** */ /* This program creates new connection pairs b/w two elments and ignores if a connection (direct or indirect) already exists */ /* ******************************************************************************************************** */ #define size 10 #define TRUE 1 int main() { int i, p, q, t, id[size]; /* vairables used for spanning the array */ char c; /* variable used to store the users choice to continue */ for(i = 0; i < size; id[i] = i, printf("%d ", id[i]), i++); /* initialize array to index and print it */ printf("\n"); while(TRUE) { scanf("%d %d", &p, &q); /* read two entries and check if they are connected */ if(id[p] == id[q]) { printf("node %d-%d already exists\n", p, q); /* if they are connected then ask for choice from user */ printf("do you wish to continue : y/n"); fflush(stdin); /* flush newline in input buffer from scanf() */ c = getchar(); if('y' == c) continue; else break; } else /* if not connected, connect them and all elements */ { /* connected to them as well */ for(t = id[p], i = 0; i < size; i++) if(id[i] == t) id[i] = id[q]; printf("%d-%d node created\n", p, q); /* print the new node created and the new array */ for(printf("new arrary is : "),i = 0; i < size; printf("%d ", id[i]), i++); printf("\n"); } } for(printf("new arrary is : "),i = 0; i < size; printf("%d ", id[i]), i++); printf("\n"); return 0; }



LinkBack URL
About LinkBacks



