Parallel Arrays--Frustrated

Ok so I know what I want to do, I just don't know how to do it. I need to establish a parallel array. I need one array to accept a integer and check to see if it is a duplicate. If it is not a duplicate it drops into the array and a counter is incrimented and the parallel array is incrimented. If it is a duplicate then it is not dropped into the first array and the counter is not incrimented, but the parallel array is incrimented so that it shows another of said integer. I don't know how to check the first array and then only incriment certain times. Thanks
Use a for loop to cycle through the array, and an if statement(s) to check if it is a duplicate or not. I'm not entirely sure what you're actually asking but it sounds as if you don't need two arrays.
I have MAX_SIZE = 10 const
data[MAX_SIZE]
freq[MAX_SIZE]
int used

I need to be able to input a number and that number goes into data and its frequency is tracked in freq
also tracked it the number of slots used in data
so that when a user inputs a number it is checked to make sure that it is a distinct number if it is duplicated it is only in data once but the frequency is incrimented to show that there are 2 of them and used shows that there are however many distinct numbers.

I was thinking a for loop to check through data to compare integers to see if there is a duplicate and if not it goes into the data array and frequency and used are incrimented if it is a duplicate then the it does not go into data and used is not incrimented, but frequency is incrimented where the duplicates are. I am just not sure how to get there with code...I have the logic I think...I know what I want to do I just am not sure how to do it. I think I over think and then I get confused as to where the numbers are held while they are checking the array and how to find where to incriment.

This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
     for(int i = 0; i < MAX_SIZE; i++)
  {
    if(data[i] == result)
    {
      used += 1;
      freq[i]++
      else
      freq[i]+=1;
    }
  }


}
Last edited on
It looks to me like you are taking an input array of int and converting it into two output arrays: the first with the actual int values in the input array, and the second with a frequency count for each corresponding value in the first.

To do this, you will need a way to search the array to see if it contains the given number. If it does not, add it to the output arrays with a frequency of one. If it does, simply bump the frequency count.

For example, given an input array of

2, 7, -3, 7, 42, ...

what you need to do is, for each value in the input array, check to see if it is already in the output array. You'll need to keep track of the used size of the output arrays.

1
2
3
int output_used_size = 0;
int output_values[ MAX_SIZE ];
int output_frequencies[ MAX_SIZE ];

Since 2 is not in output_values[], we'll need to add it.

output_used_size --> 1
output_values --> 2
output_frequencies --> 1

The next input value is 7. It also is not in the output array, so we'll add it too:

output_used_size --> 2
output_values --> 2, 7
output_frequencies --> 1, 1

Likewise with -3:

output_used_size --> 3
output_values --> 2, 7, -3
output_frequencies --> 1, 1, 1

This next time we have a 7 again. Since it is already in the array, we'll just bump the frequency counter:

output_used_size --> 3
output_values --> 2, 7, -3
output_frequencies --> 1, 2, 1

Continuing, we try to find 42 in the output array. Since it is not there, we'll have to add it:

output_used_size --> 4
output_values --> 2, 7, -3, 42
output_frequencies --> 1, 2, 1, 1

And so on.

Hope this helps.
Last edited on
UPDATED:

Yeah I think I have the logic part, but its the code part I have trouble with...would this work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

MAX_SIZE = 10 const
data[MAX_SIZE]
freq[MAX_SIZE]
int used
int result  //user input

IntStore::data [MAX_SIZE] (int result)

{
     for(int i = 0; i < MAX_SIZE; i++)
     {
         if(data[i] == result)     //check through the array to see if the user result has a match if a
                             //match is found add one to frequency
         {
              freq[i] += 1
         }
         else                 //if a match is not found incriment used by one and make frequency one
      
              used += 1;

              freq[i] = 1;
    
      }

}  

 
Last edited on
Topic archived. No new replies allowed.