  User Name Remember Me? Password

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

 June 27th, 2008, 06: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 My Computer Forum is free to register and we welcome everyone!
 June 27th, 2008, 10: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. June 29th, 2008, 06: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; //instantiates an empty array of size 4. arr=2; // array indexes start at 0. These next 4 lines are just putting "elements" in the array arr=4; arr=7; arr=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); //prints whatever is in arr. //or int a = arr+arr;``` 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 ; //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[j]. So the first row is:  Then it increases i to 1, and does it again  Then i=2  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
 June 29th, 2008, 09: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 June 29th, 2008, 10: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. July 1st, 2008, 06: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[];
jagged = new int;
jagged = new int;```
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. Tags complexity Thread Tools Show Printable Version Email this Page Display Modes Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Similar Threads Thread Thread Starter Forum Replies Last Post baxy Theory of Computation 0 September 17th, 2012 06:20 AM

 Cryptocurrency Forum - Contact - Home - Top

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