Help with adjacency matrix and scanf

This is a discussion on Help with adjacency matrix and scanf within the C Programming forums, part of the General Programming Boards category; Hi guys I have been trying to teach myself C for about 2 weeks now. To get more practice I ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    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
    Thx in advance

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote 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
    Thx in advance
    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?

    Think about it!!!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    well could you just ignore the second part and explain to me the best way to use scanf to get the input?

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote 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
    Last edited by Micko; 03-31-2005 at 01:02 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    Thx alot Hammer, I understand what I'm doing now that have an example!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program - inverse of a matrix
    By chaugh in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 11:00 PM
  2. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 11:58 PM
  3. Scanf ruining my problem!
    By AmbliKai in forum C Programming
    Replies: 21
    Last Post: 11-01-2007, 08:09 AM
  4. [C] Best way to save matrix counters
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 03-24-2006, 05:02 PM
  5. Matrix Mulitplication
    By mr_spanky202 in forum C Programming
    Replies: 5
    Last Post: 04-06-2003, 12:22 AM

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