Connecting Tech Pros Worldwide Help | Site Map

Sequence

mia023's Avatar
Member
 
Join Date: May 2007
Posts: 89
#1: Dec 12 '07
Hi everyone.
I just want to ask you how to fill a multidimensional array with a sequence of numbers from 1 to n^2 provided that each number occur once.
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Dec 12 '07

re: Sequence


Quote:

Originally Posted by mia023

Hi everyone.
I just want to ask you how to fill a multidimensional array with a sequence of numbers from 1 to n^2 provided that each number occur once.

It's hard to answer that question without just writing the code for you. Why don't you post your best effort and ask a specific question about the code?
mia023's Avatar
Member
 
Join Date: May 2007
Posts: 89
#3: Dec 12 '07

re: Sequence


Quote:

Originally Posted by BigDaddyLH

It's hard to answer that question without just writing the code for you. Why don't you post your best effort and ask a specific question about the code?

here is a small part of code that i thought about. It is wrong!!


int num[][]=new int[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++)
num[i][0]+=i;
num[0][j]+=j;
}
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#4: Dec 12 '07

re: Sequence


That code needs work. Here are a few points:
  1. You wanted to store the numbers from 1 to 2**n. Where is 2**n in your code?
  2. You only assign to num[i][0] and num[0][j] -- the first column and the first row.
  3. Why are you using the operator "+="?
  4. Always enclose a loop body in braces: {}. You didn't do that for your inner loop.
Reply