Array and recursion of 3x3 spreadsheet

Page 1 of 1

11 Replies - 3883 Views - Last Post: 07 August 2010 - 09:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=185012&amp;s=dd1ef69f6712b8e4e174c36f7bcce66e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 thePhantom

Reputation: -1
• Posts: 63
• Joined: 28-April 10

Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 07:39 AM

hi (sorry, dont know how to access my old post)...bt anyways...
ok, Im still stuck with this problem...Im using a 2D array to print out a 3x3 spreadsheet but there must be recursion in it. A can do the array, just dont know how to carry on: ie. when the user enters a formula in the input, it shud output the correct answer e.g

1)Sample Input 1 2 3 4 5 6 =A1+A2 =B1+B2 =C1+C2
Sample Output
1 2 3
4 5 6
5 7 9
2)Sample Input John 12 23 David 45 67 Total =B1+B2 =C1+C2
Sample Output
John 12 23
David 45 67
Total 57 90

this is what Ive done so far:
```   import java.util.Scanner;

class Question1
{
public static void main(String args[])
{
Scanner input= new Scanner(System.in);

int row=3;
int column =3;

String [][]grid= new  String [row][column];

for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
grid[i][j]=input.next();
System.out.print(grid[i][j]+" ");
}
System.out.println("");
}
//Ive PUT everything else in braces as im not sure how to structure my program...I think i shud use the charAt method to separate the formula and use the IntegerparseInt method to convert the String in2 a interger so that i can add or subtract them, but I have no idea how to put my thoughts into code...i do know that the recursion comes in when using the "=" as a base case then output my spreadsheet as in eg2 where the cells keep on getting referred to another block...

/*public getCell()

for(int i = 0; i<=row; i++)
for(int j=0; j<=column;j++)
{
if (grid[i][j].charAt(0)== '=')
{

public getFormula()

for(int i = 0; i<=row; i++)
for(int j=0; j<=column;j++)

for (i=1;i<=grid.length;i+=3)
if (grid[i][j].charAt(1)=='A')

/*	grid[i][j].substring(1,2);
grid[i][j].charAt(1)-'A'
grid[i][j].charAt(2)=

grid[i]=Integer.parseInt(charAt(i)- 'A');
return Integer.parseInt(grid[][])
public getRecursion()
}
}
}*/
}
}

```

Is This A Good Question/Topic? 0

Replies To: Array and recursion of 3x3 spreadsheet

#2 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 342
• Joined: 16-July 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 07:56 AM

#3 thePhantom

Reputation: -1
• Posts: 63
• Joined: 28-April 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 08:05 AM

sorry, wasnt sure what that meant...

```import java.util.Scanner;

class Question1
{
public static void main(String args[])
{
Scanner input= new Scanner(System.in);
int row=3;
int column =3;

String [][]grid= new String [row][column];

for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
grid[i][j]=input.next();
System.out.print(grid[i][j]+" ");
}
System.out.println("");
}

```

//ok, i know i did this part of my code correct, I just dont know how to carry on from here, the following is a few mixed-up ideas that came to mind//

```/*public getCell()

for(int i = 0; i<=row; i++)
for(int j=0; j<=column;j++)
{
if (grid[i][j].charAt(0)== '=')
______________________
public getFormula()

for (i=1;i<=grid.length;i+=3)
if (grid[i][j].charAt(1)=='A')

/* grid[i][j].substring(1,2);
grid[i][j].charAt(1)-'A'

________________________
grid[i]=Integer.parseInt(charAt(i)- 'A');
return Integer.parseInt(grid[i][j])
public getRecursion()
}
}
}*/

```

#4 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 08:20 AM

Could you explain the assignment more clearly? I don't quite get the examples that you posted...2nd is slightly different from 1st in terms of structure.

#5 thePhantom

Reputation: -1
• Posts: 63
• Joined: 28-April 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 08:23 AM

ok, ill post the question:
"Program to compute a simple 3x3 spreadsheet containing integers and strings. First, input the spreadsheet source values from the console (in rows), then perform calculations as outlined below and finally output the computed spreadsheet as a matrix.

Your program only needs to support integer formulae where a sequence of terms are added/subtracted. Assume that your columns (numbered A,B,C) and rows (numbered 1,2,3) will never be more than a single character and cell values/formulae have no spaces or errors. Examples of formulae are:

=A1, =A1+B1, =A1+A2-A3, =B3-C3

For that last few marks, note that your program will be tested for calculations that are not based on static cell values (think recursion)!"

#6 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 08:44 AM

I'm not quite sure what static cell values and recursion in this assignment mean, but this would be how to approach your problem non-recursively...

Well, you will want to create a 3x3 array of Object type to start with. Also, you might want to create a final array of char type, with data {'A','B','C'};
- Then probably use the split() method in the String class to put your input into a String array
- Then fill in the first two rows with indices 0-5 in your inputted array
- Then check for special cases, such as your last row having the header "Total" as in your 2nd example. If so, fill the 3rd row, 1st col.
- Else, if the element contains an equals sign, you know it is an operation. So for that element, it should correspond to which column in the 3rd row...All you have to do is retrieve the column letter using charAt and match to your char array and take that index. Then take the row # and subtract 1 to get your index. That should give you the row and column to take the 1st number from. Do the same for the 2nd number.
- Add the two numbers and place in corresponding column of 3rd row

Not quite sure why you need recursion for this, but then again, I don't quite get what a static cell value is...

#7 thePhantom

Reputation: -1
• Posts: 63
• Joined: 28-April 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 08:50 AM

thnx alot

#8 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 09:12 AM

Well, I made a demo program and it seems to work fine except it's not using recursion. Still trying to think of what your instructor meant by recursion and static cell value...

#9 thePhantom

Reputation: -1
• Posts: 63
• Joined: 28-April 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 09:23 AM

I think that the recursion cums in by the last part of the class, ie. for example if the input wat "=A2+A3 =A3 =B2 =B2 5 =B2 =B2 =C1 =C1+C2" or "1 2 3 =A1 =B1 =C1 =A1+A2 =B1+B2 =C1+C2", then you would keep on recurring to the allocated cell, using your base case of "=" to stop the recursion...so for example C2 directs you to B2 which then directs you to A1+A2...or sumthing alone those lines...

Im still stuck with my program though, probably need to read over my notes again as I cant remember much on coding...I actually just started Java coding again only recently, but Ill see what I can do, thnx for your time and effort

#10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12300
• Posts: 45,400
• Joined: 27-December 08

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 09:40 AM

The way Excel handles recursion or self-referencing is to throw an error. It's hard to handle static values with recursion, but if your instructor says the values in the cells won't remain static, then it might be easier to handle them. The important thing is to describe a base case of some sort. Just some thoughts.

#11 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 09:46 AM

Oh i get it now...just go through your input...filling in the places you know first. Then for the recursive part:
- if the entire array is full, exit
- if the two values needed for the operation are present, do the operation and set the String array of inputs to null at that particular position, indicating the operation was complete
- if a value(s) is missing for the operation, return the method, with an indice incremented truncated by the length of input array
- always return the method with an indice incremented truncated by the length of the input array

Method should stop when all cells are filled, so I suppose this is one way to do it recursively...will try this out later, I have 600pages++ to read before school starts monday

#12 thePhantom

Reputation: -1
• Posts: 63
• Joined: 28-April 10

Re: Array and recursion of 3x3 spreadsheet

Posted 07 August 2010 - 09:55 AM

600?!!! Omw...that aint normal, nyways thnx every1 for your help...Im waaay to tired to carry on today...hopefully, the sun shines brighter on my wrk 2mrw have an awesum weekend, ciao!