Results 1 to 4 of 4
  1. #1
    anonymous1126 is offline Member
    Join Date
    Feb 2015
    Posts
    1
    Rep Power
    0

    Default How to display contents in parallel array using Java

    I have to write a program that calculates the average temperature for a month using parallel arrays (it is mandatory to use a parallel array). I'm new to Java (I'm more familiar with C++) so I get confused with the use of methods. I know how to compute the averages already, I just need help with setting up the parallel arrays. This is what I have so far:

    Java Code:
    import javax.swing.*;
    import java.util.Scanner;
    
    public class Temperature 
    {
       public static void main(String[] args)
    {
          String[] day = {"Monday", "Tuesday", "Wednesday",
                 "Thursday", "Friday", "Saturday", "Sunday"};
          int[] temp = new int [7];
    }
       public static int[] getTemp(String[] day, int[] temp)
       {
          Scanner scan = new Scanner(System.in);
          for (int i = 0; i < day.length; i++)
          {
             System.out.println("Enter temperatures for : " + day[i]);
             for (int i = 0; i < day.length; i++)
             {
                temp[i] = scan.nextInt();
             }             
          }
          return temp;
       }
    }
    For now I just want to show the contents in my array before I start computing averages.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to display contents in parallel array using Java

    Quote Originally Posted by anonymous1126 View Post
    For now I just want to show the contents in my array before I start computing averages.
    Then just put them inside a loop and print them out. Similar to what you would do in C.
    There is a problem with your method though. You keep entering 7 values for each day. That won't work unless
    you want to prompt for 7 values for each day, add them, and then save the average.

    Regards,
    Jim
    Last edited by jim829; 02-21-2015 at 03:22 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    johnmmrl is offline Member
    Join Date
    Feb 2015
    Posts
    1
    Rep Power
    0

    Default Re: How to display contents in parallel array using Java

    can you help me to on how to show the swapping method using shell sort please

    this is my code:

    import java.util.*;
    //import java.util.Arrays;
    public class Runner
    {

    public static void main (String [] args ) throws Exception
    {
    try{
    //ShellSort hs = new ShellSort();
    Scanner scan = new Scanner (System.in);
    int range;
    int temp;
    int choice;

    //int []unsorted;

    System.out.print("How many integers to arrange? ");// displaying how many integer to arrange
    System.out.println();
    range = scan.nextInt();
    int [] unsorted = new int [range];

    for(int i=0; i<range; i++) //input the elements
    {
    System.out.println("Real Number:");
    unsorted[i] = scan.nextInt();
    }
    //System.out.println("Unsorted Number"+Arrays.toString(range));
    System.out.println("");
    System.out.print("Unsorted elements: [ ");
    for (int i = 0; i<unsorted.length ; i++) //displaying the unsorted elements
    {
    System.out.print(unsorted[i]+ " ");
    }
    System.out.println(" ]");


    //System.out.println(" [1].step1A");
    //System.out.println(" [2].step1B");
    //System.out.println(" [3].step1C");
    //choice = scan.nextInt();
    int temprange = unsorted.length;
    int h = 1;
    int inner,outer;
    for (int i = 0; i<unsorted.length;i++) //display the gap
    {
    if (temprange != 0) //computation for gap
    {

    System.out.println("The gap for pass "+(i+1)+ " is : "+temprange);
    temprange = (temprange/2);
    }

    }



    while (h > 0) {

    for (outer = h; outer < range; outer++) {

    temp = unsorted[outer];
    inner = outer;

    while (inner > h - 1 && unsorted[inner - h] >= temp) {

    unsorted[inner] = unsorted[inner - h];
    inner -= h;

    }

    unsorted[inner] = temp;

    }

    h = (h - 1) / 3;
    }
    System.out.println(" "+





    System.out.println("\n Ascending order");
    for (int i=0; i<range; i++) {
    System.out.print(" " +unsorted[i]);
    }

    System.out.println("\n Desceding order");
    for (int i=range-1; i>0; i--) {
    System.out.print(" " +unsorted [i]);
    }



    }catch (Exception e)
    {
    System.out.println("Input Error");
    }
    }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to display contents in parallel array using Java

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    how to show the swapping method
    Can you explain what you mean by "show swapping method"? Perhaps an example of the expected output would help.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Parallel Array help
    By garrettlwilson in forum New To Java
    Replies: 1
    Last Post: 10-11-2013, 08:25 PM
  2. Replies: 1
    Last Post: 07-23-2012, 10:37 AM
  3. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  4. Display partial file contents in JTextArea
    By tmoehlman in forum New To Java
    Replies: 0
    Last Post: 11-02-2009, 11:03 PM
  5. Display the contents of a file on jsp page
    By shiva in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-30-2009, 01:01 PM

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
  •