1 Replies - 423 Views - Last Post: 01 December 2016 - 05:56 PM Rate Topic: -----

#1 adepuydt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 06-October 16

First Time Working With Arrays?

Posted 01 December 2016 - 05:25 PM

Hey guys, I have an assignment that I'm trying to figure out. I'm not sure if I'm off to the right start... This is my first time working with arrays as well as bubble sort and I'm not totally positive if I'm on the correct track. Any input is much needed! I'll attach the assignment and my code so far, I'm obviously not finished implementing all the requirements.

Write a program in Java that initializes an array of non-negative integers and prints a two-column list, described as follows. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.
For input array 4 0 4 9 0
the output should be:
N Count
9 1
4 2
0 2
For input array 12 3 12 4 1 1 12 1 7 1 2 3 4 2 3 12
the output should be:
N Count
12 4
7 1
4 2
3 3
2 2
1 4

My code so far-

package lab16;

public class Driver {
	int[] array = {4,0,4,9,0};
	
	private int[] getDistinct(int[] dist){
		dist = new int[array.length];
		for(int i : dist){
			dist[i] = array[i];
			for(int j : dist){
				if(dist[j] == dist[j+1]){
					dist [j] = -1;
				}
			} if (dist[i] > 0){
				System.out.println(dist[i]);
			}
		}
		return dist;
	}
	
	private void BubbleSort(int[] order){
		order = new int[array.length];
		boolean flag = true;
		int temp;
		
		while(flag){
			flag = false;
			for(int k : order){
				if(order[k] > order[k+1]){
					temp = order[k];
					order[k] = order[k+1];
					order[k+1] = temp;
					flag = true;
				}
			}
		}
	}

	private int findNumberOfOccurances(int[] occ, int counter, int lastItem){
		counter = 0;
		lastItem = array[0];
		for(int i : array){
			if (array[i] == lastItem){
				counter ++;
			}else{
				counter = 1;
			}
		}
		return occ[counter];	
	}
	
	public static void main(String[] args){
		
	}
}




Again, any pointers are very much appreciated. I'm a little confused on how I'm supposed to output the correct response. I'm not sure how to store the information I'm obtaining from my loop and print out that stored array?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: First Time Working With Arrays?

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,091
  • Joined: 25-December 13

Re: First Time Working With Arrays?

Posted 01 December 2016 - 05:56 PM

Can program make multiple passes over the data?
One to extract the unique values and sort them
Another to count the occurrences?

Another approach would be to sort the values and then merge and count the duplicates.

This post has been edited by NormR: 02 December 2016 - 04:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1