C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 15th, 2008, 08:38 PM
matt_570 matt_570 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 7 matt_570 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 55 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old November 17th, 2008, 04:01 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 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;
}

Reply With Quote
  #3  
Old November 17th, 2008, 07:52 PM
matt_570 matt_570 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 7 matt_570 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 55 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old November 17th, 2008, 08:15 PM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 2
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

Reply With Quote
  #5  
Old November 17th, 2008, 08:18 PM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 2
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 !!

Reply With Quote
  #6  
Old November 17th, 2008, 08:20 PM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 2
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 << ", ";
 }
}

Reply With Quote
  #7  
Old November 17th, 2008, 09:22 PM
matt_570 matt_570 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 7 matt_570 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 55 sec
Reputation Power: 0
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%.)

Reply With Quote
  #8  
Old November 18th, 2008, 01:07 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 2
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

Reply With Quote
  #9  
Old November 18th, 2008, 08:35 AM
Bioniclegenius Bioniclegenius is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 16 Bioniclegenius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 51 m 22 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Finding the percentage of unique numbers in an array.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT