Scanning an array

Hey guys, new to c++. What I am trying to do is generate an array with random constituents (the number depicted by the user). While the array is filling, I want another loop to be scanning if a double has occured.

For example, if the array generated as:

10 4 5 18 5

I want to be able to detect that that 5th element was a double. I have some code I have been working on. Greatly appreciate if you could help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int detect(int A[], int size)
	{


		for (int i = 0; i<=size; i++)
		{
			A[i] = rand() % size + 1;
		
			if (i > 0)
			{
			int j = 0;
			while (j < i)
			{
					if (A[j]==A[i]){return i;}

					j++;
			}}

		}
		return 0;
	}
Last edited on
your using i++ in your for loop and in the body of the code, your going to loop throughin twos missing every other index of the array
Isn't the other loop using j? I'm trying to set it up such that once i>0, the j loop can scan all the elements before the ith element, to see if any of the jth elements are equal to the ith element.
Here I cleaned it up a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int detect(int A[], int size)
	{
		for (int i = 0; i<size; i=i+1)
		{
			A[i] = rand() % (size-1) + 1;
			cout << A[i] << " ";
			if (i > 0)
			{
				int j = 0;
				while (j < i)
				{
					if (A[j]==A[i]){return i;}
                                        else {j++;}
                                 }
			}

		}
		return 0;
	}
Last edited on
lol no I meant that you used i++ on line 8 of your original code :P its gone now theres a fair chance i was seeing things, i get that frm time to time...im still looking at your code with my brain
wait what you mean a double?? if you have an array its an array of ints or an array of doubles, unless you use a vector of objects :P

what do you mean by double?
Just so you know, Devon. There was i++ on line 8 before! lol
I got rid of it. I managed to get what I needed. I haven't learnt about vectors yet, and I already explained what I meant by "a double".

Randomly generated numbers: 10 8 4 5 6 4
The loop stops and counts how many numbers have been generated before the first double (the double was 4) and the numbers drawn was 6.

Anyways. I am having another problem. Note( x = 20)

1
2
3
4
5
6
7
8
9
10
11
while(x <= 1000)
	{			
				count = 0;
				int array[x];
				{while (s < N)
				{
					count = count + detect(array, x);
					s++;
				}   cout << count/N << endl;}
				x = x + 20;
	}


When this loop executes, it only calculates count/N for the first x (x=20), and then all of the others (x=40,60,80,...,1000) generate a zero. It is like value of x for the array is not taking. Ideas?



EDIT: I am an idiot and forgot to initialize s=0 for when the while block restarted
Last edited on
OP means a repeated value.

You can do this a few ways. Either continue the way you are now and check after each iteration if you have already added this element, if so generate a new one. You can also generate the entire array, and then go back in and fix any repeated values. Both of these about the same efficiency wise, which is they are both bad.

A slightly better way to do this is to sort the array, and then it's just matter of checking to see if value n == value n + 1. But this has the problem of having to insert the new value into the correct spot each time something needs replaced. There's a couple ways to go about doing that, neither really being better than the other.

And a better way again is to have a bool array of size MAX - MIN, where MAX is the maximum number that can be generated, and MIN is the smallest. Each time you insert a number into the unique valued array, you first check the bool array to see if that index is true, if it is then you generate a new value and repeat. If it's not true, set that index of the bool array to true and then insert into the unique valued array.

Or, you can just use a data structure that is meant to only have unique values. Such as std::unordered_set.
ahh now i understand how to solve this someone beat me to it :/

its the same as the pancake glutton practice code.
Topic archived. No new replies allowed.