# Thread: contingency table problem

1. Member
Join Date
May 2012
Posts
6
Rep Power
0

## could anyone help me with this contingency table problem?

I am struggling to find the java code for this problem. Firstly, I am in a beginner level and this question were hard for me to answer. I guess that this used a 2D array right? but i having problem to write the java code for this question. Kindly please help me?i need to submit this java code as soon as possible. thank you very much.

here is the question :

Write a Java program that
1.queries a user for the number of rows and columns of a contingency table,
2.read the data, row by row and
3.displays the data in tabular form along with the row totals, column totals and grand total.

For example if the six data of 2x3 table are

1,3,6,7,9, and 8. the program displays these six numbers together with the appropriate totals as

1 3 6 | 10
7 9 8 | 24
8 12 14 | 34

The character '|' is used to separate the data from the row totals
Last edited by wan Toushiro; 05-29-2012 at 11:42 AM.

2. Moderator
Join Date
Apr 2009
Posts
13,218
Rep Power
23

## Re: contingency table problem

What have you written so far and where are you having problems, including any exceptions or compilation errors, showing which line they occur on.

3. Member
Join Date
May 2012
Posts
6
Rep Power
0

## Re: contingency table problem

firstly i'm beginner in this java programming and do not know what type of code do i need to do whether it's loop array or etc.. could you guide me to solve this problem? thank you very much.

4. Moderator
Join Date
Apr 2009
Posts
13,218
Rep Power
23

## Re: contingency table problem

Well, you have 3 steps there, so start from the beginning.
So write the code to do part 1, storing the numRows and numCols in int variables.
Then extend that by writing the code to read in the data, storing that in something suitable (an array sounds good).
Then finally look at part 3.

There's no point looking at the whole problem and trying to figure it out in one go...that way lies madness.

5. Member
Join Date
May 2012
Posts
6
Rep Power
0

## Re: contingency table problem

i have done until here. then what should i do? how to assign the number(data) that user enter to the rows and columns? please help me?

package javaapplication16;

/**
*
* @author USER
*/import java.util.Scanner;
import java.util.*;

public class JavaApplication16 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

int numRows;
int numCols;
int sizeTable;
Scanner readinput = new Scanner(System.in);

System.out.println(" Enter the size of table: Rows*Columns " );

sizeTable = numRows*numCols;

System.out.println(" The size of table are : "+numRows+"*"+numCols+"="+sizeTable );
//
int[][] ContigencyTable;
ContigencyTable = new int[numRows][numCols];

}
}

6. Moderator
Join Date
Apr 2009
Posts
13,218
Rep Power
23

## Re: contingency table problem

So there you go, you now have an (empty) table based on the user input.

Now for part 2.
I have no idea where exactly you are supposed to be reading the data from, but if it's the user then you will need to loop around that array asking the user for numbers, then assigning the result to the bit of the array you are currently on.

7. Member
Join Date
May 2012
Posts
6
Rep Power
0

## Re: contingency table problem

could you please write me the code for the loop. the data is 1, 3, 4, 7. can you do it for 2 x 2 table.

8. Moderator
Join Date
Apr 2009
Posts
13,218
Rep Power
23

See my sig.

9. Member
Join Date
May 2012
Posts
6
Rep Power
0

## Re: contingency table problem

i've already email the question.

10. Moderator
Join Date
Apr 2009
Posts
13,218
Rep Power
23

## Re: contingency table problem

Please do not ask for code as refusal often offends.

11. Member
Join Date
May 2012
Posts
6
Rep Power
0

## Re: contingency table problem

ok2..sory. what if you suggest the type of loop that i will use that require the data from user like(nested-loop,if-else or etc..)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•