Results 1 to 7 of 7
  1. #1
    kri
    kri is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Question problems with functions

    ok, so i'm having some issues in writing this program, i would explain but it would be way too long. but either way... the second and third function are not working properly. the second one should print the same numbers as the first, just in reverse. the third function should add the same numbers up. i'm losing my mind trying to get it. any help would be awesome.

    thanks:confused:


    import java.util.Scanner;

    public class lab23a{
    public static int [] ar1;

    public static int randomarray(){ //array normal
    int i, ar1[];
    ar1 = new int[10];

    System.out.println("Array size is set by the Constant of 10.");
    System.out.println("10 Random numbers into the array.");
    System.out.println("Display the numbers in the array.");
    for(i=0; i<10; i++){
    ar1[i]= (int)(Math.random()*1000);
    System.out.print(ar1[i] + " ");

    }
    return 0;}//for (random numbers)


    public static int printarrayreverse(){ //reverse array
    int i, ar1[];
    ar1 = new int[10];
    System.out.println("\nDisplay the same numbers in reverse.");

    for(i=0; i < 10; i++){
    System.out.print(ar1[9-i] + " ");

    }
    return 0;}//while (random numbers in reverse)


    public static int sumit(){
    int x1, ar1[], i;
    ar1 = new int[10];
    x1=0;
    for(i=0; i<10; i++){
    x1=ar1[i]+x1;

    }//for (sum)
    System.out.println("\nThe sum is: " + x1);
    return 0;
    }

    public static int searcharray(){
    int key1, flag, ar1[], i;
    ar1 = new int[10];
    Scanner sc = new Scanner(System.in);
    System.out.print("\nEnter search key: ");
    key1 = sc.nextInt();
    flag=0;
    for(i = 0; i < 10; i++){
    if(ar1[i]==key1){
    flag=1;
    }//if1


    }//for
    if(flag==0){
    System.out.println("Number not found");
    }
    if(flag==1){
    System.out.println("Number found");
    }
    System.out.print("DONE!");
    return 0;}
    public static void main(String [] args){
    randomarray();
    printarrayreverse();
    sumit();
    searcharray();
    }

    }
    Last edited by kri; 02-07-2009 at 06:46 AM. Reason: adding script

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    A couple things...
    1) If you're going to program in Java, you have to use the lingo... they're not functions (in C they're functions)... in Java they're called methods.
    2) You can't expect people to help you by throwing a bunch of code at them and not even bothering to explain what the problem is.

    I suggest you rethink your approach to the problem. First explain what the problem is with the second method:
    • What doesn't work?
    • Does it compile?
    • What results do you get vs what is expected?

    Once the second method is working, then move on to the third method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    kri
    kri is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    technically it works, rather, it runs. when i run it the second method (reverse order) just prints out ten "0's". it should print out the same random numbers as the first just in reverse. the third method just prints out one "0" when it should sum up all the numbers in the first method.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Here we go...

    OK... do you agree that you have to call the randomArray method before you can call the printarrayreverse method (because the array is created in the first method).
    With that thought in mind, then you shouldn't be initializng the array again in the second method:
    Java Code:
    int i, [B][COLOR="Red"]ar1[];[/COLOR][/B]
    [B][COLOR="red"]ar1 = new int[10];[/COLOR][/B]
    The array is already being declared globally:
    Java Code:
    public static int [] ar1;
    Luck,
    CJSL
    Last edited by CJSLMAN; 02-07-2009 at 04:09 AM.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    kri
    kri is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    alright, that makes sense. so i fixed that and i found that the search method only finds 0. so i think i'm all screwed up.

  6. #6
    kri
    kri is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    never mind on the whole thing. i got it. thanks for the help.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default No problem...

    You're welcome...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Functions in jsp
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-25-2009, 11:04 PM
  2. HashMap functions get vs put
    By tahni in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 07:55 PM
  3. print without built functions
    By j2vdk in forum New To Java
    Replies: 19
    Last Post: 12-28-2008, 06:22 PM
  4. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 05:07 AM
  5. Calculating hyperbolic functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:55 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
  •