# problem !!

• August 30th, 2010, 04:50 AM
mister0
problem !!
hi ... i'm starting to solve usaco problems and i'm a beginner....
so the problem is easy .... or hard i dont know :)

for the first program (friday 13th) it gives me an output of array with zeros which i tried to debug with (print) to see the variables' changes (as i learned here) and my conclusion is that the loop (for) doesn't work !! and this is the part that i can't solve really .....
for the second program the greedy gift giver it gives an empty output.....
thx

Code Java:

```package friday;   import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.*;   public class friday {   /** * @param args the command line arguments */ public static void main(String[] args)throws IOException {   int[] array = new int[7] ; int ini = 3 ; int a=0 , b , c , d , e , f , g , h ; int n , i ; Scanner s = new Scanner(new File("friday.in")); PrintWriter pw = new PrintWriter(new File("friday.out")); n = s.nextInt() ; for (i=0 ; i == n-1 ; i++ ) { if ((i%4)==0) { a= ((ini+5) % 7) ; b = ((ini+9)%7) ; c = ((ini+4)%7) ; d = ((ini+1)%7) ; e = ((ini+7)%7) ; f= ((ini+3)%7) ; array[a] = array[a]+1 ; array[b] = array[b]+1 ; array[c] = array[c]+4; array[d] = array[d]+1 ; array[e] = array[e]+4 ; array[f] = array[f]+1 ; ini = (ini + 6)%7 ;   } else a= ((ini+5) % 7) ; b = ((ini+9)%7) ; c = ((ini+4)%7) ; d = ((ini+1)%7) ; e = ((ini+7)%7) ; f= ((ini+3)%7) ; g = ((ini+6)%7) ; h = ((ini+2)%7) ; array[a] = array[a]+1 ; array[b] = array[b]+1 ; array[f] = array[f]+2 ; array[e] = array[e]+3 ; array[g] = array[g]+4 ; array[h] = array[h]+1 ; ini = (ini + 5)%7 ; } a= ((ini+5) % 7) ; pw.print(array[0] + " " + array[1]+ " " + array[2]+ " " + array[3]+ " " + array[4]+ " " + array[5]+ " " + array[6]+ " ") ;   pw.print(a) ; pw.close(); s.close() ; } }```
and now Greedy gift giver program !!

Code Java:

```import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.*;   public class gift1 {   public static void main(String[] args) throws IOException { Scanner s = new Scanner(new File("gift1.in")); PrintWriter pw = new PrintWriter(new File("gift1.out")); String p , giver = null , taker ; int i , np , k , j ,h,total, no , calc,temp; np = s.nextInt() ; String names[] = new String[np] ; Hashtable <String , Object> table = new Hashtable<String, Object>(); for (i = 2; i == (np + 1); i++) { p = s.next(); names[i-2] = p ; table.put(p,0) ; }   for (k = 1; k == np; k++) { giver = s.next() ; total = s.nextInt(); no = s.nextInt() ; for (j=1 ; j == no ; j++){ if (no==0) break ; else taker = s.next() ; calc = ((total-(total%no))/no) ; temp = Integer.parseInt(table.get(taker).toString()) ; temp = temp + calc ; table.put(taker , temp) ; temp = Integer.parseInt(table.get(taker).toString()) ; temp = temp - calc ; table.put(giver , temp) ; } } for(h=0 ; h<np ; h++ ) { pw.print(names[h]) ; giver = names[h] ; pw.print(table.get(giver)) ; } pw.close(); s.close(); }   }```
• August 30th, 2010, 05:10 AM
Brt93yoda
Re: problem !!
Welcome to the forums! When you post code surround it in [highlight=java][/highlight] tags, it makes your code alot easier. It looks like the following line is your problem.
Code java:

`Scanner s = new Scanner(new File("friday.in"));`

If you're trying to get the next int from a user then use:

Code java:

`Scanner s = new Scanner(System.in);`

If your code is correct, then forgive me. It's 3am. I'll check your code again when I wake up.
• August 30th, 2010, 09:38 AM
Norm
Re: problem !!
Try debugging your code by adding some more println()s. For example print out the values of a,b,c ... h inside of the loops.

Also it helps when you have an error with the program's output to post the output here.
• August 30th, 2010, 11:18 AM
aussiemcgr
Re: problem !!
I remember doing those. Fun times...

Friday: I immediately see a bracket problem. Check that and we will work from there.
Greedy: I also see a bracket problem. Check that and we can work from there aswell.

@Brt93yoda: USACO uses input text files with the extensions .in and output text files with the extension .out. He is reading a file, not the console.
• August 30th, 2010, 04:25 PM
mister0
Re: problem !!
first of all thanks to u both .....
@Brt93yoda: as aussiemcgr said :) any way thanks
@norm : i tried and the values out side the for loop are correct ...inside the loop are printed zeros and that what i dont know why ....i will try again
@aussiemcgr : i dont think the brackets r problem as the two codes compiled successfully with me ....dont know !!
• August 30th, 2010, 04:45 PM
aussiemcgr
Re: problem !!
Ok, I think I was reading it wrong since its hard to read the code like that.

Specifically where in the for loops do you print out the values? If we know exactly when they are 0, it is more helpful when debugging.
• August 30th, 2010, 04:47 PM
mister0
Re: problem !!
after incrementing the array elements
• August 30th, 2010, 04:57 PM
Norm
Re: problem !!
Quote:

values out side the for loop are correct ...inside the loop are printed zeros
Please post the print out from the program and post the new code with the println() statements.
Be sure to put the code in code tags.
• August 30th, 2010, 07:15 PM
aussiemcgr
Re: problem !!
Ok, help me read through this.
True or False (from Friday):
Quote:

for (i=0 ; i == n-1 ; i++ )
This statement will only run once, since n is not reevaluated, and only if n equals 1 since i must equal 0 initially and n-1 must equal 0 for it to return true.
• August 31st, 2010, 01:27 AM
Brt93yoda
Re: problem !!
Quote:

Originally Posted by aussiemcgr
Ok, help me read through this.
True or False (from Friday):

This statement will only run once, since n is not reevaluated, and only if n equals 1 since i must equal 0 initially and n-1 must equal 0 for it to return true.

He actually did that quiet a few times. I wonder if he means <=?

Code java:

```for (i = 2; i == (np + 1); i++) { // Here p = s.next(); names[i-2] = p ; table.put(p,0) ; }   for (k = 1; k == np; k++) { // Here giver = s.next() ; total = s.nextInt(); no = s.nextInt() ; for (j=1 ; j == no ; j++){ //Here if (no==0) break ; else taker = s.next() ; calc = ((total-(total%no))/no) ; temp = Integer.parseInt(table.get(taker).toString()) ; temp = temp + calc ; table.put(taker , temp) ; temp = Integer.parseInt(table.get(taker).toString()) ; temp = temp - calc ; table.put(giver , temp) ; } }```
• August 31st, 2010, 05:06 PM
mister0
Re: problem !!
np >>> is the number of people that the problem give which is between1 to 10
but no << urright ... it might be zero .... checkin again ..thanks
• September 3rd, 2010, 07:01 AM
mister0
Re: problem !!
Quote:

Originally Posted by Brt93yoda
He actually did that quiet a few times. I wonder if he means <=?

Code java:

```for (i = 2; i == (np + 1); i++) { // Here p = s.next(); names[i-2] = p ; table.put(p,0) ; }   for (k = 1; k == np; k++) { // Here giver = s.next() ; total = s.nextInt(); no = s.nextInt() ; for (j=1 ; j == no ; j++){ //Here if (no==0) break ; else taker = s.next() ; calc = ((total-(total%no))/no) ; temp = Integer.parseInt(table.get(taker).toString()) ; temp = temp + calc ; table.put(taker , temp) ; temp = Integer.parseInt(table.get(taker).toString()) ; temp = temp - calc ; table.put(giver , temp) ; } }```

np >>> is the number of people that the problem give which is between1 to 10
but no << urright ... it might be zero .... checkin again ..thanks