Question:
Somebody please help me write this C++ program?
Robert
2011-09-23 10:55:05 UTC
Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts then in ascending order. Another function shoud be called that calculates the average score. The program should display the sorted list of scores and averages with appropiate headings. Use pointer notation rather than array notation whenever possible. Input validation : do not accept negative numbers for test scores
Four answers:
cja
2011-09-23 11:53:15 UTC
Of course there are C++ STL containers that would make this problem much easier, but I assume you haven't gotten to that yet. So, your C++ program will end up looking a lot like C, something like what I've done below. I've used the sort function from , you may have to write your own.



#include

#include

#include

#include



using namespace std;



int getInt(const string&);

void sortIntArray(int *, int);

float computeAvg(const int * const, int);



int main(int argc, char *argv[]) {

    int N, *scores;



    while ((N = getInt("Enter number of scores: ")) <= 0);

    scores = new int[N];

    cout << "Enter " << N << " scores, one per line ..." << endl;

    for (int n = 0; n < N; n++) {

        scores[n] = getInt("> ");

    }

    sortIntArray(scores, N);

    cout << "Scores: ";

    copy(scores, scores + N, ostream_iterator(cout, " "));

    cout << endl << "Average: " << computeAvg(scores, N) << endl;



    delete [] scores;

    return 0;

}



void sortIntArray(int *a, int len) {

    sort(a, a + len);

}



float computeAvg(const int * const a, int len) {

    int total = 0;

    for (int i = 0; i < len; i++) {

        total += a[i];

    }

    return static_cast(total) / len;

}



int getInt(const string &prompt) {

    stringstream ss;

    string line;

    bool inputOk = false;

    int n;

 

    cout << prompt;

    do {

        getline(cin,line);

        ss.clear(); ss.str(line);

        if ((!(ss >> n)) || (ss.good())) {

            cout << "invalid input, try again" << endl << "> ";

        } else {

            inputOk = true;

        }

    } while (inputOk == false);

    return n;

}



#if 0



Sample run:



Enter 3 scores, one per line ...

> 85

> 90

> 78

Scores: 78 85 90

Average: 84.3333



#endif
chingonsonson
2011-09-23 11:36:03 UTC
dude what's the point of going to school and not learning how to do your work, I mean if your really want to work or do something related to programming you have to eventually work yourself.



but now if you ha ve question about a particular thing well i can help you at least try to do some code and then ask your questions.



now you can make a user defined array with a class , im not sure if you learned this already. it looks something like this:



class YourClassName

{

private:

//declare stuff



public:

//more sstuff

};
SteveO
2011-09-23 11:00:56 UTC
Here's the start of what you need:



#include

using namespace std;



// you can put function prototypes here

// or just define them here...your choice



int main(void)

{

// some variables here

// some user input here

// some other code here

// sorting code here

// your display code

return 0;

}
?
2011-09-23 11:19:46 UTC
Print "Do your own homework"


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...