| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Memory & arrays - Finding the percentage of unique numbers in an array.
Hey I have this tough assignment that just keeps on getting harder and harder. I have 3 more functions to complete.
Function1: I have to print the positions of the three consecutive values that have the largest average. If more than 3 consecutive values have the same average, print the positions with the lowest subscripts. I have to print the positions (subscripts), not the actual values. Here's my code so far- Code: Code:
void printConsecutive(int array[], int number)
{
int largeAvg;
largeAvg = (array[0] + array[1] + array[2]);
for (int i = 3; i < number; i = i + 3){
if ( (array[i] + array[i++] + array[i = i + 2]) > largeAvg){
int j = i++;
int k = i + 2;
cout<< i << ", " << j << ", " << k << endl;}
}
Its not giving me the results I want. Then I have to do this- Function 2: determine the percentage of the numbers from 1 to 100 that appear in the array. For example, if the array holds 100 integers and all the integers are unique (all numbers from 1 to 100 appear), the percentage would be 100%. The expected percentage is lower as duplicates may appear. To do this, each value from 1 to 100 is searched for in the array (using the search function) and if the number appears, a counter is incremented. Then the percentage is calculated. The result will be a value from 0.01 to 1.00. Note: if the user has asked for 10 values, then the maximum coverage in this case would be .1. This would mean that ten unique values between 1 and 100 are contained in the array. If, for example, the user asked for 50 values, then the max coverage would be 50% (.5), meaning than the array contained 50 values between 1 and 100 with no duplicates. If there were two duplicates, this would make the coverage only 48% (.48). Function 3: using the linear search function, search for the key in the array and return the position if found or -1 if not found This function will only be needed (and used) by the coverage function (above). Here's my code so far for function 3 ( I dont know how to implement it in function 2)- Code: Code:
int search (int list [], int size, int key)
{
int pos = 0;
while (pos < size && list[pos] != key)
pos++;
if (pos == size)
pos = -1;
return pos;
}
Any advice would be appreciated, and thanks in advance. |
|
#2
|
|||
|
|||
|
you should be skipping only 1 value at a time, not 3
the sets of three values can overlap also you were change 'i' inside the loop - not the way it's normally done, you lose count of where the loop is at. i++;// adds 1 to i, returns original i, does NOT return i+1, ++i; // adds 1 to i, returns original i + 1 i=i+2;// changes i to i+2, returns i + 2 i+1;// returns value 'i+1' without changing i i+2;// returns value 'i+2' without changing i i fixed up your code a bit for you : Code:
void printConsecutive(int array[], int number)
{
int bestPos = 0, largeAvg = (array[0] + array[1] + array[2]);
for (int i = 1; i < number; i++)
{
if ( (array[i] + array[i+1] + array[i+2]) > largeAvg)
{
bestPos = i;
}
}
cout<< bestPos << " " << bestPos+1 << " " <<bestPos+2 << " " <<endl;
}
This is just a tidied version of your linear search : Code:
int search (int list [], int size, int key)
{
for(int pos = 0; pos < size; pos++)
if(list[pos] == key)
return pos;
return -1;
}
or a while version with a cleaned up 'return' value :- Code:
int search (int list [], int size, int key)
{
int pos = 0;
while(pos < size && list[pos] != key)
pos++;
return (pos < size) ? pos : -1;
}
|
|
#3
|
|||
|
|||
|
OK I changed my search function (my teacher wants only one return statement in the function), but I'm still not getting the right results. I assume the logic flaw is in the search function, not the coverage function. But I ran through it and could not find out what was wrong.
Code:
double coverage (int array[], int number)
{
int key;
int percent;
percent = 0;
for( key = 0; key<number; key++)
{
percent = percent + search(array, number, key);
}
return percent;
}
int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
if( pos == number)
pos = 0;
else
pos = 1;
return 1;
}
I use a random number function to fill the array, and sometimes my display function displays huge integers, I think the problem lies in the display function, not the random number function. Code:
void printArray( int array[], int number)
{
for (int i=0; i<number; i++)
{
for (int j = 0; j<10; j++)
{
cout<< array[i]<< ", ";
i++;
}
cout << endl;
}
}
The values should appear 10 per line and are comma separated. The last line may or may not have 10 values: for example, if the user asked for 15 numbers, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have 2 values. |
|
#4
|
|||
|
|||
|
Code:
int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
if( pos == number)
pos = 0;
else
pos = 1;
return 1;
}
You are setting pos but returning '1' no matter what happens try 'return pos' instead |
|
#5
|
|||
|
|||
|
Code:
void printArray( int array[], int number)
{
for (int i=0; i<number; i++)
{
for (int j = 0; j<10; j++)
{
cout<< array[i]<< ", ";
i++;
}
cout << endl;
}
}
sorry but the looping logic here just sucks. either have a for loop and do i++ in the for loop header or have while loop and have i++ inside the loop itself, not both !! |
|
#6
|
|||
|
|||
|
Code:
void printArray( int array[], int number)
{
for (int i=0; i<number; i++)
{
cout<< array[i];
if( (i % 10) == 9)
cout << endl;
else
cout << ", ";
}
}
|
|
#7
|
|||
|
|||
|
Ok thank you for the help.
"Number" is the integer the user chooses for the number of values in an array (min is 3, max is 100). The random number generator function generates numbers 1-100, so they key is 1-100. I'm not sure I want to return the exact position value. For example, if the pos returned is 20, then it would add 20 to the percent value, there by raising the percent by 20% instead of 1%. That's why I was trying to return 1, if the value was found. The function is supposes to return The percent of unique numbers 1-100 in the array (Note: if the numbers in the array was choose to be 10, then the max percent is 10%, if it was 92 it would be 92%.) |
|
#8
|
|||
|
|||
|
yeah, but your code changes pos to be either 1 or zero, so you should return it to get your code working
right now, you are ignoring the cases when it is zero and returning 1 all the time. my previous advice stands Code:
int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
if( pos == number)
pos = 0;
else
pos = 1;
return pos;
}
OR :- Code:
int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
if( pos == number)
return 0;
else
return 1;
}
OR:- Code:
int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
return ( pos != number); // returns 1 if pos not equal to number, returns 0 if pos == number
}
All these versions do exactly the same thing |
|
#9
|
|||
|
|||
|
Well, for the array of 100 numbers, it ought to be easy. Even if you don't use the search function, which I won't because I haven't gotten to it yet, just start a value at one. Use a for loop, send it from 1-100, and if a value is <= the target value inserted, counter++. When you're done with the for loop, cout the value. Or printf. Or whatever you feel like doing. Shouldn't be that difficult, and you don't need multiple functions for it.
The code would probably look something like this when you've done all that: Code:
int y;
time_t seconds;
time (&seconds);
srand((unsigned in) seconds);
for (y=1;y<=100;y++)
array[y]=rand()%100;
for (int x=1;x<=100;x++)
if (array[x]<=inputnumber)
counter++;
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
cout<<"The percentage of items in the list that were less than\nor equal to your number was "<<counter<<"%.\n";
system("PAUSE");
Note: I haven't done this. I just typed this up just now without testing it, so there are probably a couple bugs in that. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Memory & arrays - Finding the percentage of unique numbers in an array. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|