CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Question Ordering an Array of ints

    Assignment: Test Averaging
    Write a program that dynamically allocates an array large enough to hold any number of test scores the user wishes to enter. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should then be called that calculates the average score. The program should display the sorted list of scores and average, with appropriate headings.

    Help Information:
    I have been learning C++ from this book I bought a while back, and after every chapter it has a 'Review Questions and Exercises' section, of which I chose to do the 'Programming Challenges' to make sure I understand the concepts discussed in the chapter. This is good practice, but the major downside is that in some challenges it asks you to do something it hasn't gone over in detail, or just figures you should know.
    EDIT: I have the program working, and yes, I know, it's not perfect. If you add two or more test scores with the same value it will mess up the listing. But, since this isn't for a grade I don't really care. If you want to post something to help me out, be my guest, otherwise thank you for your time

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    
    void getOrder(int [], int);
    float getAvg(int [], int);
    
    int main()
    {
        int *input, SIZE;
    
        cout << "How many test scores do you have?\n";
        cin >> SIZE;
        system("CLS");
        input = new int[SIZE];
        for (int num = 0; num < SIZE; num++)
            {
                cout << "Test Score " << (num + 1) << ": ";
                cin >> input[num];
            }
        getOrder(input, SIZE);
        delete [] input;
        system("PAUSE");
        return 0;
    }
    
    void getOrder(int input[], int SIZE)
    {
        int test[SIZE], order[SIZE], high;
        for (int num = 0; num < SIZE; num++)
            {
                test[num] = input[num];
            }
        for (int count = 0; count < SIZE; count++)
            {
                high = test[count];
                for (int num = 0; num < SIZE; num++)
                    {
                        if (test[num] > high)
                            high = test[num++];
                    }
                order[count] = high;
                for (int num = 0; num < SIZE; num++)
                    {
                        if (high == test[num])
                            {
                                test[num] = 0;
                            }
                    }
            }
        cout << "Test Scores in Numerical Order:\n";
        for (int num = 0; num < SIZE; num++)
            {
                cout << (num + 1) << ". " << order[num] << endl;
            }
        cout << "\nAverage: " << fixed << showpoint << setprecision(2) << getAvg(input, SIZE) << endl;
    }
    
    float getAvg(int input[], int SIZE)
    {
        int total = 0;
        float avg = 0;
    
        for (int num = 0; num < SIZE; num++)
            {
                total += input[num];
            }
        avg = total / SIZE;
        return avg;
    }
    Last edited by iiSoMeGuY 7x; June 7th, 2011 at 06:44 PM. Reason: Edit code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured