Thread: don't understand an array in function program

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    don't understand an array in function program

    Code:
    #include<stdio.h>
    void round (int values[5][5]);
    int keep (int num[5][5]);
    
    void main(void){
      int first[5][5];
      int p, q;
    
      for (p = 0; p < 5; ++p)
         for (q = 0; q < 5; ++q)
           first[p][q] = p+q;
      round(first);
    }
    
    void round (int values[5][5]){
      int u, v, t;
      for (u = 0; u < 5; ++u)
         for (v = 0; v < 5; ++v)
           values[u][v] = values[u][v]%3;
     t = keep(values);
     printf("t = %i", t);
    }
    
    int keep (int num[5][5]){
      int s, t, k = 0;
      for (s = 0; s < 5; ++s)
         for (t = 0; t < 5; ++t)
           k += num[s][t];
      return k;
    }
    
    
    i need some help getting this program.  
    it looks like 
      for (p = 0; p < 5; ++p)
         for (q = 0; q < 5; ++q)
           first[p][q] = p+q;
      round(first);
    is putting each array number in the "round function" five times. and "first[p][q] = p+q;" makes each array a number? [0][1] = 1 [2][2] = 4 [3][2] = 5 etc?
    but what's confusing me is in the round function this: values[u][v] = values[u][v]%3; is making each array the number it is modulus to 3??? and then t = the keep function
    which also loops five times just adds the numbers and stores as k and returns it back to what t is. i'm not seeing how this compiles or prints t = 25 at the end at all. could
    someone possibly run me through this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    main calls round just once, not 5 times.

    Extra braces added for clarity.
    Code:
    int /* Yes, really! */ main(void){
      int first[5][5];
      int p, q;
    
      for (p = 0; p < 5; ++p) {
         for (q = 0; q < 5; ++q) {
           first[p][q] = p+q;
        }
      }
      round(first);
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    28
    Hm whydo i keep getting 19

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your program doesn't call rand(), and has no user input.
    It's going to give you the same answer each time you run it.

    Use a debugger to step through the code, so you can see things like
    - when functions are called, and when they return
    - when variables are modified.

    Or you can add printf statements at each end of the function, like
    Code:
    int keep (int num[5][5]){
      int s, t, k = 0;
      printf("Start of keep\n");
      for (s = 0; s < 5; ++s)
         for (t = 0; t < 5; ++t)
           k += num[s][t];
      printf("End of keep, return %d\n", k );
      return k;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM