random matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keith katthy
    New Member
    • Oct 2007
    • 24

    random matrix

    HELLO ALL.

    How to get a random number from this matrix?

    rand( )%[ ] [ ] =and get the value?

    if i use like this [ ] [ ] = it error, so what must I do?

    [1][2] A[1]=3
    [2][2] A[2]=6
    [3][2] A[3]=11
    [4][2] A[4]=18
    [5][2] A[5]=27
    [6][2] A[6]=38

    [2][2] B[1]=2
    [2][2] B[2]=9
    [2][2] B[3]=28
    [2][2] B[4]=65
    [2][2] B[5]=126
    [2][2] B[6]=217

    [1][3] C[1]=2
    [2][3] C[2]=5
    [3][3] C[3]=10
    [4][3] C[4]=17
    [5][3] C[5]=26
    [6][3] C[6]=37

    [1][4] D[1]=3
    [2][4] D[2]=24
    [3][4] D[3]=81
    [4][4] D[4]=192
    [5][4] D[5]=375
    [6][4] D[6]=648

    [1][5] E[1]=2
    [2][5] E[2]=5
    [3][5] E[3]=10
    [4][5] E[4]=17
    [5][5] E[5]=26
    [6][5] E[6]=37

    [1][6] F[1]=4
    [2][6] F[2]=7
    [3][6] F[3]=12
    [4][6] F[4]=19
    [5][6] F[5]=28
    [6][6] F[6]=39


    3 2 2 3 2 4
    6 9 5 24 5 7
    11 28 10 81 10 12
    18 65 17 192 17 19
    27 126 26 375 26 28
    38 217 37 648 37 39
  • DumRat
    New Member
    • Mar 2007
    • 93

    #2
    If you want to get a random number in the range [low, high), use this function.

    Code:
    int RandInRange(int low, int high)
    {
          return (int)(rand()*(high - low))/(int)(RAND_MAX + 1);
    }
    Of course, you can use float, double or any other type. Or, make it a template class. Remember to use srand() before calling rand(). I guess you'd be able to use this function for your problem.

    Comment

    • keith katthy
      New Member
      • Oct 2007
      • 24

      #3
      [CODE=cpp]#include <iostream>
      #include <conio.h>
      #include <cstdlib>
      #include <math.h>
      #include <stdio.h>
      #include <time.h>
      using namespace std;


      int main()
      {
      void srand ( unsigned int seed );
      int a[6][6] ;
      int i, j;
      int x[10];
      int A[10], B[10], C[10], D[10], E[10], F[10];
      int RandInRange(int low, int high);
      int low,high;
      {
      for(i=1;i<7;i++ )
      for(j=1;j<7;j++ )
      {
      for(i=1;i<7;i++ )
      {
      x[i]=i;
      A[i]=(2+pow(x[i],2));
      B[i]=(1+pow(x[i],3));
      C[i]=(2+pow(x[i],2)-1);
      D[i]=(3*pow(x[i],3));
      E[i]=(1+pow(x[i],2));
      F[i]=(3+pow(x[i],2));
      }
      {
      printf("\n");

      for(i=1;i<7; i++)

      {
      printf("[%d][%d] A[%d]=%d\n", i,j, i, A[i]);
      }
      printf("\n");
      for(i=1;i<7; i++)

      {
      printf("[%d][%d] B[%d]=%d\n", j,2, i, B[i]);

      }
      printf("\n");
      for(i=1;i<7; i++)

      {
      printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);

      }
      printf("\n");
      for(i=1;i<7; i++)

      {
      printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

      }
      printf("\n");
      for(i=1;i<7; i++)

      {
      printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
      }
      printf("\n");
      for(i=1;i<7; i++)

      {
      printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
      }
      }
      }
      }
      for(i=1;i<7;i++ )
      {
      cout<

      printf("%d\t", A[i]);
      printf("%d\t", B[i]);
      printf("%d\t", C[i]);
      printf("%d\t", D[i]);
      printf("%d\t", E[i]);
      printf("%d\t", F[i]);
      }
      _getch();

      printf("\n");

      return (int)(rand()*(h igh - low))/(int)(RAND_MAX + 1);
      srand (1);


      }

      [/CODE] i have done this programming, but how to get the random number, ?
      Last edited by r035198x; Oct 18 '07, 07:15 AM. Reason: added code tags. Please don't forget them next time

      Comment

      • DumRat
        New Member
        • Mar 2007
        • 93

        #4
        You have to take the RandInRange() function outside the main function, right?

        Comment

        • keith katthy
          New Member
          • Oct 2007
          • 24

          #5
          I dont understand. How to put it?

          Comment

          • DumRat
            New Member
            • Mar 2007
            • 93

            #6
            Your functions and global variables have to be declared outside the main function. That's basic.

            Code:
            int A[10];
            .......
            ......
            
            int main()
            {
            //your code
            }

            Comment

            • keith katthy
              New Member
              • Oct 2007
              • 24

              #7
              How to get therandom numebr ..like I want a rand ( )% A [ ] [ ], i have an error if i done like that...an someone help me?

              #include <iostream>
              #include <conio.h>
              #include <cstdlib>
              #include <math.h>
              #include <stdio.h>
              #include <time.h>
              using namespace std;




              void srand ( unsigned int seed );
              int a[10][10] ;
              int i, j;
              int x[10];
              int A[10][10], B[10][10], C[10][10], D[10][10], E[10][10], F[10][10];

              int main()
              {
              {
              for(i=1;i<7;i++ )

              {
              for(i=1;i<7;i++ )
              {
              x[i]=i;
              A[i]=(2+pow(x[i],2));
              B[i]=(1+pow(x[i],3));
              C[i]=(2+pow(x[i],2)-1);
              D[i]=(3*pow(x[i],3));
              E[i]=(1+pow(x[i],2));
              F[i]=(3+pow(x[i],2));
              }
              {
              printf("\n");

              for(i=1;i<7; i++)

              {
              printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
              }
              printf("\n");
              for(i=1;i<7; i++)

              {
              printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);

              }
              printf("\n");
              for(i=1;i<7; i++)

              {
              printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);

              }
              printf("\n");
              for(i=1;i<7; i++)

              {
              printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

              }
              printf("\n");
              for(i=1;i<7; i++)

              {
              printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
              }
              printf("\n");
              for(i=1;i<7; i++)

              {
              printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
              }
              }
              }
              }
              for(i=1;i<7;i++ )
              {
              cout<<endl;

              printf("%d\t", A[i]);
              printf("%d\t", B[i]);
              printf("%d\t", C[i]);
              printf("%d\t", D[i]);
              printf("%d\t", E[i]);
              printf("%d\t", F[i]);
              }
              _getch();

              printf("\n");

              printf("Random number: %d\n", rand() %[10][10]);
              srand ( 1 );

              }

              ompiling...
              120.cpp
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (27) : error C2440: '=' : cannot convert from 'double' to 'int [10]'
              There are no conversions to array types, although there are conversions to references or pointers to arrays
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (28) : error C2440: '=' : cannot convert from 'double' to 'int [10]'
              There are no conversions to array types, although there are conversions to references or pointers to arrays
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (29) : error C2440: '=' : cannot convert from 'double' to 'int [10]'
              There are no conversions to array types, although there are conversions to references or pointers to arrays
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (30) : error C2440: '=' : cannot convert from 'double' to 'int [10]'
              There are no conversions to array types, although there are conversions to references or pointers to arrays
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (31) : error C2440: '=' : cannot convert from 'double' to 'int [10]'
              There are no conversions to array types, although there are conversions to references or pointers to arrays
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (32) : error C2440: '=' : cannot convert from 'double' to 'int [10]'
              There are no conversions to array types, although there are conversions to references or pointers to arrays
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (93) : error C2059: syntax error : '['
              C:\Documents and Settings\Admini strator\Desktop \MASTER\120.cpp (96) : warning C4508: 'main' : function should return a value; 'void' return type assumed
              Error executing cl.exe.

              120.exe - 7 error(s), 1 warning(s)

              Comment

              • Meetee
                Recognized Expert Contributor
                • Dec 2006
                • 928

                #8
                [CODE=CPP]#include <iostream>
                #include <conio.h>
                #include <cstdlib>
                #include <math.h>
                #include <stdio.h>
                #include <time.h>
                using namespace std;

                void srand ( unsigned int seed );
                int a[10][10] ;
                int i, j;
                int x[10];
                int A[10][10], B[10][10], C[10][10], D[10][10], E[10][10], F[10][10];

                int main()
                {
                {
                for(i=1;i<7;i++ )

                {
                for(i=1;i<7;i++ )
                {
                x[i]=i;
                A[i]=(2+pow(x[i],2));
                B[i]=(1+pow(x[i],3));
                C[i]=(2+pow(x[i],2)-1);
                D[i]=(3*pow(x[i],3));
                E[i]=(1+pow(x[i],2));
                F[i]=(3+pow(x[i],2));
                }
                {
                printf("\n");

                for(i=1;i<7; i++)

                {
                printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
                }
                printf("\n");
                for(i=1;i<7; i++)

                {
                printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);

                }
                printf("\n");
                for(i=1;i<7; i++)

                {
                printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);

                }
                printf("\n");
                for(i=1;i<7; i++)

                {
                printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

                }
                printf("\n");
                for(i=1;i<7; i++)

                {
                printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
                }
                printf("\n");
                for(i=1;i<7; i++)

                {
                printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
                }
                }
                }
                }
                for(i=1;i<7;i++ )
                {
                cout<<endl;

                printf("%d\t", A[i]);
                printf("%d\t", B[i]);
                printf("%d\t", C[i]);
                printf("%d\t", D[i]);
                printf("%d\t", E[i]);
                printf("%d\t", F[i]);
                }
                _getch();

                printf("\n");

                printf("Random number: %d\n", rand() %[10][10]);
                srand ( 1 );

                }[/CODE]


                Please put code tags around your codes as per reply guidelines.

                Yiu have taken variables as int type and the values you calculate and store in them are of type double. That's the reason of error "cannot convert from 'double' to 'int [10]'" Declare it as double. Aslo main is int type so it must return a value. That will eliminate the warning!

                Regards

                Comment

                • keith katthy
                  New Member
                  • Oct 2007
                  • 24

                  #9
                  cpp

                  I have declaire it as double, but still error. Is it true i ldo like this?


                  #include <iostream>
                  #include <conio.h>
                  #include <cstdlib>
                  #include <math.h>
                  #include <stdio.h>
                  #include <time.h>
                  using namespace std;




                  void srand ( unsigned int seed );
                  int a[10][10] ;
                  int i, j;
                  int x[10];
                  double A[10][10], B[10][10], C[10][10], D[10][10], E[10][10], F[10][10];
                  int main()
                  {
                  {
                  for(i=1;i<7;i++ )

                  {
                  for(i=1;i<7;i++ )
                  {
                  x[i]=i;
                  A[i]=(2+pow(x[i],2));
                  B[i]=(1+pow(x[i],3));
                  C[i]=(2+pow(x[i],2)-1);
                  D[i]=(3*pow(x[i],3));
                  E[i]=(1+pow(x[i],2));
                  F[i]=(3+pow(x[i],2));
                  }
                  {
                  printf("\n");

                  for(i=1;i<7; i++)

                  {
                  printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
                  }
                  printf("\n");
                  for(i=1;i<7; i++)

                  {
                  printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);

                  }
                  printf("\n");
                  for(i=1;i<7; i++)

                  {
                  printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);

                  }
                  printf("\n");
                  for(i=1;i<7; i++)

                  {
                  printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

                  }
                  printf("\n");
                  for(i=1;i<7; i++)

                  {
                  printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
                  }
                  printf("\n");
                  for(i=1;i<7; i++)

                  {
                  printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
                  }
                  }
                  }
                  }
                  for(i=1;i<7;i++ )
                  {
                  cout<<endl;

                  printf("%d\t", A[i]);
                  printf("%d\t", B[i]);
                  printf("%d\t", C[i]);
                  printf("%d\t", D[i]);
                  printf("%d\t", E[i]);
                  printf("%d\t", F[i]);
                  }
                  _getch();

                  printf("\n");

                  printf("Random number: %d\n", rand() %[i][j]);
                  srand ( 1 );

                  }

                  Comment

                  • Meetee
                    Recognized Expert Contributor
                    • Dec 2006
                    • 928

                    #10
                    Don't write %d. Use %lf instead. Also refer this for datatype specifiers



                    Regards

                    Comment

                    • keith katthy
                      New Member
                      • Oct 2007
                      • 24

                      #11
                      you mean that i must chage all i used for %d o %lf?Like that?

                      Comment

                      • keith katthy
                        New Member
                        • Oct 2007
                        • 24

                        #12
                        Its still error, even I have change from %d to %lf.

                        #include <iostream>
                        #include <conio.h>
                        #include <cstdlib>
                        #include <math.h>
                        #include <stdio.h>
                        #include <time.h>
                        using namespace std;




                        void srand ( unsigned int seed );
                        int a[6][6] ;
                        int i, j;
                        double x[10];
                        double A[10][10], B[10][10], C[10][10], D[10][10], E[10][10], F[10][10];

                        int main(void)
                        {
                        {
                        for(i=1;i<7;i++ )

                        {
                        for(i=1;i<7;i++ )
                        {
                        x[i]=i;
                        A[i]=(2+pow(x[i],2));
                        B[i]=(1+pow(x[i],3));
                        C[i]=(2+pow(x[i],2)-1);
                        D[i]=(3*pow(x[i],3));
                        E[i]=(1+pow(x[i],2));
                        F[i]=(3+pow(x[i],2));
                        }
                        {
                        printf("\n");

                        for(i=1;i<7; i++)

                        {
                        printf("[%lf][%lf] A[%lf]=%lf\n", i,1, i, A[i]);
                        }
                        printf("\n");
                        for(i=1;i<7; i++)

                        {
                        printf("[%lf][%lf] B[%lf]=%lf\n", i,2, i, B[i]);

                        }
                        printf("\n");
                        for(i=1;i<7; i++)

                        {
                        printf("[%lf][%lf] C[%lf]=%lf\n", i,3, i, C[i]);

                        }
                        printf("\n");
                        for(i=1;i<7; i++)

                        {
                        printf("[%lf][%lf] D[%lf]=%d\n", i,4, i, D[i]);

                        }
                        printf("\n");
                        for(i=1;i<7; i++)

                        {
                        printf("[%lf][%lf] E[%lf]=%lf\n", i,5, i, E[i]);
                        }
                        printf("\n");
                        for(i=1;i<7; i++)

                        {
                        printf("[%lf][%lf] F[%lf]=%lf\n", i,6, i, F[i]);
                        }
                        }
                        }
                        }
                        for(i=1;i<7;i++ )
                        {
                        cout<<endl;

                        printf("%lf\t", A[i]);
                        printf("%lf\t", B[i]);
                        printf("%lf\t", C[i]);
                        printf("%lf\t", D[i]);
                        printf("%lf\t", E[i]);
                        printf("%lf\t", F[i]);
                        }
                        _getch();

                        printf("\n");

                        printf("Random number: %lf\n", rand() %[i][j]);
                        srand ( 1 );

                        }

                        Comment

                        • Meetee
                          Recognized Expert Contributor
                          • Dec 2006
                          • 928

                          #13
                          Use CODE tags around your code:
                          [CODE=CPP ]..code goes here..[/CODE ] (without space between CODE and ] )

                          You have taken i as int type and while printf you have taken %lf for i also. Kindly check your code again. There are lots of same mistakes done.

                          Regards

                          Comment

                          • keith katthy
                            New Member
                            • Oct 2007
                            • 24

                            #14
                            Still error, anybody can help me?stuck..


                            #include <iostream>
                            #include <conio.h>
                            #include <cstdlib>
                            #include <math.h>
                            #include <stdio.h>
                            #include <time.h>
                            using namespace std;




                            void srand ( unsigned int seed );
                            int a[6][6] ;
                            double i, j;
                            double x[10];
                            double A[10][10], B[10][10], C[10][10], D[10][10], E[10][10], F[10][10];

                            double main(void)
                            {
                            {
                            for(i=1;i<7;i++ )

                            {
                            for(i=1;i<7;i++ )
                            {
                            x[i]=i;
                            A[i]=(2+pow(x[i],2));
                            B[i]=(1+pow(x[i],3));
                            C[i]=(2+pow(x[i],2)-1);
                            D[i]=(3*pow(x[i],3));
                            E[i]=(1+pow(x[i],2));
                            F[i]=(3+pow(x[i],2));
                            }
                            {
                            printf("\n");

                            for(i=1;i<7; i++)

                            {
                            printf("[%lf][%lf] A[%lf]=%lf\n", i,1, i, A[i]);
                            }
                            printf("\n");
                            for(i=1;i<7; i++)

                            {
                            printf("[%lf][%lf] B[%lf]=%lf\n", i,2, i, B[i]);

                            }
                            printf("\n");
                            for(i=1;i<7; i++)

                            {
                            printf("[%lf][%lf] C[%lf]=%lf\n", i,3, i, C[i]);

                            }
                            printf("\n");
                            for(i=1;i<7; i++)

                            {
                            printf("[%lf][%lf] D[%lf]=%d\n", i,4, i, D[i]);

                            }
                            printf("\n");
                            for(i=1;i<7; i++)

                            {
                            printf("[%lf][%lf] E[%lf]=%lf\n", i,5, i, E[i]);
                            }
                            printf("\n");
                            for(i=1;i<7; i++)

                            {
                            printf("[%lf][%lf] F[%lf]=%lf\n", i,6, i, F[i]);
                            }
                            }
                            }
                            }
                            for(i=1;i<7;i++ )
                            {
                            cout<<endl;

                            printf("%lf\t", A[i]);
                            printf("%lf\t", B[i]);
                            printf("%lf\t", C[i]);
                            printf("%lf\t", D[i]);
                            printf("%lf\t", E[i]);
                            printf("%lf\t", F[i]);
                            }
                            _getch();

                            printf("\n");

                            printf("Random number: %lf\n", rand() %[i][j]);
                            srand ( 1 );

                            }

                            Comment

                            Working...