3 Replies - 3806 Views - Last Post: 03 March 2010 - 03:40 PM Rate Topic: -----

#1 Guest_Sam*


Reputation:

Random Numbers Squared in 2D array

Posted 03 March 2010 - 02:41 PM

Hey guys.

Im new to c# and this is my first post.

Basically i'm dealing with a 10 rows x 2 columns array. i want to populate the first column with random numbers and the 2nd column with squared values of those numbers.

my code looks like this
    class Program
    {
        static void Main(string[] args)
        {

            const int NUM_ROWS = 10;
            const int NUM_COLS = 2;

            int[,] randint = new int [NUM_ROWS,NUM_COLS];
            Random randNum = new Random();
            

            for (int row = 0; row < randint.GetLength(0); row++)
            {
                for (int col = 0; col < randint.GetLength(1); col++)
                {
                    int x=randNum.Next(1,100);
                    int y = x * x;
                    randint[row,col] = x;

                    Console.Write(string.Format("{0,5:d} ", randint[row, col]));
                }
                Console.WriteLine();
            }

            }
     }


now this populates both columns with random numbers but i only want one of them, and the other with squared values of those random numbers.

I appreciate any help
Thank you

Is This A Good Question/Topic? 0

Replies To: Random Numbers Squared in 2D array

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Random Numbers Squared in 2D array

Posted 03 March 2010 - 02:50 PM

un-nest the for loops. You essentail want to do two passes, first adding some number and then second pass square the numbers
for(...){
 randint [0,i] = random num
}
for(...){
 randint[1,i] = randint[0,i] * randint[0,i];
}


OR

have a conditional in the inner loop
if( col == 0){ make random number
}else if( col == 1){
  square then number from randint [row,col-1] and save it to randint [row,col]
}


This post has been edited by SwiftStriker00: 03 March 2010 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Sam*


Reputation:

Re: Random Numbers Squared in 2D array

Posted 03 March 2010 - 03:31 PM

View PostSwiftStriker00, on 03 March 2010 - 01:50 PM, said:

un-nest the for loops. You essentail want to do two passes, first adding some number and then second pass square the numbers
for(...){
 randint [0,i] = random num
}
for(...){
 randint[1,i] = randint[0,i] * randint[0,i];
}


OR

have a conditional in the inner loop
if( col == 0){ make random number
}else if( col == 1){
  square then number from randint [row,col-1] and save it to randint [row,col]
}




Swift i cannot thank you enough sir!
i used the if statement and it worked like a charm. again i appreciate it.
Was This Post Helpful? 0

#4 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Random Numbers Squared in 2D array

Posted 03 March 2010 - 03:40 PM

Even simpler, use 1 loop:
class Program
    {
        static void Main(string[] args)
        {
            const int NUM_ROWS = 10;
            const int NUM_COLS = 2;

            int[,] randint = new int [NUM_ROWS,NUM_COLS];
            Random randNum = new Random();
 
            for (int row = 0; row < randint.GetLength(0); row++)
            {
                randint[row,0] = randNum.Next(1,100);
                randint[row,1] = randint[row,0]*randint[row,0];

                Console.Write(string.Format("{0,5:d} {0,5:d}\n", randint[row,0], randint[row,1]));
            }
        }
     }


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1