4 Replies - 259 Views - Last Post: 10 October 2012 - 02:39 PM Rate Topic: -----

#1 Portista90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-October 12

Code explanation

Posted 10 October 2012 - 11:23 AM

Can someone please translate some of these While loops and for loops so I can better understand them. I just want an explanation of how each one works. Thanks!


public class Palin {
	public static void main(String[] args)
	{
		char[][] comb = new char[250][5];//2d array
		
		//Fill in first letter
		
		int count=0;
		int check=0;
		while(count < 1)
		{
			for(int i = 0; i < 81; i++)
				comb[i][0] = 'a';
			
			for(int j = 81; j < 162; j++ )
				comb[j][0] = 'b';
			
			for(int k = 162; k < 243; k++)
				comb[k][0] = 'c';
			
			count++;
			
		}
//System.out.println(comb[162][0]); check to make sure rows are filled correctly

		// Fill in second letter
		count = 0;
		check = 0;
		while(count < 3)
		{
			for(int i = 0; i < 27; i++)
				comb[i+check][1] = 'a';
			
			for(int j = 27; j < 54; j++ )
				comb[j+check][1] = 'b';
			
			for(int k = 54; k < 81; k++)
				comb[k+check][1] = 'c';
			
			count++;
			check+=81;
		}
		
		// Fill in third letter
		count = 0;
		check = 0;
		while(count < 9)
		{
			for(int i = 0; i < 9; i++)
				comb[i+check][2] = 'a';
			
			for(int j = 9; j < 18; j++ )
				comb[j+check][2] = 'b';
			
			for(int k = 18; k < 27; k++)
				comb[k+check][2] = 'c';
			
			count++;
			check+=27;
		}
		
		// Fill in fourth letter
		count = 0;
		check = 0;
		while(count < 27)
		{
			for(int i = 0; i < 3; i++)
				comb[i+check][3] = 'a';
			
			for(int j = 3; j < 6; j++ )
				comb[j+check][3] = 'b';
			
			for(int k = 6; k < 9; k++)
				comb[k+check][3] = 'c';
			
			count++;
			check+=9;
		}
		
		// Fill in fifth letter
		count = 0;
		check = 0;
		while(count < 81)
		{
			for(int i = 0; i < 1; i++)
				comb[i+check][4] = 'a';
			
			for(int j = 1; j < 2; j++ )
				comb[j+check][4] = 'b';
			
			for(int k = 2; k < 3; k++)
				comb[k+check][4] = 'c';
			
			count++;
			check+=3;
		}
		int palinCount = 0;
		char[][] partB = new char[250][5];
		char[][] partC = new char[250][5];
		
		int partB_count = 0;
		int partC_count = 0;
		
		// Part A: Count all Palindromes
		
		for(int i = 0; i < 243; i++)
		{
			if((comb[i][0] == comb[i][4]) && (comb[i][1] == comb[i][3]))
			{
				palinCount++;
				
				// Part B: Print all Palindromes where letter appears more than twice
				
				if((comb[i][0] == comb[i][1]) || (comb[i][0] == comb[i][2]) || (comb[i][1] == comb[i][2]))
				{
					for(int j = 0; j < 5; j++)
						partB[partB_count][j] = comb[i][j];
					
					partB_count++;
				}
				else
				{
					for(int j = 0; j < 5; j++)
						partC[partC_count][j] = comb[i][j];
					
					partC_count++;	
				}
			}				
		}
		
		System.out.println("Part A: There are " + palinCount + " palindromes.");
		System.out.println("Part B: ");
		
		for(int i = 0; i < partB_count; i++)
		{
			for(int j = 0; j < 5; j++)
				System.out.print(partB[i][j]);
			
			System.out.println();
		}
		

		System.out.println();		
		System.out.println("Part C: ");
		
		for(int i = 0; i < partC_count; i++)
		{
			for(int j = 0; j < 5; j++)
				System.out.print(partC[i][j]);
			
			System.out.println();
		}

		
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Code explanation

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Code explanation

Posted 10 October 2012 - 11:39 AM

http://www.dreaminco...post__p__392359
Was This Post Helpful? 1
  • +
  • -

#3 Portista90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-October 12

Re: Code explanation

Posted 10 October 2012 - 11:47 AM

Thanks, I pretty much understand for and while loops in general but this exact code I want some helping breaking down.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Code explanation

Posted 10 October 2012 - 11:56 AM

The while(count < 1) is completly useless
before the while() count == 0 so we enter the loop
in the loop count++; set count == 1 so we will exit the loop
so sure you can remove the while() and its ending }

the for() puts
'a' in comb[0][0] to comb[80][0]
'b' in comb[81][0] to comb[161][0]
'c' in comb[162][0] to comb[241][0]
Was This Post Helpful? 1
  • +
  • -

#5 Portista90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-October 12

Re: Code explanation

Posted 10 October 2012 - 02:39 PM

View Postpbl, on 10 October 2012 - 11:56 AM, said:

The while(count < 1) is completly useless
before the while() count == 0 so we enter the loop
in the loop count++; set count == 1 so we will exit the loop
so sure you can remove the while() and its ending }

the for() puts
'a' in comb[0][0] to comb[80][0]
'b' in comb[81][0] to comb[161][0]
'c' in comb[162][0] to comb[241][0]

Thanks alot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1