Hello I am tring to write a code to generate random numbers into an array, I don't understand why this isn't working. please help

#include <iostream>
#include <ctime>
#include <cstdlib>

const int NRows = 5;
const int Ncols = 5;

using namespace std;
int main() 
{
    //declare variables
    int num[NRows][Ncols]; // my matrix
    int temp; //tempoary spot for random numbers
    //seed random number generator
    srand(time(0));

    //introduce program
    cout << "this program will generate a matrix of random number between 0 and 10!" << endl;

    //generate random numbers and input into matrix

    for(int i=0; i<NRows; i++)
    {   

        for(int j=0; j<Ncols; j++)
        {

        temp = rand()%10;
        temp >> num[i][j];

        }
    }

    //display the array
    for(int i=0; i<NRows; i++)
         { 
            for(int j=0; j<Ncols; j++)
            {
            cout << num [i][j] << ' ';
            cout << endl;
            }
         }
        return(0);  
}

You can do away with the temp variable and just have:

num[i][j] = rand() % 10;

Edited 1 Year Ago by nullptr

NullPtr is 100% correct -- just a litle brief I think!

Your question is what is wrong with this code. So as was pointed out the error lies in the line

 temp >> num[i][j];

Let us consider a similar expression:

 int a=5;
 int t=2;
 int x = t >> a;
 std::cout<<"X == "<<x<<std::endl;

Now what I have done here is use >> in its ORIGINAL form (i.e. what C++ took from C) Here the >> operator is used as a right shift operator. That is if we take the binary representation of 5 : 000101
and shift the bits to the right, discarding any bit that get pushed off the end of the number, and do that work twice (because t=2) twice we get 1.

consider the operation as

   000101   ->  00010  -> 00001

Now, before we go on -- hopefully you remember/or will find out soon, that any class object [which is going to act as a complex data store with function that apply to it] can have MOST operators overloaded. We could write classes like this

class number 
  {
    int value;

    public:

      number(int N) : value(N);
      //..   

      // rather strange operator overload !!
      int operator*(const number& A) { return value+A.value; } 
   };

int main()
  { 
     number X(5);
     number Y(7);
     // slightly strange output
     std::cout<<"Output == "<<X*Y<<std::endl;
   }

In this gives Output == 12 !!! Note that I have overloaded the multiply operator and I can do anything I like with it, and it this case I have made it an addition!!

So that brings us back to your code. The problem is that std::cin and std::cout are such classes. They are iostream objects, and as such the programmers that provided them for you, overloaded the operator<< and operator>>. They choose those operators because what does left and right shift mean for such an object?

Unfortunately, you wrote temp >> num[i][j] BUT temp (which is the object under consideration is an integer and it does not have an overloaded operator AND the default bit shift make sense for an integer, so no complains from the compiler. So we are left with the statement : calculate the value temp shifted as many times as the value in num[i][j]. Then do nothing with the result.

That just leaves one question, why do you see strange output from the code. That is because you did not set num[i][j] to any value. So it is not set, it is just whatever, numbers are left in the memory at the time of allocation, probabily different each time you run the program.

Hope that helps, and further question on this answer / or other stuff, just ask

Edited 1 Year Ago by StuXYZ

This article has been dead for over six months. Start a new discussion instead.