Results 1 to 9 of 9
  1. #1
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Question 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 {
                
                    FileReader fr = new FileReader( "cinema.txt" ) ;
                    BufferedReader br = new BufferedReader( fr ) ;
                
    	        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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

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

    i dont understand where is mistake. please help...
    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.

    The more information you provide it makes it easier for others to help you.

  3. #3
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The more information you provide it makes it easier for others to help you.
    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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    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. #5
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    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. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

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

    db

  7. #7
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    what about now?

  8. #8
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by remus19 View Post
    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.

Similar Threads

  1. Need some help with kode, Cinema
    By spot in forum Java Applets
    Replies: 2
    Last Post: 11-09-2009, 06:38 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
  •