By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Please Help Me Debug this Code

P: 1
#include <iostream>
using namespace std;


int main()
{
double[100] r;
for (int i = 0; i < 100; i++)

for (int r = 0; r < 100; i++)
{
r(i) = rand() % 100;
}
return 0;
}
Jun 11 '07 #1
Share this Question
Share on Google+
2 Replies


ilikepython
Expert 100+
P: 844
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main()
  6.     double[100] r;
  7.     for (int i = 0; i < 100; i++)
  8.  
  9.     for (int r = 0; r < 100; i++)
  10.     {    
  11.     r(i) = rand() % 100;
  12.     }
  13.     return 0;
  14. }
I'm pretty sure that won't compile. You have several errors:
Expand|Select|Wrap|Line Numbers
  1. double[100] r;
  2. //should be
  3. double r[100];
  4. //
  5. // the integer in the 2nd for loop is called "r" and so is the array; change one of them
  6. //
  7. r(i) = rand() % 100;   // use square brackets for indexing
  8. //should be
  9. r[i] = rand() % 100;
  10.  
And a couple more things. You never seeded the random generator using srand() and you don't need two for loops.
Jun 12 '07 #2

ilikepython
Expert 100+
P: 844
Btw, thanks for joining and read the posting guidelines
Jun 12 '07 #3

Post your reply

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