CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    8

    Need help with arrays

    So im trying to find the min number in an array. Seems simple but I keep getting an error

    Here is my code.

    private int findMin(int numbers[]) {
    //PRE numbers.length >= 1

    int min = numbers [0];
    for (int i = 0; i > numbers.length; i++); {
    if (numbers [i] < min) {
    min = numbers [i];
    }
    }
    return min;
    }

    I keep getting the error (cannot find symbol - variable i). Am I missing something. Would greatly appreciate any help

  2. #2
    Join Date
    Nov 2011
    Posts
    8

    Re: Need help with arrays

    I think I might have figured it out. Am I just missing a () after numbers.length?



    My second question though is, is this how you build an array? I dont have any notes on it or could I find any good tuts online.

    private int[] buildArray(String line) {
    int[]arr = new int[line.length()];
    arr[0] = 0;
    for (int i = 1; i < line.length(); i++){
    arr[i] = Character.digit(line.charAt(i),10);

    }
    return arr;
    }

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help with arrays

    Please use code tags when posting code

    I think I might have figured it out. Am I just missing a () after numbers.length?
    Don't just guess an answer and ask us to check if it is correct. Does it now compile and run, if so you have figured it out?

    Hint: The answer is NO.

    'i' is declared in the for statement and so is only visible in the for statement and its code block. However you have put a semi-colon after the for loop which terminates the code black and so the code following the semi-colon has nothing to do with the for loop.

    BTW you may want to consider if you really want the for loop to continue whilst 'i' is greater than the length of the number array.

    My second question though is, is this how you build an array?
    Again, it is up to you to compile it, run it and see what the output is. If you can't get it to work feel free to ask for help.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

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