Connecting Tech Pros Worldwide Forums | Help | Site Map

Java help needed.. badly.

Newbie
 
Join Date: Oct 2006
Posts: 6
#1: Oct 11 '06
Hey im stuck on a question that i need to do, i have pretty much the basics of the programme but am not sure how to get it implemented so that it'll work. The question is :

Declare a two dimensional array with 10 rows and 10 columns and initialise:
a) All the elements of its third row to 10
b) All the elements of its 6th column to 20
c) And all the other elements to 5
in that order.

I have the following piece of code that is pretty much the core of the program:

Expand|Select|Wrap|Line Numbers
  1. import uwejava.*;
  2. import glos.*;
  3.  
  4. public class exc4 
  5. {
  6.  
  7.  public int nums;
  8.  
  9.  
  10.  for(int y = 0; y < nums.length; ++y)
  11.   for(int x = 0; x < nums[i].length; ++x)
  12.     if(y == 2)
  13.       nums[y][x] = 10;
  14.     else if(x == 19)
  15.       nums[y][x] = 20;
  16.     else
  17.       nums[y][x] = 5;
  18.   }
  19.  
So if anyone can figure out how to implemet it to work correctly id be delighted. Two solid days of java has pretty much turned my brain to mush :(

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 11 '06

re: Java help needed.. badly.


Quote:

Originally Posted by derbys

Hey im stuck on a question that i need to do, i have pretty much the basics of the programme but am not sure how to get it implemented so that it'll work. The question is :

Declare a two dimensional array with 10 rows and 10 columns and initialise:
a) All the elements of its third row to 10
b) All the elements of its 6th column to 20
c) And all the other elements to 5
in that order.

I have the following piece of code that is pretty much the core of the program:

Expand|Select|Wrap|Line Numbers
  1. import uwejava.*;
  2. import glos.*;
  3.  
  4. public class exc4 
  5. {
  6.  
  7.  public int nums;
  8.  
  9.  
  10.  for(int y = 0; y < nums.length; ++y)
  11.   for(int x = 0; x < nums[i].length; ++x)
  12.     if(y == 2)
  13.       nums[y][x] = 10;
  14.     else if(x == 19)
  15.       nums[y][x] = 20;
  16.     else
  17.       nums[y][x] = 5;
  18.   }
  19.  
So if anyone can figure out how to implemet it to work correctly id be delighted. Two solid days of java has pretty much turned my brain to mush :(

Expand|Select|Wrap|Line Numbers
  1. public class Exc4 {
  2.     public static void main(String[] args) { // every java application has to have this method
  3.         int[][] nums = new int[10][10];//declares a 10 x 10 array of ints
  4.         for(int i = 0; i < nums.length; i++) {
  5.             for(int j = 0; j < nums.length; j++) {
  6.                 if(i == 2) {
  7.                     nums[i][j] = 10;
  8.                 }
  9.                 else if(j == 5) {
  10.                     nums[i][j] = 20;
  11.                 }
  12.                 else {
  13.                     nums[i][j] = 5;
  14.                 }
  15.             }
  16.         }
  17.         for(int i = 0; i < nums.length; i++) {
  18.             for(int j = 0; j < nums.length; j++) {
  19.                 System.out.print(""+nums[i][j]);
  20.             }
  21.             System.out.println();
  22.         }
  23.     }
  24. }
Newbie
 
Join Date: Oct 2006
Posts: 6
#3: Oct 11 '06

re: Java help needed.. badly.


That's brillaint thanks, and opne more question (im a cheeky so and so, i know) I need to

-Create a 50-element integer array
-Fills this array with the random numbers in the range 1 to 10000 and display it.

I *think* i know how i create the integer and random numbers, with the code below - but im not 100% sure on how i should go about displaying it, any help again would be brilliant.

Expand|Select|Wrap|Line Numbers
  1. int[] myArray= new int[50]; // Declaring 50 integers
  2.  
  3. for (int i = 0; i < myArray.length; i++) { //Random number
  4.             Double random = new Double(Math.random() * 10000);
  5.             int randomInt = random.intValue();
  6.             myArray[i] = randomInt;
  7.  
  8.         }
  9.  
I think that's what it is, just need to know what to do to display it

Cheers
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 12 '06

re: Java help needed.. badly.


Quote:

Originally Posted by derbys

That's brillaint thanks, and opne more question (im a cheeky so and so, i know) I need to

-Create a 50-element integer array
-Fills this array with the random numbers in the range 1 to 10000 and display it.

I *think* i know how i create the integer and random numbers, with the code below - but im not 100% sure on how i should go about displaying it, any help again would be brilliant.

Expand|Select|Wrap|Line Numbers
  1. int[] myArray= new int[50]; // Declaring 50 integers
  2.  
  3. for (int i = 0; i < myArray.length; i++) { //Random number
  4.             Double random = new Double(Math.random() * 10000);
  5.             int randomInt = random.intValue();
  6.             myArray[i] = randomInt;
  7.  
  8.         }
  9.  
I think that's what it is, just need to know what to do to display it

Cheers

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < myArray.length; i++) {
  2.    System.out.print(myArray[i] + " ");// will print all numbers in one line
  3. }
or if you are using jdk 1.5

Expand|Select|Wrap|Line Numbers
  1. for(int i : myArray) {
  2.    System.out.print(i + " ");
  3. }
Reply