Thread: 2d arrray,pick among a number of values to print randomly

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

    2d arrray,pick among a number of values to print randomly

    hi,
    i want to print Largest number from any 5 rows.Th number printed should be any one of the largest in the five rows of 2d arrays.I have created code for largest number in each row but how to pick and print them randomly?.

    Code:
     #include<conio.h>
     
    main( )
    { 
      int a,b,c,d,e,x;
      int arr[] = {a,b,c,d,e};
      int Matrix[5][5] ={            /*Initializing array*/                
                          2,4,3,5,9,
                          6,8,2,2,10,
                          9,12,345,23,5,
                          45,67,349,234,56,
                          23,34,64,347,6
                        };    
                        
                         
     printf ( "Largest number from any 5 rows is : ") ;
     a= ROW(&Matrix[0][0]);
     b= ROW(&Matrix[1][0]);
     c= ROW(&Matrix[2][0]);
     d= ROW(&Matrix[3][0]);
     e= ROW(&Matrix[4][0]);
     
     x = ;/*<===here i want any value of the array arr[]*/
     printf("\n%d",x);
     }
     
     ROW(int *a)
     {
      int i,j,temp;
      temp = *a;
      for(j=1;j<=4;j++)
       {
        if (temp<*(a+j))
        temp = *(a+j);
       }
      return(temp);
    }
    Last edited by coder1; 09-26-2013 at 08:37 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so why not to scan the whole array? Why only one row?
    If you got 100x100 matrix you would use 100 variables for each line?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I interpret his question as asking for the largest element in a random row.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    Quote Originally Posted by oogabooga View Post
    I interpret his question as asking for the largest element in a random row.
    yes,but i just want to pick any random value of array "arr" and print it.how i do that?

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    simplist idea, get a random number between 1 and 4, then that would be your row number??? then send it to your "largest number in the row" function!!!

    in other words..."how to pick...them randomly"

    i would say learn how to seed then pick a random number! MANY MANY posts on here about that!



    //edit//

    oh pick one at random from entire array...

    two random number picks...

    array[random number 1][random number 2] ?

  6. #6
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    Quote Originally Posted by Crossfire View Post
    simplist idea, get a random number between 1 and 4, then that would be your row number??? then send it to your "largest number in the row" function!!!

    in other words..."how to pick...them randomly"



    i would say learn how to seed then pick a random number! MANY MANY posts on here about that!



    //edit//

    oh pick one at random from entire array...

    two random number picks...

    array[random number 1][random number 2] ?


    Tried that but the answer is always coming 345 (i.e ran() is only giving 2 as a value each time i run it).what is the problem here,why its not working properly?

    Code:
     #include<stdio.h>
     
    main( )
    { 
      int a;
      int Matrix[5][5] ={            /*Initializing array*/                
                          2,4,3,5,9,
                          6,8,2,2,10,
                          9,12,345,23,5,
                          45,67,349,234,56,
                          23,34,64,347,6
                        };    
                        
     
     printf ( "Largest number from any 5 rows is : ") ;
     a= ROW(&Matrix[rand()%4+1][0]);
     
     printf("\n%d",a);
     }
     
     ROW(int *a)
     {
      int i,j,temp;
      temp = *a;
      for(j=1;j<=4;j++)
       {
        if (temp<*(a+j))
        temp = *(a+j);
       }
      return(temp);
    }
    Last edited by coder1; 09-27-2013 at 04:17 AM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to use srand

    Code:
    #include <stdio.h> /* conio.h is not standard */
    #include <stdlib.h> /* you need this for rand */
    #include <time.h>
    
    #define LINE_SIZE 5
    
    int ROW(int *a); /* we need to have prototype before the function call */
    
    int main(void)/* read FAQ */
    {
        int a;
        //int arr[] = {a,b,c,d,e}; /* you are initializing array with not initialized variables - pointless */
        int Matrix[LINE_SIZE][LINE_SIZE] ={            /*Initializing array*/
            2,4,3,5,9,
            6,8,2,2,10,
            9,12,345,23,5,
            45,67,349,234,56,
            23,34,64,347,6
        }; 
    
        srand((unsigned int) time(NULL)); /* so that rand sequence be different for each run */
        
        a= ROW(&Matrix[rand()%LINE_SIZE][0]); /* why to skip first row ? */
    
        printf ( "Largest number from any 5 rows is : %d\n",a);
    
        return 0;
    }
    
    int ROW(int *a)
    {
        int j,temp;
        temp = a[0];
        for(j=1;j<LINE_SIZE;j++)
        {
            if (temp<a[j])
                temp = a[j];
        }
        return temp;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    Quote Originally Posted by vart View Post
    you need to use srand

    Code:
    #include <stdio.h> /* conio.h is not standard */
    #include <stdlib.h> /* you need this for rand */
    #include <time.h>
    
    #define LINE_SIZE 5
    
    int ROW(int *a); /* we need to have prototype before the function call */
    
    int main(void)/* read FAQ */
    {
        int a;
        //int arr[] = {a,b,c,d,e}; /* you are initializing array with not initialized variables - pointless */
        int Matrix[LINE_SIZE][LINE_SIZE] ={            /*Initializing array*/
            2,4,3,5,9,
            6,8,2,2,10,
            9,12,345,23,5,
            45,67,349,234,56,
            23,34,64,347,6
        }; 
    
        srand((unsigned int) time(NULL)); /* so that rand sequence be different for each run */
        
        a= ROW(&Matrix[rand()%LINE_SIZE][0]); /* why to skip first row ? */
    
        printf ( "Largest number from any 5 rows is : %d\n",a);
    
        return 0;
    }
    
    int ROW(int *a)
    {
        int j,temp;
        temp = a[0];
        for(j=1;j<LINE_SIZE;j++)
        {
            if (temp<a[j])
                temp = a[j];
        }
        return temp;
    }

    Thank you, that explains a lot and my program is working now.

    I have one question does that mean rand() will not generate different numbers randomly until srand() is used ?
    Last edited by coder1; 09-28-2013 at 03:31 AM.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    srand will 'seed' the random number generator, often using the system time, to create a different series of numbers - without that rand will give you a random series, but if you are repeatedly using it, it will always be the same.

    You do not have to call srand repeatedly though, once will suffice.

    Here is another thread that asks the same question
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    rand is pseudo-random generator which for the given seed value will produce same "random" sequence of values.

    It is very convenient for testing of programs using rand() - when you encounter a bug with specific seed value - you just use same seed value each time you try to reproduce the bug till you fix it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pick a random number between x and y
    By CMEE in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 01:27 PM
  2. Replies: 15
    Last Post: 05-21-2011, 09:22 AM
  3. pick randomly from array
    By trentboyette in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2010, 09:24 AM
  4. Print Randomly!
    By davewang in forum C Programming
    Replies: 3
    Last Post: 12-09-2008, 09:04 AM
  5. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 07:27 AM