1. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
10

## 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. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
10

## Re: Arrays help

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. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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.

#### Posting Permissions

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