hello all, i am new here ,, and i need some help to do this array problem.. i can't figure it out..
any help will be greatly appreciated..
here is the question:

1- define an array of size 100 of integers, and fill it with random numbers between 1 and 20,

2- define a function accepting the array as a first argument and an two arrays of size 20 as second and third arguments. Regardless of the number of times that a value repeats (values that occur more than one time in the first array) the second array should contain that value only once. Count the number of times each value occurs in the first array and store the information in the third array.

3- Print each element of the second array followed by a tab, and then the number of times that value occurred in the first array on one line. Print your output to the screen.

i have done filling the random numbers between 1 and 20 in the array of size 100.. but couldn't do the rest of it..

thanks for the help.

Why? What is it you don't understand? Looks straight forward to me.

i dont know, my logic might be wrong...
here is the code i'm trying to do...

#include<iostream>
#include<cstdlib>
using namespace std;

void final(int num[ ], int dist[ ], int ctr[ ]){
	int i=0, j=0, k;
	bool found=false;

	for(i=0;i<100;i++){
		for(j=0;j<20;j++){
			if(num[i]==dist[j]){
				found=true;
			}//if
		}//for
		if(found!=true){
			dist[i]=num[i];
		}//if
        }//for

       //for the 3rd part of the question
       for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			if(num[i]==num[j])
				ctr[i]+=1;
		}//for	
	}//for

	for(i=0;i<20;i++)
	cout<<dist[i]<<" "<<num[i]<<" "<<ctr[i]<<endl;
}

int main(){
	int num[100], ctr[20], dist[20]={}, i;
	srand(time(NULL));
	
	for(i=0;i<100;i++){
		num[i]=1+rand()%21;
		cout<<num[i]<<"  ";
		}
	cout<<endl;
	final(num, dist, ctr);
}

can you please see what am i doing wrong..

the program works if the generated random numbers Do Not Duplicate.. but its not possible, and after they Duplicate.. it just dont work as intended...

Plan out what you need to do on paper first. As a start, you should be taking care of part 2 all at the same time instead of in a separate loop.

March along your num array, when you come upon a number, see if it's in dist, if not add it, and continue along num, incrementing a counter as you go. Enter that counter into the respective spot in the ctr array.

For the third part of the question, set the 3rd array to all zeros. Then you can simply use the element in the first array num[i] to increment the proper element in the 3rd array: dist[num[i]] In other words, if first array contains:
num[]= 2 4 3 8 5 3 4 2 8 4

the 3rd array will contain
ctr[0]=0
ctr[1]=0
ctr[2]=2
ctr[3]=2
ctr[4]=3
ctr[5]=1
ctr[6]=0
ctr[7]=0
ctr[8]=2
ctr[9]=0

You don't need the double loop.

The answer to the second question can now be loaded into dist[] just by looping through cnt[] and looking for != 0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.