Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,530 people online right now. Registration is fast and FREE... Join Now!




help with dynamically acllocated arrays

 
Reply to this topicStart new topic

help with dynamically acllocated arrays

joelw
14 Dec, 2006 - 06:32 AM
Post #1

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 2


My Contributions
hello,
ok this code works fine but it still dosnt compute the avearage with the lowest score dropped.
here is my code.
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);
    
}

User is offlineProfile CardPM
+Quote Post

czy1121
RE: Help With Dynamically Acllocated Arrays
14 Dec, 2006 - 07:14 AM
Post #2

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 9


My Contributions
j should start with 0
CODE

for ( j = 0; j < sz; j++ )
    {
        sum += arr[j];
    }


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 06:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month