1. ## Alphan's Cinema...

Hi mates. I have to do write a programme for this algorithm. I think i wrote right but the output isnt right. First i'll explain the algorithm.
Alphan's cinema is a corridor. The audiences a little bit annoyed because of couldnt see the screen very well. Every angry level of an audience is equal to the number of taller people sitting in front of her. Alphan are curious about the lenghts of the audiences, but he can not see because of the room is dark. Requested from you is to find lenghts of the audience in every seat acording to the angry levels given.
*Each client's level of angry minimum 0, maximum N-1.
*Each client's length is between 1 and N (1 and N included) is an integer and there is no any two of the customers have a same lenght.
*First line of the cinema.txt is number of person/seats N
*The second line of the cinema.txt is N angry levels of the customers according to start the nearest one to cinema screen and there are spaces between them.
Now i'll write my codes.
Java Code:
```import java.io.* ;

public class cinema {

public static void main (String args[]) {

String ln ;
String[] number ;
String[] input = new String[ 2 ] ;
int[] angry,  lng,  output,  temp ;
int person,  size ;
boolean[] deleteItem ;

try {

for ( int j = 0 ; j < input.length ; j++ ) {
input[ j ] = br.readLine() ;
}

ln = input[ 1 ] ;
person = Integer.parseInt( input[ 0 ] ) ;

String delimiter = " " ;
number = ln.split( delimiter ) ;

angry = new int[ number.length ] ;

for (int i = 0 ; i < number.length ; i++ ) {
angry[ i ] = Integer.parseInt( number[ i ] ) ;
}

lng = new int[ person ] ;
for ( int k = 0 ; k < person ; k++ ) {
lng[ k ] = k + 1 ;
}

output = new int[ person ] ;
size = person ;

for ( int m = person - 1 ; m >= 0 ; m-- ) {

int q = angry[ m ] ;
output[ m ] = lng[ m - q ] ;
deleteItem = new boolean[ size ];
for ( int k = 0 ; k < size ; k++ ) {
deleteItem[ k ] = false ;
}

deleteItem[ m - q ] = true ;

temp = new int[ size ] ;
for ( int w = 0 ; w < size ; w++ ) {
temp[ w ] = lng[ w ] ;
}

size-- ;
lng = new int[ size ] ;

int index = 0 ;
for ( int e = 0 ; e < temp.length ; e++ ) {
if ( ! deleteItem[ e ]) {
lng[ index++ ] = temp[ e ];
}
}
}

for (int r = 0 ; r < person ; r++ ) {
System.out.println( output[ r ] ) ;
}
}

catch(IOException e) {
System.out.println( "Error: " +  e ) ;
}
}
}```
sorry about my bad english and confusing. i checked over and over again. every time, it seems to right but it isnt. i dont understand where is mistake. please help...
Last edited by remus19; 06-03-2011 at 05:16 AM. Reason: indentation of code and solving the error

2. Originally Posted by remus19
I think i wrote right but the output isnt right.
Obviously you didn't write it correctly then.

Neither do we. It would help if you provided information about what isn't right. Do you get compiler errors? Do you get runtime errors? If so, copy and paste the EXACT error message. Do you get incorrect output? Then show what your input is, what your expected output is and what the actual output is.

3. Originally Posted by Junky
ofcourse! sorry, my mistake. you are right.
my input file is this:
4
0 0 1 3
the true output must be this:
2 4 3 1
i get runtime error here it is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at sinema.main(sinema.java:54)
the 54th line is lng[index++]=temp[e];
also i added a try-catch there. and i get 0 0 3 1.
then i wrote System.out.println("Error: " + e1); under the catch, i get that:
Error: java.lang.ArrayIndexOutOfBoundsException: 3
Error: java.lang.ArrayIndexOutOfBoundsException: 2
Error: java.lang.ArrayIndexOutOfBoundsException: 3
Error: java.lang.ArrayIndexOutOfBoundsException: 2
Error: java.lang.ArrayIndexOutOfBoundsException: 3
0
0
3
1

4. The indentation of your code is all over the place like a mad womans knitting. This makes it hard to read.

Do you know what an ArrayIndexOutOfBoundsException is? If you create an array with a length of 3 then the valid indicies are 0, 1 & 2. The element at index 3 does not exist but your code is trying to access it. The error message tells you on which line it occurs, 54. You need to do some debugging and find out why you are trying to access an element in the array at a position that does not exist.

5. ok i solved the array index out of bounds error. but i am still getting the wrong output: 0 0 3 1. the only difference is this time there was no error.
i tried to fix indentation of code. please ask me if there is anywhere in code or algorithm you cant understand. because i really need help. my partner's java information is worse than me.

6. i tried to fix indentation of code.
Still not good.
Code Conventions for the Java(TM) Programming Language: Contents

db

8. i solved the problem thanks for help. now i getting the right result.
i have one last problem. if i can transform the code to recursion this will be better.

9. Originally Posted by remus19
i solved the problem thanks for help. now i getting the right result.
i have one last problem. if i can transform the code to recursion this will be better.
And you have our blessings to go ahead and try this. Please feel free to come on back if you have an answerable question.

#### Posting Permissions

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