Hi,
I am working on a program question from a tutorial website and so have done the following(below) but would like to make some alterations to meet the specifications better. Currently it prints a grid (matrix) and shows the sum of the columns at the bottom. The first change I would like to make is to ask the user to enter the number of columns and rows e.g. if they entered 10 - ten columns would be printed.
The second change I would like to make is to get the program to generate the numbers randomly instead of asking the user to input them. I think you have to use the =rand() to do this but I am struglling to work the rest out.
Could somebody implement the changes for me in the code or show me how to do it.
Thanks
---------------------------------------------------------------------------
import java.io.*;
class PrintMatrix
{
public static void main(
String[ ] args)
{
PrintMatrix theApp = new PrintMatrix();
theApp.run();
}
private void run()
{
int[][] b = new int[5][5];
int[] sum = new int[5];
int total = 0;
//Get the info
for(int i = 0; i < b.length; i++ )
{
System.out.println("Enter data for Column " + (i+1));
for (int j = 0; j < b[i].length; j++ )
{
sum[i] += b[i][j] = getInt();
}
total += sum[i];
}
//Print the info
for(int i = 0; i < b.length; i++ )
{
for (int j = 0; j < b[i].length; j++ )
{
System.out.print(b[j][i] + "\t");
}
System.out.println();
}
//Print the sums
for(int i = 0; i < sum.length; i++){
System.out.print(sum[i] + "\t");
}
System.out.println("Total: " + total);
}
private int getInt()
{
int num;
String inStr = null;
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter integer data: ");
inStr = in.readLine();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
num = Integer.parseInt(inStr);
return num;
}
}