Results 1 to 7 of 7

Thread: Arrays help

  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Arrays help

    The Problem:
    How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
    My Code:
    Java Code:
    public class Problem019 {
    	public static void main (String[] args){
    		
    		//System.out.println(LeapYearTest(1996));
    		int ThirtyOne = 31;
    		int Thirty = 30;
    		int FebNorm = 28;
    		int FebLeap = 29;
    		int a, c, Day, e = 0, f = 0;
    		int Calander[] []= new int [12] [] ;
    		
    		Calander[0] = new int [ThirtyOne];
    		Calander[1] = new int [FebNorm];
    		Calander[2] = new int [ThirtyOne];
    		Calander[3] = new int [Thirty];
    		Calander[4] = new int [ThirtyOne];
    		Calander[5] = new int [Thirty];
    		Calander[6] = new int [ThirtyOne];
    		Calander[7] = new int [ThirtyOne];
    		Calander[8] = new int [Thirty];
    		Calander[9] = new int [ThirtyOne];
    		Calander[10] = new int [Thirty];
    		Calander[11] = new int [ThirtyOne];
    		
    		for (a=1900;a<2001;a++){
    			if (LeapYearTest(a)){
    				Calander[1] = new int [FebLeap];
    				//System.out.println(LeapYearTest(a));
    			}
    			else Calander[1] = new int [FebNorm];
    			
    			for (e=0;e<Calander.length;e++)
    				f=0;
    				for (Day=0;Day<7;Day++){
    					//System.out.println(Day);
    					f+=Day;
    					while (f<20){			//Want to put Column Length here
    					++Calander[0][0];
    					System.out.println(Calander[0][f]); //How to add 1 to array everytime it counts to 7?
    					}
    					
    				}
    			//System.out.println(a);
    		}
    		
    	}
    
    	public static boolean LeapYearTest(int x){
    		if (x%4==0 || x%400==0){
    			return true;
    		}
    		if (x%100==0){
    			return false;
    		}
    		else return false;
    	}
    	
    }
    I'm getting very confused with all these loops.

    What I want to do, is have a counter going from 1-7, down to the end of each column, from left to right across rows. And then each time it counts to 7, mark/add 1 to that spot in the array. And then have a counter that counts every time that I completely pass through the array.

    I don't know how to get the length of a column, and I'm just not sure if this is the right approach to get the answer to this problem?

    Thanks!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Arrays help

    I think the program that you are writing doesn't solve the problem of counting number of Sundays that fell on the first of January between 1901 and 2000 :D

  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Arrays help

    So I can't make it count from 0 to 6 while <Column length, and if it finishes a column when it's at say, 3, start the next column at 4? Should I use an array at all?

    EDIT: Ohhh, lol, I realise I said 1-7 before. I mean count 7 times.

    There is no .length for an array column is there? I think I will have to write a method that returns the length of a column because I can't find an easy way to get it.
    Last edited by Zigster; 04-27-2012 at 08:49 AM.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Arrays help

    Back to your problem:

    How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
    How is the code that you wrote above can find number of Sundays between the year 1901 and 2000?

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Arrays help

    Well, it can't atm.

    My theory is, I loop through the array 100 times, and I loop through the array by counting 7 times, and on each 7th count I add 1 to that point on the array. And then at the end, I sum row 0, and that's how many Sundays are on the first of the month.

  6. #6
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Arrays help

    So this is my new code:
    Java Code:
    public class Problem019 {
    	public static void main (String[] args){
    		
    		//System.out.println(LeapYearTest(1996));
    		int ThirtyOne = 31;
    		int Thirty = 30;
    		int FebNorm = 28;
    		int FebLeap = 29;
    		int a, b, c, Day, e = 0, f = 0;
    		int Calander[] []= new int [12] [] ;
    		
    		Calander[0] = new int [ThirtyOne];
    		Calander[1] = new int [FebNorm];
    		Calander[2] = new int [ThirtyOne];
    		Calander[3] = new int [Thirty];
    		Calander[4] = new int [ThirtyOne];
    		Calander[5] = new int [Thirty];
    		Calander[6] = new int [ThirtyOne];
    		Calander[7] = new int [ThirtyOne];
    		Calander[8] = new int [Thirty];
    		Calander[9] = new int [ThirtyOne];
    		Calander[10] = new int [Thirty];
    		Calander[11] = new int [ThirtyOne];
    		
    		for (a=1901;a<2001;a++){
    			//System.out.println(a);
    			if (LeapYearTest(a))
    			{
    				Calander[1] = new int [FebLeap];
    				//System.out.println(LeapYearTest(a));
    				//System.out.println(Calander[1].length);
    			}
    			else
    			{
    				Calander[1] = new int [FebNorm];
    			}
    			//System.out.println(Calander[1].length);
    			
    			for (e=0;e<Calander.length;e++)
    			{	
    				System.out.println("e: " + e);
    				f=0;
    				while (f<Calander[e].length)
    				{
    					//System.out.println(Calander[e].length);
    					for (Day=0;Day<7;Day++)
    					{	
    						if (f<Calander[e].length)
    						{
    							f++;
    							System.out.println("f: " + f);
    							//System.out.println(f);
    							//System.out.println(Day);
    							//System.out.println(Calander[e][f]);
    						}
    						else
    						{
    							break;
    						}
    					}
    					if (f<Calander[e].length)
    					{
    					++Calander[e][f];
    					//System.out.println(Calander[e][f]);
    					}
    				}
    			}
    			//System.out.println(a);
    		}
    		for (b=0;b<Calander.length;b++)
    		{	
    			System.out.print(Calander[0][b]);
    		}
    	}	
    	
    
    	public static boolean LeapYearTest(int x)
    	{
    		if (x%4==0 || x%400==0){
    			return true;
    		}
    		if (x%100==0){
    			return false;
    		}
    		else return false;
    	}
    	
    }
    The answer it gives me is wrong, when it prints out Calander row 0 at the end it prints: 00000001000000

    I think there must be something wrong with line 64: ++Calander[e][f]; (It's supposed to add 1 to Calander [e][f] everytime day counts to 7).

    But I don't know what or how to fix it?

    Cheers!
    Last edited by Zigster; 04-28-2012 at 05:22 AM.

  7. #7
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Arrays help

    I'm still trying to figure this one out, I can't get my head around these loops.

    This is how I want it to work, where the first column is a column of the array, the second column counts the length of the column, and the third column counts 7 times, and then on the 7th count (Sunday) adds 1 into the array. And the most important part, is that when it moves to the next column in the array, the counting to 7 continues from the previous column, it doesn't restart.

    Column 0 in Array:

    0 0 1
    0 1 2
    0 2 3
    0 3 4
    0 4 5
    0 5 6
    1 6 7 ++
    0 7 1
    0 8 2
    0 9 3
    0 10 4
    0 11 5
    0 12 6
    1 13 7 ++
    0 14 1
    0 15 2
    0 16 3
    0 17 4
    0 18 5
    0 19 6
    1 20 7++
    0 21 1
    0 22 2
    0 23 3
    0 24 4
    0 25 5
    0 26 6
    1 27 7 ++
    0 28 1
    0 29 2
    0 30 3

    Column 1:

    0 1 4
    0 2 5
    1 3 6
    0 4 7++
    0 5 1
    0 6 2

    etc, etc.

    Code:
    Java Code:
    public class Problem019 {
        public static void main (String[] args){
             
            //System.out.println(LeapYearTest(1996));
            int ThirtyOne = 31;
            int Thirty = 30;
            int FebNorm = 28;
            int FebLeap = 29;
            int a, b, c, Day, e = 0, f = 0;
            int Calander[] []= new int [12] [] ;
             
            Calander[0] = new int [ThirtyOne];
            Calander[1] = new int [FebNorm];
            Calander[2] = new int [ThirtyOne];
            Calander[3] = new int [Thirty];
            Calander[4] = new int [ThirtyOne];
            Calander[5] = new int [Thirty];
            Calander[6] = new int [ThirtyOne];
            Calander[7] = new int [ThirtyOne];
            Calander[8] = new int [Thirty];
            Calander[9] = new int [ThirtyOne];
            Calander[10] = new int [Thirty];
            Calander[11] = new int [ThirtyOne];
             
            for (a=1901;a<2001;a++){
                //System.out.println(a);
                if (LeapYearTest(a))
                {
                    Calander[1] = new int [FebLeap];
                    //System.out.println(LeapYearTest(a));
                    //System.out.println(Calander[1].length);
                }
                else
                {
                    Calander[1] = new int [FebNorm];
                }
                //System.out.println(Calander[1].length);
                 
                while (e<Calander.length)
                {   
                    //System.out.println("e: " + e);
                    f=0;
                    while (f<Calander[e].length)
                    {	
                    	
                        //System.out.println(Calander[e].length);
                    	Day=1;
                        while (Day<8 && f<Calander[e].length)
                        {   
                        	System.out.println("f: " + f + "\tDay: " + Day + "\tCalander[e][f]: " + Calander[e][f]);
                        	Day++;
                        	f++;
                        	
                        	if (f==Calander[e].length)
                        	{
                        	e++;
                        	}
                        	if (f<Calander[e].length && f!=0 && Day==7)
                            {
                            Calander[e][f]=Calander[e][f] + 1;
                            }
                        }
                        
                    }
                }
                //System.out.println(a);
            }
            for (b=0;b<Calander.length;b++)
            {   
                System.out.print(Calander[0][b]);
            }
        }   
         
     
        public static boolean LeapYearTest(int x)
        {
            if (x%4==0 || x%400==0){
                return true;
            }
            if (x%100==0){
                return false;
            }
            else return false;
        }
         
    }
    Prints:
    f: 0 Day: 1 Calander[e][f]: 0
    f: 1 Day: 2 Calander[e][f]: 0
    f: 2 Day: 3 Calander[e][f]: 0
    f: 3 Day: 4 Calander[e][f]: 0
    f: 4 Day: 5 Calander[e][f]: 0
    f: 5 Day: 6 Calander[e][f]: 0
    f: 6 Day: 7 Calander[e][f]: 1
    f: 7 Day: 1 Calander[e][f]: 0
    f: 8 Day: 2 Calander[e][f]: 0
    f: 9 Day: 3 Calander[e][f]: 0
    f: 10 Day: 4 Calander[e][f]: 0
    f: 11 Day: 5 Calander[e][f]: 0
    f: 12 Day: 6 Calander[e][f]: 0
    f: 13 Day: 7 Calander[e][f]: 1
    f: 14 Day: 1 Calander[e][f]: 0
    f: 15 Day: 2 Calander[e][f]: 0
    f: 16 Day: 3 Calander[e][f]: 0
    f: 17 Day: 4 Calander[e][f]: 0
    f: 18 Day: 5 Calander[e][f]: 0
    f: 19 Day: 6 Calander[e][f]: 0
    f: 20 Day: 7 Calander[e][f]: 1
    f: 21 Day: 1 Calander[e][f]: 0
    f: 22 Day: 2 Calander[e][f]: 0
    f: 23 Day: 3 Calander[e][f]: 0
    f: 24 Day: 4 Calander[e][f]: 0
    f: 25 Day: 5 Calander[e][f]: 0
    f: 26 Day: 6 Calander[e][f]: 0
    f: 27 Day: 7 Calander[e][f]: 1
    f: 28 Day: 1 Calander[e][f]: 0
    f: 29 Day: 2 Calander[e][f]: 0
    f: 30 Day: 3 Calander[e][f]: 0
    f: 0 Day: 1 Calander[e][f]: 0

    f: 1 Day: 2 Calander[e][f]: 0
    f: 2 Day: 3 Calander[e][f]: 0
    f: 3 Day: 4 Calander[e][f]: 0
    f: 4 Day: 5 Calander[e][f]: 0
    How can I have the day count continue when it switches to the next column?

    Cheers.
    Last edited by Zigster; 04-28-2012 at 02:25 PM.

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

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