Remove an element from an array

Chubbi >
Here is the project I have to do :

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores (for one student). Do not accept negative numbers for test scores! Once all the scores are entered, the array should be passed to a function that drops the lowest score and calculates the average of the remaining scores. The result must be displayed in main. Your function must have the void return type (think about proper list of function parameters!).
Use pointer notations rather than subscripts whenever possible!

Here is code again:

I still trying work avg. but i need this code to work first:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>

using namespace std;

void dropgrade (double *[], int);
void calaverage (double *);

int main ()
{
    cout << "Welcome" << endl;
    
    double *score;
    int userinput;
    
    cout << "How many test score would you like to enter ? " << endl;
    cin >> userinput;
    
    while (userinput <= 0)
    {
        cout << "Please enter number that greater than 0! " << endl;
        cout << "How many test score would you like to enter ? " << endl;
        cin >> userinput;
        
    }
    
    score = new double [userinput];
    
    
    cout << "Enter the test score below" << endl;
    cout << endl;
    
    for (int index = 0; index < userinput; index ++)
    {
        cout << "Test number " << (index + 1) << ": " << endl;
        cin >> score [index];
        
        if (score [index] <= 0)
        {
            cout << "The test number " << (index + 1) << " : is invalid" << endl;
            cout << "Please enter only 0 or more!" << endl;
            cin >> score [index];
        }
    }

    dropgrade(&score, userinput);
    delete [] score;
    return 0;
}
void dropgrade (double *array[] , int size)
{
    double holder =0;
    for (int counter = size - 1; counter >= 0; counter --)
    {
        for (int index = 0; index < size - 1; index ++)
        {
            if (array [index + 1] < array [index])
            {
                holder = *array [index + 1];
                array [index +1] = array [index];
                array [index] = &holder;
                
            }
        }
    }
    
    for (int display = 0; display < size; display ++)
    {
        cout << array [display] << ", ";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>

using namespace std;

// void dropgrade (double *[], int);
// pass pointer to the first element of the array, and its size
int dropgrade ( double score[], int size ); // return new size

void calaverage ( const double* );

int main ()
{
    cout << "Welcome" << endl;

    double *score;
    int n_scores;

    cout << "How many test score would you like to enter ? " << endl;
    cin >> n_scores;

    while (n_scores <= 0)
    {
        cout << "Please enter number that greater than 0! " << endl;
        cout << "How many test score would you like to enter ? " << endl;
        cin >> n_scores;

    }

    score = new double [n_scores];


    cout << "Enter the test score below" << endl;
    cout << endl;

    for (int index = 0; index < n_scores; index ++)
    {
        cout << "Test number " << (index + 1) << ": " << endl;
        cin >> score [index];

        if (score [index] <= 0)
        {
            cout << "The test number " << (index + 1) << " : is invalid" << endl;
            cout << "Please enter only 0 or more!" << endl;
            cin >> score [index];
        }
    }

    std::cout << "scores as entered:\n" ;
    for( int i = 0; i < n_scores; ++i ) std::cout << score[i] << ' ' ;
    std::cout << '\n' ;

    //dropgrade(&score, n_scores);
    if( n_scores > 1 ) n_scores = dropgrade( score, n_scores );

    std::cout << "after dropping the lowest score:\n" ;
    for( int i = 0; i < n_scores; ++i ) std::cout << score[i] << ' ' ;
    std::cout << '\n' ;


    delete [] score;
}

// return the position of the lowest score in the array (or -1 if empty)
int position_of_lowest_score( const double score[] , int size )
{
    if( size > 0 )
    {
        int pos_lowest = 0 ;
        double lowest_score = score[0] ;

        for( int i = 1 ; i < size ; ++i )
        {
            if( score[i] < lowest_score )
            {
                lowest_score = score[i] ;
                pos_lowest = i ;
            }
        }

        return pos_lowest ;
    }

    return -1 ;
}

// drop the lowest score and return the new size
int dropgrade( double score[] , int size)
{
    if( size > 1 ) // at lest two scores
    {
        int pos_lowest = position_of_lowest_score( score, size ) ;

        // drop the lowest score by moving the score in the last position
        // to the position that holds the lowest score
        score[pos_lowest] = score[size-1] ;

        return size-1 ; // number of scores is one less after dropping the lowest
    }

    return size ; // nothing was dropped
}


TODO: Use pointer notations rather than subscripts whenever possible!
Last edited on
Chubbi > can you please explain what you did here?
if( n_scores > 1 ) n_scores = dropgrade( score, n_scores );

dropgrade() returns the new size of the array (after dropping the lowest score). Update the number of scores n_scores with this returned value.



Chubbi > Can i use pointer for calculate the avg?

Yes. For instance:
double average( const double* pointer_to_first_score, const double* pointer_to_last_score ) ;
Topic archived. No new replies allowed.