3 Replies - 957 Views - Last Post: 19 November 2012 - 07:49 PM Rate Topic: -----

#1 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

2d arrays, making a table

Posted 19 November 2012 - 12:27 AM

hello ,im trying to learn how to use 2d arrays. so i thought id first try to make a table that is filled with random numbers. I want the program to take in the size of the rows and columns for users to create the table. im almost done with it , except im having 1 problem. i cant get it to print the table out. i made a method to print the arrays , and passed the table through it , but i get an error that says the method printarray in the type table is not applicable for the arguments (int). ive never seen this error before , so i have no idea what to correct.





import java.util.Scanner;
public class table {

	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the number of rows in the table: ");
		int rows = input.nextInt();
		System.out.println("Enter the number of columns in the table: ");
		int columns = input.nextInt();
		//System.out.println("Enter the data for the table: ");
		//int data = input.nextInt();
		// creates a new 2d array
		int [][] table = new int[rows][columns];
		// fills the array 
		for (int i=0; i<table.length; i++){
		for (int j=0; j<table[i].length; j++)
		table[i][j] = (int)(Math.random() * 1000);
		
	
		}
		printarray(table [rows][columns]);
		
		
	}
	public static  void printarray(int x[][]){

		for (int i =0; i<x.length;i++ ){
			for (int j =0; j<x[i].length;j++)

			System.out.print(x[i][j]+" ");		
		}	
        System.out.println();
}
}




This post has been edited by zerocool18: 19 November 2012 - 12:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 2d arrays, making a table

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 2d arrays, making a table

Posted 19 November 2012 - 12:42 AM

Just pass the name of the array to the method, table. What you have now would be a single element of the array which would be an int.
Was This Post Helpful? 0
  • +
  • -

#3 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

Re: 2d arrays, making a table

Posted 19 November 2012 - 12:55 AM

woohoo!! got that working. thanks!! one more thing. im getting my table back as
771 184 747 986

its not putting the new line after each row. though my printarray method is supposed to do this.

alright i just moved the System.out.println(); in my method inside the inner for loop and it works. thats right right? just seems odd, i usually put it outside when i want it to print a new line,like i have it in my posted code
Was This Post Helpful? 0
  • +
  • -

#4 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

Re: 2d arrays, making a table

Posted 19 November 2012 - 07:49 PM

alright ive got 1 new problem with my program. when somone enters even column and rows (ie 3x3 ,4x4) everything works fine, but if somone puts in more rows that columns or vice vers,i get out of bounds errors for both diagonals and the summing of the columns is off. im guessing theres something wrong with the methods. im trying to fix the main diagonal method to accomidate the uneven rows/columns. but im not sure how to do this.









import java.util.Scanner;
public class table {


	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the number of rows in the table: ");
		int rows = input.nextInt();
		System.out.println("Enter the number of columns in the table: ");
		int columns = input.nextInt();

		// creates a new 2d array
		int [][] table = new int[rows][columns];
		// creates the array for the entered data and stores it
		System.out.println("Enter the data for the table: ");
		int [][]data = new int [rows][columns];
		//fills the array 
		for (int i=0; i<data.length; i++){
			for (int j=0; j<data[i].length; j++)
				table[i][j] = input.nextInt();		   
		}
		System.out.println();
		printarray(table);
		System.out.println();
		System.out.println("The Total for the table is: "+total(table));
		System.out.println();
		System.out.println("The Total for each row is: ");
		sumByRow(table);
		System.out.println("The Total for each column is: ");
		sumByColumn(table);
		System.out.println();
		majorDiagonal(table);
		System.out.println();
		minorDiagonal(table);




	}
	public static void minorDiagonal (int x[][]){
		int minorSum=0;
		for (int i=0; i<x.length; i++){	
			minorSum = minorSum+x[i][(x.length-1)-i];
			
		}
		System.out.println("The Total for the minor diagonal is:  " + minorSum);
	}
	
	public static void majorDiagonal (int x[][]){
		int majorSum=0;
		for (int i=0; i<x.length; i++){	
			majorSum = majorSum+x[i][i];
			
		}
		System.out.println("The Total for the major diagonal is:  " + majorSum);
	
		}

	
	public static void sumByColumn (int x[][]){
		int sum ;
		for(int j=0;j<x.length;j++)
		{
			sum=0;
			for (int i=0;i<x[0].length;i++)
			{
				sum=sum+x[i][j];

			}
			System.out.println("column " +(j+1)+" is: "+sum);

			}

		}

		public static void sumByRow (int x[][]){
			int sum ;
			for (int i=0; i<x.length; i++){
				sum=0;
				for (int j=0; j<x[i].length; j++)
					sum =sum+x[i][j];
				System.out.println("Row " + i + " is: " + sum); 
			}

		}


		public static int total (int x[][]){
			int sum = 0;
			for (int i=0; i<x.length; i++){
				for (int j=0; j<x[i].length; j++){
					sum =sum+x[i][j];
				}

			}

			return sum;	

		}
		public static  void printarray(int x[][]){

			for (int i =0; i<x.length;i++ ){
				for (int j =0; j<x[i].length;j++)

					System.out.print(x[i][j]+" \t");	
				System.out.println();
			}	

		}


	}


This post has been edited by zerocool18: 19 November 2012 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1