I have to read graph inputs (all at once) (an example below):
6 8 //no of vertices, edges
0 1 2 //connectivity from vertex 0 to vertex 1, with weight 2
0 2 1
0 4 2
1 2 4
2 3 5
3 4 5
4 1 2
4 5 5
What is the best way to read inputs dynamically. I have to read all the above content at once. I need to read it dynamically since the number of edges and vertices can change, with a maximun of 10000. The following doesn't work:
int *twod_array;
int N,M; //no of vertices, edges
scanf("%d %d", &N, &M);
twod_array = (int *)malloc(sizeof(int)*N*M); //where N = no of rows, M = no of cols
for(i=0; i < N; i++) {
for(j=0; j < M; j++) {
scanf("%d",&twod_array[i*M +j]);
}
}
for(i=0; i < N; i++) {
for(j=0; j < M; j++) {
if(twod_array[i*M +j] == "\0") {
twod_array[i*M +j] = 0;
}
}
}
Also, is this the best way for graphs in C/C++ or is it better to use struct, since traversals will be done.
&N&Mis invalid. it should be&N,&M2.2d_arrayis invalid as identifier since it cannot start with a number. 3.where N = no of rows, M = no of colsmakes no sense as code. make it a comment. 4.Nwon't affect the number of rows nor cols. 5.Mis number of rows, not cols. 6. the asssignment2d_array[i*M +j] = "\0"make no sence. it should be comparation to'\0'or just nothing.if(twod_array[i*M +j] = "\0")dosent compare...should be==.twod_array[i*M +j] == "\0"makes no sense. What do you want to do by it?