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:

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:

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.

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;}

}

You're on the right track in some ways, but there are a few problems. Break this function up into three separate tasks:

Task 1 - initialize largeAvg using the first three elements (you do this, but keep in mind that you need to divide by 3 or else it is the sum, not the average.

Task 2 - Go through the array and find the largest average (you have the right idea with the if statement)

Task 3 - Display the largest average's indexes.

You are combining tasks 2 and 3. Keep them separate. Note that you never change largeAvg and you have no variable that keeps track of the indexes.

// task 1
int largeAvg;
largeAvg = (array[0] + array[1] + array[2]);
int index = 0;  // add this variable

// task 2
for (i = ?; i < ?; ?)
{
     if (?)
     {
          // assign new value to largeAvg
          // assign new value to index
     }
}

// task 3 - display
cout << index << ' ' << index + 1 << ' ' << index + 2;
// Note that you are only displaying AFTER for-loop is over.
// There is no cout statement inside of an if-statement.

Your for-statement needs to be changed slightly. Your if-statement should not change the value of i. Let i change in the for-statement instead. Also note that I have added a variable called index. You cannot use i since i will have gone "out of scope" after the for-loop ends.

Ok thanks for the help. I was able to get the function where I had to print the three consecutive values in the array with the highest average. However I'm still having trouble on the percentage of unique number functions. Here's the 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;
	
	return 1;
}

If the "number" integer is only 10, then the max percent is only 10%, which I think, makes the problem easier. However, my code does not work. The percentage always turns out to be the integer for "number" or 0. I tried to make the for statement in the coverage function as : for(key = 0; key<100; key++), but that didn't work either.

I have to fill the array with a random number function, with numbers between 1-100, but sometimes I get huge integers in the array.

void fillArray (int array[size], int number)
{
srand((unsigned)time(0)); 
     
    for(int i=1; i<number; i++) 
        array[i] = (rand()%100)+1;          
}

There was a very small section in my text book on random numbers. I'm not sure why the function would give huge integers only sometimes.

int search ( int array[], int number, int key )
{
	int pos = 0;
	
	while(pos < number && array[pos] != key)
		pos++;
	
	if( pos == number)
		pos = 0;
	
	return 1;
}

This function is the same as this:

int search ( int array[], int number, int key )
{
     return 1;
}

which I am sure you don't want.

void fillArray (int array[size], int number)
{
srand((unsigned)time(0)); 
     
    for(int i=1; i<number; i++) 
        array[i] = (rand()%100)+1;          
}

This looks fine except that array[0] never gets a value. But you shouldn't have any "huge" values for indexes 1 through number - 1 when you get done with this function.

I think you may need to post the whole program. But like I said, the search function just returns 1.

OK I changed my search 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.

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 think my large integer problem comes from my display function -

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.

using the linear search function, search for the key in the array and return the position if found or -1 if not found

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;
}

Your function is still this:

int search ( int array[], int number, int key )
{
	return 1;
}

Does your search function ever return -1? What do number and key represent in the function above?

"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.
Here's the whole code, it generates 100 random numbers that are 1-100 that is stored in the array, then the user is prompt for how many numbers are used.
Then array is displayed, followed by the average of the array, then the amount of numbers in the array that are above the average,
Then 3 consecutive values that yield the highest average are displayed, followed by 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%.)

#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

using namespace std;

const int size = 100;

void fillArray(int [size], int);
void printArray(int[size], int);
double average(int[size], int);
int countAboveAverage( int[size], int, double);
void printConsecutive( int [size], int);
double coverage( int [size], int);
int search ( int [size], int, int);

void main()
{

 
 int list[size];
 int number;
 double avgNum; 
 int aboveAvg;
 double percent;
 
 cout <<"Please enter an integer that is between 3 and 100:  "<< endl;
 cin >> number;

 fillArray( list, number);
 printArray(list, number);
 avgNum = average(list, number);
 aboveAvg = countAboveAverage(list, number, avgNum);
 

 cout << "The average of the numbers is: " << avgNum << endl;

 cout << "The amount of numbers that are above the average is: "<< aboveAvg << endl;

 printConsecutive( list, number);
 percent = coverage(list, number);

 cout << "The percent is: " << percent << endl; 
 
}

void fillArray (int array[size], int number)
{
 srand((unsigned)time(0)); 
     for(int i=0; i<number; i++) 
        array[i] = (rand()%100)+1;          
} 
void printArray( int array[], int number)
{

 for (int i=0; i<number; i++)
 {
    cout<< array[i];

    if( (i % 10) == 9)
        cout << endl;
    else
        cout << ", ";
 }
}

double average ( int array[], int number)
{
 double avg;
 avg = 0;
 
 for (int i = 0; i <number; i++)
  avg = avg + array[i];

 avg = avg/number;

return avg;
}

int countAboveAverage( int array[], int number, double average)
{
 int total;
 total = 0;
 for (int i = 0; i <number; i++)
 {
  if (array[i] > average)
   total++;
 } 
return total;
}

void printConsecutive(int array[], int number)
{
 
 int largeAvg;
 int index;

 index = 0;
 largeAvg = (array[0] + array[1] + array[2])/3;

 for (int i = 1; i < number -2; i++)
 {
  if ( (array[i] + array[i + 1] + array[i + 2])/3 > largeAvg)
  {
	 largeAvg = (array[i] + array[i+1] + array[1+2])/3;
	 index = i;
  }
 }
 
 cout << "The three consecutive numbers with the largest average is: " 
 << index << ", " << index+1 << ", " << index+2 << endl;
}

double coverage (int array[], int number)
{
	int key;
	int percent;
	
	percent = 0;
	
	for( key = 1; key<100; 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;
}

Edit 1: I edit to post to update my print array function, it does what its supposed to now.

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 specification tells you that you must return the position or -1, so that's what you want to do.

using the linear search function, search for the key in the array and return the position if found or -1 if not found

Thus don't try to change to change the search function to meet the needs of the function that calls it. Change the function that calls it so that it can make use the numbers that the search function is supposed to return.

That's why I was trying to return 1, if the value was found.

It is returning 1 even when the value is NOT found. Perhaps you intended this:

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;  // instead of returning 1
}

You still want to write the function to the specification, but at least this way the function would do different things under different circumstances.

Thank you! I think I finally got it. I also cant believe I missed such an obvious mistake by returning 1 instead of pos, the first time around.

include<stdio.h>
#include<stdlib.h>
#include<conio.h>

void main()
{
  clrscr();
  int a[5],i,j,c=0,t,uniqu_number;
  float per,total_number;
  total_number=5.0;
  for(i=0;i<5;i++)
  {
	scanf("%d",&a[i]);
  }

  printf("Repeating elements are ");

  for(i = 0; i < 5; i++)
    for(j = i+1; j < 5; j++)
      if(a[i] == a[j])
      {
	printf(" %d ", a[i]);
	c=c+1;
      }
t=c*2;
uniqu_number=total_number-t;
printf("\nsame Element in Array : %d",c);
printf("\nTotal No. Of Uniqu Number In Array : %d",uniqu_number);
per=uniqu_number/total_number*100.00;
printf("\nPercentage Of Uniqu No In array : %f",per);
  getch();

}
commented: Three years too late giving away the answer with bad code. Epic fail. -4
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.