473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using rand() with matrix

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. cpp.
  4.  
  5. How to get the random number in matrix like [ ] [ ], the number inside the [ ] [ ], and get the value?
  6.  
  7.  
  8. #include <cstdlib> 
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <time.h>
  12. using namespace std;
  13.  
  14.  
  15.     int i;
  16.     int x[10];
  17.     int A[10], B[10], C[10], D[10], E[10], F[10]; 
  18.  
  19.     int  main(void)
  20. {
  21. {
  22.     for(i=1;i<7;i++)
  23.  
  24. {
  25.     for(i=1;i<7;i++)
  26. {
  27.     x[i]=i;
  28.     A[i]=(2+pow(x[i],2));
  29.     B[i]=(1+pow(x[i],3));
  30.     C[i]=(2+pow(x[i],2)-1);
  31.     D[i]=(3*pow(x[i],3));
  32.     E[i]=(1+pow(x[i],2));
  33.     F[i]=(3+pow(x[i],2));
  34. }
  35. {
  36.     printf("\n");
  37.  
  38.     for(i=1;i<7; i++)
  39.  
  40. {   
  41.     printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]); 
  42. }
  43.     printf("\n");
  44.     for(i=1;i<7; i++)
  45.  
  46. {   
  47.     printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]); 
  48.  
  49.     printf("\n");
  50.     for(i=1;i<7; i++)
  51.  
  52. {   
  53.     printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]); 
  54.  
  55. }
  56.     printf("\n");
  57.     for(i=1;i<7; i++)
  58.  
  59. {   
  60.     printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]); 
  61.  
  62. }
  63.     printf("\n");
  64.     for(i=1;i<7; i++)
  65.  
  66. {   
  67.     printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]); 
  68.     }        
  69.     printf("\n");
  70.     for(i=1;i<7; i++)
  71.  
  72. {   
  73.     printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]); 
  74. }
  75. }
  76. }
  77. }
  78.     for(i=1;i<7;i++)
  79. {
  80.     cout<<endl;
  81.  
  82.     printf("%d\t", A[i]);
  83.     printf("%d\t", B[i]);   
  84.     printf("%d\t", C[i]);
  85.     printf("%d\t", D[i]);
  86.     printf("%d\t", E[i]);
  87.     printf("%d\t", F[i]);
  88. }
  89.     _getch();
  90.  
  91.     printf("\n");
  92.  
  93.     printf("Random number: %d\n", rand() % 100);
  94.     srand ( 1 );
  95.  
  96. }
Oct 22 '07 #1
8 2611
MarkoKlacar
296 Expert 100+
Hi,

did I understand you correctly if I assume you want to index a matrix with some values and get the value stored?

Cheers
Oct 22 '07 #2
sicarie
4,677 Expert Mod 4TB
So you can print rand(), what are you trying to do with that?

If you are attempting to randomly pull values from it, and you have a 2-d array you will need to pull two values from rand both within the limits of your array/matrix. You can call rand(), so I'm assuming saving it isn't the issue, are you having trouble with getting it in the range? If so, you will need to look at the modulus operator ( % ). Otherwise I have no idea what your question is unless it's filling the matrix with random values, in which case you would just implement two for loops to go through each of your index values and save the call to rand() in them.

PS - get rid of getch() and conio.h. They are nonstandard and might break things. Just put a print statement at the bottom of your program, it does the same thing.
Oct 22 '07 #3
dear sicarie,

if i delete conio.h, it will be error, and if i change _getch into printf, undeclaire, so what must I do?
Oct 25 '07 #4
yes. true , did u now how to do?
Oct 25 '07 #5
I have this 2 programming...In the second progrraming I want to call the minimum values from the matrix that I have done, How to the programming?


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib> 
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7. using namespace std;
  8.  
  9.     void srand ( unsigned int seed );
  10.     int i, j;
  11.     int x[10];
  12.     int A[10], B[10], C[10], D[10], E[10], F[10]; 
  13.  
  14.    int  main(void)
  15. {
  16. {
  17.     for(i=1;i<7;i++)
  18.  
  19. {
  20.     for(i=1;i<7;i++)
  21. {
  22.     x[i]=i;
  23.     A[i]=(2+pow(x[i],2));
  24.     B[i]=(1+pow(x[i],3));
  25.     C[i]=(2+pow(x[i],2)-1);
  26.     D[i]=(3*pow(x[i],3));
  27.     E[i]=(1+pow(x[i],2));
  28.     F[i]=(3+pow(x[i],2));
  29. }
  30. {
  31.     printf("\n");
  32.  
  33.     for(i=1;i<7; i++)
  34.  
  35. {   
  36.     printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]); 
  37. }
  38.     printf("\n");
  39.     for(i=1;i<7; i++)
  40.  
  41. {   
  42.     printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]); 
  43.  
  44.     printf("\n");
  45.     for(i=1;i<7; i++)
  46.  
  47. {   
  48.     printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]); 
  49.  
  50. }
  51.     printf("\n");
  52.     for(i=1;i<7; i++)
  53.  
  54. {   
  55.     printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]); 
  56.  
  57. }
  58.     printf("\n");
  59.     for(i=1;i<7; i++)
  60.  
  61. {   
  62.     printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]); 
  63.     }        
  64.     printf("\n");
  65.     for(i=1;i<7; i++)
  66.  
  67. {   
  68.     printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]); 
  69. }
  70. }
  71. }
  72. }
  73.     for(i=1;i<7;i++)
  74. {
  75.     cout<<endl;
  76.  
  77.     printf("%d\t", A[i]);
  78.     printf("%d\t", B[i]);   
  79.     printf("%d\t", C[i]);
  80.     printf("%d\t", D[i]);
  81.     printf("%d\t", E[i]);
  82.     printf("%d\t", F[i]);
  83. }
  84.     _getch();
  85.  
  86.     printf("\n");
  87.  
  88.     printf("Random number: %d\n", rand() %10);
  89.     srand ( 1 );
  90.  
  91. }
  92. #include <iostream>
  93. using namespace std;
  94.  
  95. int Min(const int *Numbers, const int Count)
  96. {
  97.     int Minimum = Numbers[0];
  98.  
  99.     for(int i = 0; i < Count; i++)
  100.         if( Minimum > Numbers[i] )
  101.             Minimum = Numbers[i];
  102.  
  103.     return Minimum;
  104. }
  105.  
  106. double Min(const double *Numbers, const int Count)
  107. {
  108.     double Minimum = Numbers[0];
  109.  
  110.     for(int i = 0; i < Count; i++)
  111.         if( Minimum > Numbers[i] )
  112.             Minimum = Numbers[i];
  113.  
  114.     return Minimum;
  115. }
  116.  
  117. int main()
  118. {
  119.     int Nbrs[] = {                    };    
  120.                 int Total = sizeof(Nbrs) / sizeof(int);
  121.  
  122.     int Minimum = Min(Nbrs, Total);
  123.     cout << "Minimum: " << Minimum << endl;
  124.  
  125.     return 0;
  126. }
Oct 25 '07 #6
rhitam30111985
112 100+

int main()
{
int Nbrs[] = { };
int Total = sizeof(Nbrs) / sizeof(int);

int Minimum = Min(Nbrs, Total);
cout << "Minimum: " << Minimum << endl;

return 0;
}
dunno what ur trying to do here but assuming u wanna find the minimum value in a two dimensional array .. just assign the first element of the first row A[0][0] and iterate thru the matrix and compare each element with this value.. if u find a smaller value then store it in the variable ... something like this:

Expand|Select|Wrap|Line Numbers
  1. int i,j,small=Nbrs[0][0];
  2. for(i=0;i<ROWS;i++)
  3.         for(j=0;j<COLUMNS;j++)
  4.                 if(Nbrs[i][j]<small)
  5.                            small=Nbrs[i][j]
  6.  
this should work
Oct 25 '07 #7
sicarie
4,677 Expert Mod 4TB
dear sicarie,

if i delete conio.h, it will be error, and if i change _getch into printf, undeclaire, so what must I do?
printf() is a C function, try a cin. You would have to press 'enter', but you could always do a print statement saying "Press enter to continue".
Oct 25 '07 #8
Sorry, I do the mistake, actually I dont want the to make a random number, but i want to choose the minimum values from the matrix that I have done? How to call the number that i done?


Expand|Select|Wrap|Line Numbers
  1. >#include 
  2. >#include 
  3. >#include 
  4. >#include 
  5. >#include 
  6. >#include 
  7. >using namespace std;
  8.  
  9.  
  10.  
  11.  
  12. > int i, j;
  13. > int x[10];
  14. > int A[10], B[10], C[10], D[10], E[10], F[10];
  15. >
  16. > int main(void)
  17. >{
  18. >{
  19. > for(i=1;i<7;i++)
  20. >
  21. >{
  22. > for(i=1;i<7;i++)
  23. >{
  24. > x[i]=i;
  25. > A[i]=(2+pow(x[i],2));
  26. > B[i]=(1+pow(x[i],3));
  27. > C[i]=(2+pow(x[i],2)-1);
  28. > D[i]=(3*pow(x[i],3));
  29. > E[i]=(1+pow(x[i],2));
  30. > F[i]=(3+pow(x[i],2));
  31. >}
  32. >{
  33. > printf("\n");
  34. >
  35. > for(i=1;i<7; i++)
  36. >
  37. >{
  38. > printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
  39. >}
  40. > printf("\n");
  41. > for(i=1;i<7; i++)
  42. >
  43. >{
  44. > printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
  45. >}
  46. > printf("\n");
  47. > for(i=1;i<7; i++)
  48. >
  49. >{
  50. > printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
  51. >}
  52. > printf("\n");
  53. > for(i=1;i<7; i++)
  54. >
  55. >{
  56. > printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);
  57. >}
  58. > printf("\n");
  59. > for(i=1;i<7; i++)
  60. >
  61. >{
  62. > printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
  63. > }
  64. > printf("\n");
  65. > for(i=1;i<7; i++)
  66. >
  67. >{
  68. > printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
  69. >}
  70. >}
  71. >}
  72. >}
  73. > for(i=1;i<7;i++)
  74. >{
  75. > cout<>
  76. > printf("%d\t", A[i]);
  77. > printf("%d\t", B[i]);
  78. > printf("%d\t", C[i]);
  79. > printf("%d\t", D[i]);
  80. > printf("%d\t", E[i]);
  81. > printf("%d\t", F[i]);
  82. >}
  83. > _getch();
  84. >
  85.  
  86.  
  87. >}
  88.  
  89.  
  90.  
  91.  
  92. >[1][1] A[1]=3
  93. >[2][1] A[2]=6
  94. >[3][1] A[3]=11
  95. >[4][1] A[4]=18
  96. >[5][1] A[5]=27
  97. >[6][1] A[6]=38
  98. >[1][2] B[1]=2
  99. >[2][2] B[2]=9
  100. >[3][2] B[3]=28
  101. >[4][2] B[4]=65
  102. >[5][2] B[5]=126
  103. >[6][2] B[6]=217
  104. >[1][3] C[1]=2
  105. >[2][3] C[2]=5
  106. >[3][3] C[3]=10
  107. >[4][3] C[4]=17
  108. >[5][3] C[5]=26
  109. >[6][3] C[6]=37
  110. >[1][4] D[1]=3
  111. >[2][4] D[2]=24
  112. >[3][4] D[3]=81
  113. >[4][4] D[4]=192
  114. >[5][4] D[5]=375
  115. >[6][4] D[6]=648
  116. >[1][5] E[1]=2
  117. >[2][5] E[2]=5
  118. >[3][5] E[3]=10
  119. >[4][5] E[4]=17
  120. >[5][5] E[5]=26
  121. >[6][5] E[6]=37
  122. >[1][6] F[1]=4
  123. >[2][6] F[2]=7
  124. >[3][6] F[3]=12
  125. >[4][6] F[4]=19
  126. >[5][6] F[5]=28
  127. >[6][6] F[6]=39
  128.  
  129.  
  130. >3 2 2 3 2 4
  131. >6 9 5 24 5 7
  132. >11 28 10 81 10 12
  133. >18 65 17 192 17 19
  134. >27 126 26 375 26 28
  135. >38 217 37 648 37 39
  136. >
  137. >
Oct 27 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
3
by: Josh | last post by:
Howdy i was recently given a program to do. I have to create a 2d matrix with pointers i have the whole idea down with pointers but there is a problem with one of them i have the code written down...
8
by: jimmy | last post by:
Hi, Have tried my best but can't seem to get it. I have a Matrix and a Vector class that I would like to be in a Geometry namespace. More detail... ****** Matrix.h ****** #include...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
8
by: Skay | last post by:
Someone challenged me to create a dynamic multidimensional array using auto_ptr http://www.gotw.ca/gotw/042.htm Provides a good way to create a single dimensional array using an adapter. ...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
3
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.