Two posts ago, I talked about arrays. Did you know that you can make an array of arrays? These arrays will work like a matrix in java. Today I will teach you how to use arrays of arrays or matrices in java.
Think about an array as a line, it exists in exactly one dimension. Now for each point on that line, place a line that is perpendicular to the starting line. Now Image this with arrays instead of lines. This basically gives you a plane filled with data on points, or a matrix. This means that we can view a matrix as simply an array of arrays.
Now that you know this, you may be wondering how you initialize an array. To do this you would type:
(type) [][] (name);
Where type is the data type that you want to use and name is what you want to name the matrix. This will successfully create the matrix, but the matrix will be pointing to null (not there). To get the matrix to point to something, you would type:
(type) [][] (name) = new (type) [(# of rows)] [(# of columns)];
This will create a matrix filled with the number of rows and columns that you want to use. Now that you have the matrix, you probably want to fill it with some data. To add data to a point in a matrix, you would type:
(data you want to add) = (name) [(row)] [(column)];
Hopefully you know know something new about matrices (aka arrayception).
Sunday, November 18, 2012
Monday, November 12, 2012
11/12/12 Quiz bowl
This has been a very busy week for me, and we did not learn a new topic this week because we had a test. Today I will not teach you something new, but I will talk about the computer science questions that I did or did not get at a quiz bowl tournament this weekend. You can find the stats of the tournament here, and my stats here and here.
Computer science dose not come up very often in quiz bowl, but in a tournament there are normally about 4 questions. The first was a bonus question about data structures. The answers were array, stack, and red black tree. I got array correct, but confused heaps and stacks. For the last part I answered binary tree, and I was prompted but I did not know what a red and black tree was, so I also missed that part. This was probably one of my weakest computer science questions that day.
Next was a java question. I would have gotten this question if our opponent was anyone but St. Johns A, the players of which just memorize certain key words to answers, which allows them to get questions very early with out knowing very much. Because of this they got the question on the second word, while I would probably have got it about a sentence later.
The third question was a search algorithm question. I was able to get this one when they were talking about big O notation, which can tell you how much time it takes for a search algorithm to run. I knew that the answer was eater going to be search or sort from the start of the question, but I was not sure which until the big O clue.
I am sorry that I did not talk teach you anything new about java, but last week we just studied and took a test on logic, loops, and arrays. Also, I am sorry if this post did not make much sense to you, but I had to miss school on Friday for the tournament and I need to do all my homework today. Just to give you some context, here is an example quiz bowl question:
The next version of this language has the designation 0x (zero x). One important part of this language is templates and it supports references. One can manage memory in it by using the delete and new operators. In it, one can print to the standard output using cout. Unlike its ancestor, this language supports classes. For 10 points, name this programming language whose name reflects that it is a step up from its predecessor, C.
Answer: c++
Computer science dose not come up very often in quiz bowl, but in a tournament there are normally about 4 questions. The first was a bonus question about data structures. The answers were array, stack, and red black tree. I got array correct, but confused heaps and stacks. For the last part I answered binary tree, and I was prompted but I did not know what a red and black tree was, so I also missed that part. This was probably one of my weakest computer science questions that day.
Next was a java question. I would have gotten this question if our opponent was anyone but St. Johns A, the players of which just memorize certain key words to answers, which allows them to get questions very early with out knowing very much. Because of this they got the question on the second word, while I would probably have got it about a sentence later.
The third question was a search algorithm question. I was able to get this one when they were talking about big O notation, which can tell you how much time it takes for a search algorithm to run. I knew that the answer was eater going to be search or sort from the start of the question, but I was not sure which until the big O clue.
I am sorry that I did not talk teach you anything new about java, but last week we just studied and took a test on logic, loops, and arrays. Also, I am sorry if this post did not make much sense to you, but I had to miss school on Friday for the tournament and I need to do all my homework today. Just to give you some context, here is an example quiz bowl question:
The next version of this language has the designation 0x (zero x). One important part of this language is templates and it supports references. One can manage memory in it by using the delete and new operators. In it, one can print to the standard output using cout. Unlike its ancestor, this language supports classes. For 10 points, name this programming language whose name reflects that it is a step up from its predecessor, C.
Answer: c++
Friday, November 2, 2012
11/2/12 Arrays
Let's say that you want to create a program that will hold as many Fibonacci numbers as you input. This seems possible with what we know, we could just use a for loop that counts up to the imputed number, defining a variable for each value. This may seem like it would work however, how you need create a variable for EACH number they enter, which could be infinity large. This means that you must define infinity many variables to do this. This may seem impossible, however it is easy if you use an array.
An array is what is called a data structure, meaning that it holds a bunch of variables. A string for example is just a collection of chars, meaning that a string is an array. So, getting back to the Fibonacci problem, you could create an array that contains the entered number of Fibonacci numbers. Here is my code for solving this problem:
import java.util.Scanner;
public class test2{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("how many numbers?");
int HowMany = scan.nextInt();
int [] test = new int [HowMany];
test[0]=1;
test[1]=1;
for(int runs = 2; runs<HowMany; runs++){
test[runs]=((test[runs-1])+(test[runs-2]));
}
for(int runs = 0; runs<HowMany; runs++){
System.out.print(test[runs]+" ");
}
}
}
In this code, test is an array, and when I wanted to use or edit a value of a number in the array, I put test[x], where x is the index that you want to reference. If any of this is confusing to you, please tell me, and I will do my best to clarify for you. Hopefully you learned a thing or two about arrays and java.
ps, for Halloween, I dressed up as windows vista =)
An array is what is called a data structure, meaning that it holds a bunch of variables. A string for example is just a collection of chars, meaning that a string is an array. So, getting back to the Fibonacci problem, you could create an array that contains the entered number of Fibonacci numbers. Here is my code for solving this problem:
import java.util.Scanner;
public class test2{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("how many numbers?");
int HowMany = scan.nextInt();
int [] test = new int [HowMany];
test[0]=1;
test[1]=1;
for(int runs = 2; runs<HowMany; runs++){
test[runs]=((test[runs-1])+(test[runs-2]));
}
for(int runs = 0; runs<HowMany; runs++){
System.out.print(test[runs]+" ");
}
}
}
In this code, test is an array, and when I wanted to use or edit a value of a number in the array, I put test[x], where x is the index that you want to reference. If any of this is confusing to you, please tell me, and I will do my best to clarify for you. Hopefully you learned a thing or two about arrays and java.
ps, for Halloween, I dressed up as windows vista =)
Subscribe to:
Comments (Atom)