Hey guys, I had to determine the output of given code that uses a 2D array. The 2D array given looks like this:

data.txt:

5 8 4 3 9 5
6 4 9 5 3 2
2 2 0 9 7 3
7 4 5 6 9 5
8 8 3 2 6 4
9 5 6 3 7 6

The code given had me thinking that it was trick code and simply gave an error output, or that if one of the numbers is less than 6, it will take the number and % by 2. So, if numbers were bigger than 6, nothing happens to them. But, there's a point where I questioned the % 2 and thought that it was simply going to increment everything less than 6. Anyways heres the code let me know if I'm close to being correct >_<

``````import java.util.Scanner;

public class TwoDArray {
final private static int NUM = 6;

public TwoDArray(){
}

public void load(int[][] grid){
int row, col;
String fileName = "data.txt";
try{
Scanner inFile = new Scanner(new File(filename));
for (row = 0; row < NUM; row++){
for (col = 0; col < NUM; col++){
grid[row][col] = inFile.readInt();
}
}
}catch(IOException e){
System.out.println(“Error: “ + e.getMessage());
}
}

public void display(int[][] grid){
int row, col;
for (row = 0; row < NUM; row++){
for (col =0; col < NUM; col++){
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
System.out.println();
}

public void fun(int[][] grid){
int row, col;
for (row = 0; row < NUM; row++){
for (col = 0; col < NUM; col++)
if ((grid[row][col] % 2) == 0){
grid[row][col] = 0;
}
}
}
}
}

public class driver{
public static void main(String[] args){
TwoDArray app = new TwoDArray();
int [][] matrix = new int[NUM][NUM];
app.load(matrix);
app.display(matrix);
app.fun(matrix);
app.display(matrix);
}
}``````

Thanks for any of your help guys. I'm new to this particular forum, but I think that the introduction threads are redundant.

-Asmodaii

"There is no spoon" -The Matrix

3
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by Asmodaii

Hi Asmodaii and welcome to DaniWeb,

You haven't really told us what you are after, but I think you are a bit confused by this section of the code because of your description:

``````public void fun(int[][] grid){
int row, col;
for (row = 0; row < NUM; row++){
for (col = 0; col < NUM; col++)
if ((grid[row][col] % 2) == 0){
grid[row][col] = 0;
}
}
}
}``````

It is actually saying for each row and column, that is for every value in the 2D Array, if the value is even set it to 0. If the value is odd, nothing happens to it. The less than 6 is because there are 6 rows and 6 columns, it doesn't affect the value of the item at row, column. Please let us know what question you would like answered or what you are trying to do...

I was told to determine the output of this, like whatever 2D array would be made from it. I get what you said about the odd numbers because I didn't see that they were using boolean algebra. Anywho, would the output look like something along the lines of...

5 0 0 3 9 5
0 0 9 5 3 0
0 0 0 9 7 3
7 0 5 0 9 5
0 0 3 0 0 0
9 5 0 3 7 0

...?

You have the program, you have the set of input over which to test I wonder what stops you from running the program, feeding it the input and then checking whether the output you predicted was right or wrong.

We have BlueJ here at the school and quite frankly, I have had problems with it, and I think I should be able to learn how to figure these things out by merely viewing the code and array that are provided. Anyways I will be back on later tonight to hopefully work on a few of these problems. If anyone else can contribute in between then and now, feel free to post any advice.

-Asmodaii

>We have BlueJ here at the school and quite frankly, I have had problems with it

You are telling me you haven't been taught running Java programs from console or from BlueJ either.

>I think I should be able to learn how to figure these things out by merely viewing the code and array that are provided

You have already figured it out "looking" at it, by running the program you are confirming what you have figured out is correct or not. Do you mean from this that you are asked to not to run the program, I am damn sure thats not the case go ahead and run the program and check for yourself.

>If anyone else can contribute in between then and now, feel free to post any advice.

Translation : I don't want answers such as the one given in the previous post, which tell me to do something on my own. I want answers that spoon feed me, I want to rely on other people for telling me whether I am right or wrong when I myself can do that with a little bit of effort.

>You are telling me you haven't been taught running Java programs from console or from BlueJ either.

I haven't been taught jack about hardly anything I'm on an online course and have basically no instructor with very vague lessons written in worse than terribad context to what I may be actually doing.

>You have already figured it out "looking" at it, by running the program you are confirming what you have figured out is correct or not. Do you mean from this that you are asked to not to run the program, I am damn sure thats not the case go ahead and run the program and check for yourself.

I wasn't asked to run the code, and quite frankly, I don't want to go through all the trouble of doing it becaue I wouldn't LEARN as much. A lot of the problems in the worksheets they give me include the most nondescript instructions that are debatebly the worst I've ever gotten in my entire career as a student.

>If anyone else can contribute in between then and now, feel free to post any advice.

ACTUAL Translation: I've got enough work backed up on me that I'm constantly doing work on multiple worksheets, and I'm not asking you guys to do the work for me, but perhaps just point me in the right direction or give me some clues instead of wasting half an hour picking my post apart. You'd be amazed at how much more you could help a person with a schedule as packed and strenuous as mine by actually giving advice rather than commands or criticisms. I can honestly say you seem like a very Type A personality person.

I haven't been taught jack about hardly anything I'm on an online course and have basically no instructor with very vague lessons written in worse than terribad context to what I may be actually doing.

This doesn't give you any excuse to not search for information online. Don't tell me, or anyone here for that matter, that you are taking a course in java where you haven't been taught to run programs which should rather be among the very first things taught.
I have seen many classes where on the first days of it they teach you to write and run a program called "Hello World". So don't just throw this crap onto us. Look for tutorials on the class site, I am sure you'll find help there.

have basically no instructor with very vague lessons written in worse than terribad context to what I may be actually doing.

A lot of the problems in the worksheets they give me include the most nondescript instructions that are debatebly the worst I've ever gotten in my entire career as a student.

Then this course is not worth it, do yourself a favour get out of the course.

I wasn't asked to run the code

Hmmm.. exactly and when you are given the exam papers no one asks you to write them. Got the point ?

I don't want to go through all the trouble of doing it becaue I wouldn't LEARN as much

Hehe ....This is a typical statement people like you make, I don't know the type of it but in my culture we call them LAME.

just point me in the right direction or give me some clues instead

You have the program, you have the set of input over which to test I wonder what stops you from running the program, feeding it the input and then checking whether the output you predicted was right or wrong.

What was this then ? But you won't take it because you don't want any trouble even though you are the one whose going to benefit from it, all you want is ready answers - lame lame lame.

You'd be amazed at how much more you could help a person with a schedule as packed and strenuous as mine by actually giving advice rather than commands or criticisms

I don't want to be amazed, I am quite amazed by my own work itself, and qouting me once again you had been given the right advice but you don't want to take it, since it doesn't suit you.

I can honestly say you seem like a very Type A personality person.

Whatever that means it works for me good as long as I don't go begging for help to people just because I don't want to work my ***. Now I don't know what type of a person that is may be Type F. eeh... get it ?

Lastly, has it dawned upon you why nobody has offered you help yet ? Because they all have understood what I did.

I haven't been taught jack about hardly anything I'm on an online course and have basically no instructor with very vague lessons written in worse than terribad context to what I may be actually doing.

This doesn't give you any excuse to not search for information online. Don't tell me, or anyone here for that matter, that you are taking a course in java where you haven't been taught to run programs which should rather be among the very first things taught.
I have seen many classes where on the first days of it they teach you to write and run a program called "Hello World". So don't just throw this crap onto us. Look for tutorials on the class site, I am sure you'll find help there.

To be honest I have searched online about this topic and I've done plenty to try to understand this more, giving you no right to discredit my efforts. As far as the running programs being the first thing we learn, yes, that is true, but that doesn't include using data files or arrays. We've already programmed plenty of things in java, but it doesn't help me here to waste a bunch of time doing that when I could learn to just look at code like this for 30 seconds and determine the exact output a heck of a lot easier.

Then this course is not worth it, do yourself a favour get out of the course.

If you didn't know, AP Computer Science is a year-round course and although the entire class, with about 2 exemptions, has done terribly, they won't let any of us out of it. Instead they just send letters home and give us a conference to make it look like it's our fault instead of theirs, because honestly I am not a very arrogant person, but it is their fault.

I don't want to go through all the trouble of doing it becaue I wouldn't LEARN as much

Hehe ....This is a typical statement people like you make, I don't know the type of it but in my culture we call them LAME.

Well you must have a very strange culture because where I come from, people who want to learn aren't necessarily called lame, but rather a bit more astute and willing. Going in and running this code would be more like one of you giving me an answer right off. I run the code and get an answer, what do I learn other than how to run it? Go ahead and tell me, because you must be the all-knowing one to share this with me.

What was this then ? But you won't take it because you don't want any trouble even though you are the one whose going to benefit from it, all you want is ready answers - lame lame lame.

I don't see your point in this because I never asked for ready answers, in fact, I detest them. That's why I asked for suggestions or maybe, I don't know, some advice or clues as to what I should do rather than running the code or going off and finding it out on my own online somewhere else. I figured I would do this before I came to this forum, and this forum is what I found to hopefully do just that.

I don't want to be amazed, I am quite amazed by my own work itself, and qouting me once again you had been given the right advice but you don't want to take it, since it doesn't suit you.

I don't know anyone who takes over-assertive commands as advice that suits them. In retrospect, I fully regret you ever posting on my thread because you've done nothing but waste your precious time you could spend on your own work that amazes you. In my culture, which seems to be a bit more proper and upstanding over yours, that is called CONCEITED, good sir.

Whatever that means it works for me good as long as I don't go begging for help to people just because I don't want to work my ***. Now I don't know what type of a person that is may be Type F. eeh... get it ?

Lastly, has it dawned upon you why nobody has offered you help yet ? Because they all have understood what I did.

Maybe you should take the initiative to learn a bit more about yourself and look that term up. And I don't know who you would be to know enough about me to judge my efforts or how hard I'm working my rear end, but I have been and I don't think anywhere I was begging, but merely asking. And before you even posted someone already gave me help, so I don't understand your last bit there, but I can say that nobody else would want to post on this thread with you to get in between the feud you've invoked here. I'll just move to a different forum maybe, since you seem to be more interested in flaming than informing. Go ahead and get your last word in please, because I know your life would seem worthless to yourself if you let someone like me, whom you seem to demean to the highest degree, get the last word in a now seemingly pointless thread that is now based on degrading my efforts instead of assisting them.

Ok timeout guys, this isn't a slinging match. :P

Asmodaii, I will try to help you, but I'm not really sure what it is you are after. I think from what you wrote earlier, you are asking what would be the output of the program you have been given if the supplied 2D array is the input? If that's what you are after, can I point you towards the method of the driver class, main. This is the method that is called when your program is run. Step through it logically, one line at a time, and see what you come up with. I'll give you a start:

``````public class driver{
public static void main(String[] args){
// the first line of code creates an instance of your TwoDArray class
// (nothing is output from this line)
TwoDArray app = new TwoDArray();
int [][] matrix = new int[NUM][NUM];
app.load(matrix);
app.display(matrix);
app.fun(matrix);
app.display(matrix);
}
}``````

See my comments in the code to give you a start.
Have a try, if you still can't get it let us know where you are struggling and we will try to help.

Regards,
d :)

It's true that I haven't been agreeing with most of what you have to say on this thread but one thing I need to agree with you on is that we both are certainly wasting our time picking at each other which isn't what we both are here for.

Lets say I was wrong and you were right, if that makes you smile and be happy. Yes we both got carried away and thats what we should keep an eye for and avoid. I want you to know that I wanted you to learn the way I think things could be learnt but I guess you have your opinions.

You don't have to go away from this forum. This forum promises help which is much more important than any particular member, whether you or me, if you have to go away just because of this, than it would be like defeating the forum's purpose. I leave this thread open to others who would want to help you. And also you could continue posting your further queries here and I can continue posting my replies to them if I find I could be of help minus all the fued.

:)

Edit : drakagn had already posted when I submitted mine.

Unless I'm mistaken, I would believe that there is multiplication in this, and each time it runs through a count, it increases in size (by a lot). If I'm off on this let me know, I'll be working on other things...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.