1. ## Help with adjacency matrix and scanf

Hi guys I have been trying to teach myself C for about 2 weeks now. To get more practice I decided to program my alogorithms class program in C instead of java( I dont't like java).

Here is my question:
I have to input from stdin so my input is
Code:
5
00101
00100
11001
00000
10100
5
01010
10101
01011
10101
01110
so I'm trying to figure how do I wirte my code accept this from standard in.
This is what I have so far: *note*this is not the whole program

Code:
scanf("%d", &nn);
for (i = 0; i < nn; i++)
for (j = 0; j < i; j++)
cc =getchar()
This is some code I found that inputs random 1's and 0's into a matix
Code:
void complete_graph(int nv)
{
int **mat;
int i, j;

mat = initialize_mat();

for (i = 0; i < nv; i++) {
for (j = 0; j < nv; j++)
mat[i][j] = 1;
}
for (i = 0; i < nv; i++)
mat[i][i] = 0;
print_mat(mat);
}
Sorry I wrote so much hope I got my point across

2. Originally Posted by hollywoodcole

so I'm trying to figure how do I wirte my code accept this from standard in.
This is what I have so far: *note*this is not the whole program

Code:
scanf("%d", &nn);
for (i = 0; i < nn; i++)
for (j = 0; j < i; j++)
cc =getchar()
This is some code I found that inputs random 1's and 0's into a matix
Code:
void complete_graph(int nv)
{
int **mat;
int i, j;

mat = initialize_mat();

for (i = 0; i < nv; i++) {
for (j = 0; j < nv; j++)
mat[i][j] = 1;
}
for (i = 0; i < nv; i++)
mat[i][i] = 0;
print_mat(mat);
}
Sorry I wrote so much hope I got my point across
Since you've posted some code and claim that you're trying yourself I'll give you some code for entering values from standard input and store in matrix.

Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nn;
int** mat;
int i, j;

printf("Enter dimension of SQUARED matrix: ");
fflush (stdout);
scanf("%d", &nn);
/* first goes allocation code for the matix*/
mat = malloc(nn*sizeof(int*));/*allocate memory for pointers*/
/*every pointer will point to each row*/
for (i = 0; i < nn; ++i)
mat[i] = malloc(nn*sizeof(int));/*allocate memory for numbers in each row*/
printf("Enter elements row by row.\n");
for (i = 0; i < nn; ++i)
for(j = 0; j < nn; ++j)
scanf("%d", &mat[i][j]);
printf("You entered following matrix\n");

for (i = 0; i < nn; ++i, printf("\n"))
for(j = 0; j < nn; ++j)
printf("%d ", mat[i][j]);

/*proper deallocation with free() is assumed*/

}
Do you really think your second code will produce matrix with random 1 and 0, I don't think so.
And that part mat = initialize_mat() is more than suspicios to me, initialize_mat has no arguements, why nv is not passed? How many elements can be stored in mat after calling initialize_mat?

3. well could you just ignore the second part and explain to me the best way to use scanf to get the input?

4. Originally Posted by hollywoodcole
well could you just ignore the second part and explain to me the best way to use scanf to get the input?
In that case I suggest you to find a good tutorial on basics C.

Choose: here

5. >>best way to use scanf to get the input?
I wouldn't use scanf(). Try these for size:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
http://faq.cprogramming.com/cgi-bin/...&id=1043284385

6. Thx alot Hammer, I understand what I'm doing now that have an example!