Results 1 to 7 of 7
Thread: problems with functions
- 02-07-2009, 01:55 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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 05:46 AM. Reason: adding script
- 02-07-2009, 02:31 AM #2
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-07-2009, 02:47 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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.
- 02-07-2009, 02:57 AM #4
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]
Java Code:public static int [] ar1;
CJSLLast edited by CJSLMAN; 02-07-2009 at 03:09 AM.
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-07-2009, 06:01 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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.
- 02-07-2009, 07:49 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
never mind on the whole thing. i got it. thanks for the help.
- 02-07-2009, 08:09 PM #7
Similar Threads
-
Functions in jsp
By samson in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 03-25-2009, 10:04 PM -
HashMap functions get vs put
By tahni in forum New To JavaReplies: 3Last Post: 01-14-2009, 06:55 PM -
print without built functions
By j2vdk in forum New To JavaReplies: 19Last Post: 12-28-2008, 05:22 PM -
Using functions that return values?
By Megapixelz in forum New To JavaReplies: 1Last Post: 04-30-2008, 04:07 AM -
Calculating hyperbolic functions
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:55 PM
Bookmarks