CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: arrays problem

  1. #1
    Join Date
    Aug 2009
    Posts
    52

    arrays problem

    I'd like to know if by initializing an array it would give you 0000000? when no values are stored
    For example:

    final static int [] array = new int[11];
    By initializing if i display them out it would look like this in my program.
    [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
    0 0 0 0 0 0 0 0 0 0 0

    Question is So if after initializing they become 0, what if a user inserts a value 0 into the array[1] then how do you differentiate it between a stored 0 and an initialized 0. Im quite confused.


    ---------------------------------------------------------------------------------------------
    This is how i did my Insert Method:
    Im actually searching for the key 0(since it is 0 when initialized) then insert a number into it. I dont know if doing this is actually correct?

    Code:
    public static void insert(int[] array, int n) {
             int nElems = 11; // number of items
             int j; // loop counter
             long searchKey; // key of item to search for
             Scanner sc = new Scanner(System.in);
             System.out.print("Enter new number: ");
             n = sc.nextInt();
             
             searchKey = 0; // find item with key 0
             for(j=0; j<nElems; j++) // for each element,
                if(array[j] == searchKey) // found item?
                   break; // yes, exit before end
             if(j>array.length-1) // at the end?
                System.out.println("Array Full"); // yes
             else if(j !=nElems) 
                System.out.println("Found" + searchKey); // no
             else if (j == nElems)
                System.out.println("Error Number Not Found"+searchKey);
          //--------------------------------------------------------------
             array[j]=n; //store

  2. #2
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: arrays problem

    Quote Originally Posted by hugo84 View Post
    I'd like to know if by initializing an array it would give you 0000000? when no values are stored
    For example:

    final static int [] array = new int[11];
    By initializing if i display them out it would look like this in my program.
    [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
    0 0 0 0 0 0 0 0 0 0 0

    Question is So if after initializing they become 0, what if a user inserts a value 0 into the array[1] then how do you differentiate it between a stored 0 and an initialized 0. Im quite confused.


    ---------------------------------------------------------------------------------------------
    This is how i did my Insert Method:
    Im actually searching for the key 0(since it is 0 when initialized) then insert a number into it. I dont know if doing this is actually correct?

    Code:
    public static void insert(int[] array, int n) {
             int nElems = 11; // number of items
             int j; // loop counter
             long searchKey; // key of item to search for
             Scanner sc = new Scanner(System.in);
             System.out.print("Enter new number: ");
             n = sc.nextInt();
             
             searchKey = 0; // find item with key 0
             for(j=0; j<nElems; j++) // for each element,
                if(array[j] == searchKey) // found item?
                   break; // yes, exit before end
             if(j>array.length-1) // at the end?
                System.out.println("Array Full"); // yes
             else if(j !=nElems) 
                System.out.println("Found" + searchKey); // no
             else if (j == nElems)
                System.out.println("Error Number Not Found"+searchKey);
          //--------------------------------------------------------------
             array[j]=n; //store
    i don't think u can use n like this..n is parameter and must get when u call method..but u try get it after u call method..did it work..

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  3. #3
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: arrays problem

    ohh..maybe u send value..but i did not think like that.. but it seems it is not required like that..post your full code..
    Last edited by holestary; September 21st, 2009 at 05:59 AM.

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  4. #4
    Join Date
    Aug 2009
    Posts
    52

    Re: arrays problem

    Yes it works fine

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: arrays problem

    When using int 0 is always 0, whether it is the initial value or was input by the user or read from a file, etc. Using just the int variable, there is no way of identifying where that value came from.

    If it is important to determine if a variable's certain value was entered by the user (or any other means) as opposed to been the initial value, you have a few alternatives:
    • use an initialisation value that is impossible to be entered by the user
    • use a boolean to indicate the variable initial value has been changed
    • use an enum to stablish the variable's state (this could be extended to several other states, rather than just initialised and entered by the user)
    • use Integer instead of int (initially null, if it has an integer value it must have been entered by the user)


    Regarding holestary's post, assigning a new value to a parameter passed to the method will work, but the question is why passing the value n if then you are not using it and you assign a new value? Why not using a local variable for that?

    Another question: why is searchKey declared as long when you use it to search for an int value?

    An yet another one: why not using a set instead of an array?

  6. #6
    Join Date
    Aug 2009
    Posts
    52

    Re: arrays problem

    OK. so what im seeing now when i initialize an array i would see 000000000000 is correct.
    About that long for searchKey sorry my bad.
    About not using set and all those, sorry its an assignment i had to go by the guidelines.

    --------------------------------------------------------------------------------------
    Another problem:
    When i do a remove my method would remove all numbers containing searchKey. How do i delete only the first impression of that number

    EG:
    After insertion
    1,2,3,4,5,6,6,6,8

    I want to delete 6, from my codes it would look for all that contains 6 and delete them all
    After deletion
    1,2,3,4,5,8

    What is want is:
    After deletion
    1,2,3,4,5,6,6,8
    Last edited by hugo84; September 21st, 2009 at 11:40 AM.

  7. #7
    Join Date
    Aug 2009
    Posts
    52

    Re: arrays problem

    Quote Originally Posted by holestary View Post
    ohh..maybe u send value..but i did not think like that.. but it seems it is not required like that..post your full code..
    Yes n is actually in my main method switch case when i pass in the number to the method. Sorry for the messiness i have arleady clean it up. Thanks for the reminder

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: arrays problem

    Quote Originally Posted by holestary View Post
    i don't think u can use n like this..n is parameter and must get when u call method..but u try get it after u call method..did it work..
    Of course you can use it - it's not particularly Good Practice, but there's nothing to stop you, unless the parameter is declared 'final'.

    Why not just try it out before posting?

    They know enough who know how to learn...
    J. Adams
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: arrays problem

    Quote Originally Posted by hugo84 View Post
    How do i delete only the first impression of that number
    Exit the loop immediately after resetting the number (i.e. in the 'if' block). This points to the value of always using curly braces {} with control statements like 'if'.

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  10. #10
    Join Date
    Aug 2009
    Posts
    52

    Re: arrays problem

    Quote Originally Posted by dlorde View Post
    Exit the loop immediately after resetting the number (i.e. in the 'if' block). This points to the value of always using curly braces {} with control statements like 'if'.

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Thanks for your pointer. Works now.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured