Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Array Operations

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Operations

    Hi,

    I can't seem to figure this out. I'm trying to create a program that takes user input to create an array. A loop will then print out possible operations to perform that the user will select. The operations will be count, sum, average, min, max, and standard deviation.


    Any help will be very appreciated.

    Thank you.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Array Operations

    What did you try? Paste your code here you have done until now and we can help you. We don't do homework.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Operations

    Here is what I have so far. Most of this is from a similar program I found online. I'm just trying to get it working so I can understand it. I'm not sure where to go from here.


    package arrayoperations;

    import java.util.Scanner;

    public class ArrayOperations {

    double count;
    double sum;
    double average;
    double min;
    double max;
    double sd;

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("How many numbers do you want to enter?");
    int num = input.nextInt();

    int array[] = new int[num];

    System.out.println("Enter the " + num + " numbers now.");

    for (int i = 0 ; i < array.length; i++ ) {
    array[i] = input.nextInt();
    }


    System.out.println("These are the numbers you have entered.");
    printArray(array);
    System.out.println("\nWhich action would you like to perform? \n1 for count, 2 for sum, 3 for average, 4 for min, 5 for max, or 6 for standard deviation: ");

    }


    public static void printArray(int arr[]){

    int n = arr.length;

    for (int i = 0; i < n; i++) {
    System.out.print(arr[i] + " ");


    }




    }

    }

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Array Operations

    Do you know what this program does? And what else you want it to do?
    Also, close your code in code tags, for the ease of understanding.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Operations

    Right now it is just taking user input for an array and printing the array out. I need a loop to print out possible operations to perform on the array. Count, sum, average, min, max, and standard deviation. I am not sure how to start that part. Any tips on how to do that would be awesome.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Array Operations

    Right. So, take a copy and pencil and start doing the counting of array numbers. Write down the points how are you doing this and implement them as Java language. Same as all other operations.
    Hope you will be able to solve this. If you get stuck in your code, ask here for more help.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Operations

    Any tips on how to code the operations for the array?

    package arrayoperations;

    import java.util.Scanner;

    public class ArrayOperations {


    public static void main(String[] args) {


    Scanner input = new Scanner(System.in);


    System.out.println("How many numbers do you want to enter?");
    int num = input.nextInt();

    int array[] = new int[num];

    System.out.println("Enter the " + num + " numbers now.");

    for (int i = 0 ; i < array.length; i++ ) {
    array[i] = input.nextInt();



    }


    System.out.println("These are the numbers you have entered.");
    printArray(array);


    }


    public static void printArray(int arr[]){

    int n = arr.length;

    for (int i = 0; i < n; i++) {
    System.out.print(arr[i] + " ");


    }
    double option;
    Scanner reader1 = new Scanner(System.in);
    System.out.println("\nWhich action would you like to perform? \n1 for count, 2 for sum, 3 for average, 4 for min, 5 for max, or 6 for standard deviation: ");
    option = reader1.nextDouble();
    for(int i = 0; i < n; i++){
    if(option == 1 ){
    System.out.println("count");
    } else if (option == 2){
    System.out.println("sum");
    } else if (option == 3){
    System.out.println("average");
    } else if (option == 4){
    System.out.println("min");
    } else if (option == 5){
    System.out.println("max");
    } else if (option == 6){
    System.out.println("standard deviation");
    } else {
    System.out.println(option);


    }



    }
    }
    }

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Array Operations

    Work on the operations one at a time. Start with count.

    Follow Mr.777's advice in #6 (where he also already suggested starting with count) and use pencil and paper, thinking critically about how you rather than the computer would perform the task.

    Suppose you have an array containing

    1, 42, 0, -1, -1, 0, 0, 666, 1, 1, 1, 1000000

    How many numbers are there? How did you figure that out? (No, not "I counted them", but what are the specific steps that counting involves?)

    Suppose people entered from a door on the left carrying numbers on cardboard signs, paused for a moment and exited through a door on the right. The sign saying million had the word "the end" on it and you knew that when you saw a sign like that you were going to be asked how many people you had seen. How would you go about preparing yourself to answer a question like that?

Similar Threads

  1. RPN Help Please! (Order of Operations)
    By yuvalb in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 30th, 2011, 11:53 AM
  2. Help With Java Homework: Set Operations
    By kilroyjr in forum Java Theory & Questions
    Replies: 10
    Last Post: April 1st, 2011, 10:41 AM
  3. Help with OS operations!
    By benglish in forum Java Theory & Questions
    Replies: 8
    Last Post: April 1st, 2011, 07:32 AM
  4. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 09:54 AM
  5. unsafe operations note??
    By bookface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2010, 10:58 AM