ok, im trying to write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate. Lastly the function should return a pointer to the array. If anyone can help much appreciated
Thanks in advance

Recommended Answers

All 8 Replies

>>im trying to write a function
please post what you have so far.

Are you using C or C++?

im using c++ and havnt figured out what kind of fucntion to write this program is a 3 part question this is just the first part of it.

you know that it the function returns an int pointer and has one argument

int* foo(int n)
{
  // blabla
}

ok i got my program written however im having two problems im trying to get the lowest score to drop off when doing the average score. secondly if you compile my program you can clearly see i have it in acsending order it looks something like this
score 1: 50
score 2: 25
score 3: 75

which is fine but i want it to look like this though
score 2: 25
score 1: 50
score 3: 75

here is my code if anyone can help much appreciated.

#include <iostream>


void sortArray( double arr[], int sz );
void sortArray( double ( &arr[] ), int sz );
void displayArray( double arr[], int sz );


int main()
{
    double *scores, total = 0;  // average;
    int count;
    int numAmount;
    
	cout << "How many test scores are you going to record? ";
	cin >> numAmount;
    scores = new double[numAmount];
    
	cout << "Please enter in your test scores below.\n";
    for ( count = 0; count < numAmount; count++ )
    {
        cout << "Test Score " << (count + 1) << ": ";
        cin >> scores[count];
    }
	
	
	sortArray( scores, numAmount );
	displayArray( scores, numAmount );
	
	delete [] scores;

	return 0;
}

void sortArray( double arr[], int sz )
{
	
}

void displayArray( double arr[], int sz )
{
	cout << std::endl;  // for spacing
	for ( int i = 0; i < sz; i++ )
	{
		std::cout << "Score " << ( i + 1 ) << " : ";
		
		std::cout << arr[i] << std::endl;
	}
}

if you compile my program you can clearly see i have it in acsending

void sortArray( double arr[], int sz )
{
	
}

:mrgreen: :mrgreen: :mrgreen: you have to write code for that function if you expect the array to get sorted.

hello,
ok this code works fine but it still dosnt compute the avearage with the lowest score dropped.
here is my code.

#include <iostream>
#include <iomanip>
using namespace std;
void sortArray( double arr[], int sz, int scoresPos[], int size );
void displayArray( double arr[], int sz, int scoresPos[], int size );

int main()
{
    double *scores, total = 0;  // average;
 
    int count;
    int numAmount;
    
    cout << "How many test scores are you going to record? ";
    cin >> numAmount;
    scores = new double[numAmount];
    int *scorePos = new int[numAmount];
    cout << "Please enter in your test scores below.\n";
    for ( count = 0; count < numAmount; count++ )
    {
        cout << "Test Score " << (count + 1) << ": ";
        cin >> scores[count];
 scorePos[count] = count + 1;
    }
 
 sortArray( scores, numAmount, scorePos, numAmount );
 displayArray( scores, numAmount, scorePos, numAmount );
 
 delete [] scores;
 delete [] scorePos;

    return 0;
}
//***********************************************************************
// Definition of a sortArray                                            *
// This function excepts a double array and its size as an              *             
// argument. The results will be diplayed in ascending order            *
//***********************************************************************

void sortArray( double arr[], int sz, int scoresPos[], int size )
{
    double temp = 0;
    int posTemp = 0;
    for ( int i = 0; i < sz - 1; i++ )
    {
        for ( int j = ( i + 1 ); j < sz; j++ )
 {
            if ( arr[i] > arr[j] )
           {
                temp = arr[i];
  posTemp = scoresPos[i];
                arr[i] = arr[j];
  scoresPos[i] = scoresPos[j];
                arr[j] = temp;
  scoresPos[j] = posTemp;
           }
        }
     }
}
//*************************************************************************
// Definition of a display array                                          *
// This function excepts array and its size as an argument. The           *
// average will be displayed dropping the lowest score.                   *
//*************************************************************************

void displayArray( double arr[], int sz, int scoresPos[], int size )
{
 double average = 0;
 double sum = 0;
 std::cout << std::endl;  // for spacing
 for ( int i = 0; i < sz; i++ )
 {
  cout << "Score " << scoresPos[i] << " : ";
  cout << arr[i] << std::endl;
 }
 
 int j;
 for ( j = 1; j < sz; j++ )
 {
  sum += arr[j];
 }
 average = sum / j;
 cout << "\nThe average score disregarding the lowest score is: " << average << endl;
    cout << fixed << showpoint << setprecision(2);
    
}

To determine the average of all elements except the lowest you need to write a routine that obtains the sum of all values except the lowest and divide the sum by the number of elements in the array minus one. Thus this:

average = sum / j;

should probably be this:

average = sum / (j - 1);

since at the end of this loop

for ( j = 1; j < sz; j++ )

j equals sz. Personally I think this would be even better

average = sum/(sz - 1);

but the other version works in the scenario presented.

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.