4 Replies - 1137 Views - Last Post: 16 October 2010 - 09:16 PM Rate Topic: -----

#1 ROCKY86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 05-August 07

Stuck in creating 1 dimensional array

Posted 16 October 2010 - 11:11 AM

hey guys! need help to create this http://[IMG]http://i580.photobucket.com/albums/ss245/rock_James/GAVA.jpgPosted Image[/img]
I reallx gt no idea how to continues to create the above output! this is my code:
public class NewMain2
{
	public static void main(String[] args)
	{

int Star;
Scanner sc = new Scanner(System.in);
System.out.print("Input number of element");//ask for size of element from user
Star=sc.nextInt();//put inside Star
int[]data=new int[Star];//delcare data size of array of the userinput
System.out.print("Enter inputs: ");
for(int count=0; count<Star; count++)
{
data[count]=sc.nextInt           
 }

displayChart(data);
           
 }          

        

 public static void displayChart(int[] array)
{
            String dash="";
            String arrayNum="";


             for(int i = 1; i > 0; i--)
{
         	
            System.out.print(i + "|");
                         }
               else
               {
                  System.out.print(" *");
               }
               System.out.print("  ");//to align * with its x-axis value
            }
            System.out.println();
         }
        }





I don't know how to display the height and colume

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck in creating 1 dimensional array

#2 bobjob  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 163
  • Joined: 29-March 08

Re: Stuck in creating 1 dimensional array

Posted 16 October 2010 - 11:41 AM

It might be easyer to make rows instead of columns for this graph, as you are using the console to output the data.
Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Stuck in creating 1 dimensional array

Posted 16 October 2010 - 12:01 PM

That is a matrix of 7x7 elements but you need to do it like it was an 1 dimensional array. You can print the elements in one new row every 7 positions. You need print a linefeed ('\n') every 7 elements or build a string with 7 elements. Then print the string and build the string again with next 7 elements.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Stuck in creating 1 dimensional array

Posted 16 October 2010 - 01:30 PM

You really want two loops to do this. Rows and columns are good. For number of rows, you need to find the max number in the array. For number of columns, the length of the array. Every (row,column) will have a boolean value based on array[column]>=row. Given your graph, perhaps array[column]>=(maxRow-row).

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 ROCKY86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 05-August 07

Re: Stuck in creating 1 dimensional array

Posted 16 October 2010 - 09:16 PM

Can anyone explain to me how this code work??

public static void displayChart(int[] array)
{
int num=array.length
   for(int i=num; i>0; i--)
   {
     System.out.print(i+"|");
     for(int j=0; j<num; j++)
       {
          if(array[j]>=i)
           System.out.print(" *  ");
          else
           System.out.print("    ");
       }
      System.out.print();
}
 



This post has been edited by ROCKY86: 16 October 2010 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1