My Computer Forum Computer Science Forum

Go Back   My Computer Forum > Computer Science Forum > Algorithms

Algorithms Algorithms and Data Structures - Analysis, Graph, Search, String, Sorting, Merge, Compression, Optimization, Quantum


Reply
 
LinkBack Thread Tools Display Modes
June 27th, 2008, 07:41 AM   #1
 
Joined: Apr 2008
Posts: 5
Help with complexity

Hello. I just started learning the language Java since it's very useful. So I'm at a very low level so far!
I just wrote this small program wich types out the multiplication tabel (spelling?) in a odd way.
It seems abit confusing though, maybe you guys can tell me what I can make more simple.
Don't tell me very complex things I could do with it just help me with the structure, please.

Here it is:

Code:
		int limit, number2, head, number1;
		limit=1;
		head=1;
		number1=1;
		
		System.out.println("---THE MULTIPLICATION TABEL---\n");
		//EVERYTHING STARTS
		//DESCRIBING WICH TABEL BEGINS
		while (limit!=6) {
			System.out.print("Multiplication table for "+head+"\t\t");
			head++;
			System.out.println("Multiplication table for "+head);
			head++;
			
                        //TYPING OUT THE NUMBERS
			for(number2=0; number2<=10; number2++) {
				System.out.print(number1+ "*" +number2+ "=" +number1*number2+"\t\t\t\t\t");
				number1++;
				System.out.println(number1+ "*" +number2+ "=" +number1*number2);
				number1--;
				}
			System.out.print("\n");
			limit++;
			number1=number1+2;
		}

Thanks in advance
/Pusik
Pusik is offline  
 

My Computer Forum is free to register and we welcome everyone!

June 27th, 2008, 11:52 PM   #2
 
Joined: Dec 2007
Posts: 138
Re: Help with complexity

I'll be responding to this tomorrow... I'm having computer troubles, so I'm going to give up for the night and figure out why my computer hates me tomorrow, but long story short:
In order to get a "prettier" output, you either need to make the code much more complex, or use more advanced techniques.
cknapp is offline  
June 29th, 2008, 07:54 AM   #3
 
Joined: Dec 2007
Posts: 138
Re: Help with complexity

Ok. finally got around to it.

This may be too advanced, but have you ever worked with arrays? (I'm guessing not...) If so, you just need to read the next paragraph, otherwise, read the whole post. (also, there's another idea that bypasses the arrays at the bottom...)

Create a square two dimensional array of whatever dimensions you want to make the table (you can hard-code this, or allow the user to select it, depending on how comfortable you are with user input. Then iterate through, placing each product in its own index. The print function might be tricky... I have it at the bottom.

Now, a more detailed description.

An array is just a bunch of pieces of data in a "row". Think of each array index as a blank spot to place a variable into
for example, the code for an array of integers (let's say 2, 4, 7, 3) is:

Code:
int [] arr = new int[4]; //instantiates an empty array of size 4.

arr[0]=2; // array indexes start at 0. These next 4 lines are just putting "elements" in the array
arr[1]=4;
arr[2]=7;
arr[3]=3;

//alternatively, you could use the following code:
int [] anArray= {2,4,7,3};
Now accessing each element is just like accessing any other variable, only you need to give an index.
for example:
Code:
System.out.print(arr[0]); //prints whatever is in arr[0].
//or
int a = arr[0]+arr[1];
a good way to iterate through arrays is to use a for loop (or a while loop):
Code:
int i=0;

//while loop prints each element of the array
while (i<4) {
    System.out.println(arr[i]);
    i++;
}
A multidimensional array is the same thing as an array, only there are two indexes. Think of a 2-dimensional array as a grid:
[][][][]
[][][][]
[][][][]
Each one of those boxes can hold a variable. And each row holds an array.
They are indexed: (
(0,0)(0,1)(0,2)...
(1,0)(1,1)...
(2,0)...
...

the code to initiate one is pretty much the same:
Code:
int [][] 2dArr = new int [4][4]; //first box is row, second is columns
iterating through is the same thing with an internal loop:
Code:
for(int i=0;i<4;i++) { //iterates through rows
    for(int j=0;j<4;j++) { //iterates through each column on that row
        arr[i][j]=i*j;
    }
}
Now, let's look at a few iterations of this.
first i=0:
it iterates through j=0-->j=3, multiplying each by 0 (since i=0) and puting that in arr[0][j]. So the first row is:
[0][0][0][0]
Then it increases i to 1, and does it again
[0][1][2][3]
Then i=2
[0][2][4][6]
etc.

Notice anything? We've just created a multiplication table!

Now let's look at the whole code to create and print our multiplication table:

Code:
public void create_mult_table(int size){
/*the above line just says this is a method called create_mult_table, which returns nothing (void) and takes one argument: an integer which
will be called size within the method. Instead, you can just paste the rest of the method into a "main()", and change the line I say to change later.*/

    int[][] table = new int[size][size]; /*if you copy into main(), either hardcode this line with some number, or set the variable "size" before this line*/

    for(int i=0;i<size;i++) { //iterates through rows
         for(int j=0;j<size;j++) { //iterates through each column on that row
              arr[i][j]=i*j;
         }
    }

    //print top row of table
    System.out.print("*\t");
    for(int i=0;i<size;i++){ //prints out the numbers along the top
         System.out.print(i+"\t");
    }
    System.out.println(); //create a new line

    //print table, along with row on left
    for(int i=0;i<size;i++) { //iterates through rows
         System.out.print(i+"|\t");
         for(int j=0;j<size;j++) { //iterates through each column on that row
              System.out.print(arr[i][j]+"\t");
         }
         System.out.println(); //new line
    }
}
The output you get should look like this (for size=4)
Code:
*  0    1    2    3
0| 0    0    0    0
1| 0    1    2    3
2| 0    2    4    6
3| 0    3    6    9
****
Another thing you can do, which is actually more efficient and easier to follow in retrospect is to bypass the array altogether:
Code:
    for(int i=0;i<size;i++) { //iterates through rows
         System.out.print(i+"|\t");
         for(int j=0;j<size;j++) { //iterates through each column on that row
              System.out.print(i*j+"\t"); //multiplies i and j within the print method, taking care of everything in 1 line.
         }
         System.out.println(); //new line
    }
}

Cheers!
cknapp is offline  
June 29th, 2008, 10:43 AM   #4
 
Joined: Apr 2008
Posts: 5
Re: Help with complexity

Ah cool. Now I got an intruduction to arrays aswell! Before this programming-language I was learning C/C++, but I stopped doing that since after I worked at Ericsson Research for two weeks as a school project, they told me that when I got to Advanced Arrays it could be very tough for a rookie like me.
So they introduced me to Java. So this was very helpful.

Is there any limit for how many rows and boxes an array can have?

/Pusik
Pusik is offline  
June 29th, 2008, 11:49 PM   #5
 
Joined: Dec 2007
Posts: 138
Re: Help with complexity

The limit is how much memory you have to work with

Also, unless I'm mistaken, arrays can have any number of dimensions, so the line
Code:
int[][][][] arr = new int;
actually has meaning... although I might be wrong.

The place you'll start running into problems with arrays is uninitialized array elements and "index out of bounds" exceptions. Other than that, they're really simple.
cknapp is offline  
July 1st, 2008, 07:30 AM   #6
 
Joined: Dec 2007
Posts: 232
Re: Help with complexity

Quote:
Originally Posted by cknapp
The limit is how much memory you have to work with

Also, unless I'm mistaken, arrays can have any number of dimensions, so the line
Code:
int[][][][] arr = new int;
actually has meaning... although I might be wrong.

The place you'll start running into problems with arrays is uninitialized array elements and "index out of bounds" exceptions. Other than that, they're really simple.
Java and C# both have the 'jagged' arrays you mention:

Code:
int[][] jagged = new int[2][];
jagged[0] = new int[7];
jagged[1] = new int[3];
C# also has multidimensional arrays, where each has the same size:
Code:
int[,] arr = new int[2,7];
I think both Java and C# (except the latter in unsafe mode with special calls) initialize array members for you.
CRGreathouse is offline  
Reply

  My Computer Forum > Computer Science Forum > Algorithms

Tags
complexity



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Complexity Question baxy Theory of Computation 0 September 17th, 2012 07:20 AM





Copyright © 2018 My Computer Forum Forum. All rights reserved.