CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  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

  2. #2
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Ordering an Array of ints

    Just as a pointer have a look at std::vectors or std::list , people here do not usually answer assignment related questions

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Ordering an Array of ints

    You don't want to simply "list" elements of the array----you actually want to re-order the elements in the array. When the array is sorted, simply outputting the elements will write them in order. At this point, identifying the max and min is also trivial----the first and last elements.

    So how do you sort? That depends. There is a sorting algorithm available in the standard library. If you'd rather write your own, there are a variety of algorithms available with varying degrees of complexity and speed. Bubble sort is one of the simplest but also the slowest; quicksort is one of the fastest, but is a bit more complex.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Ordering an Array of ints

    Quote Originally Posted by iiSoMeGuY 7x View Post
    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.
    Since you posted an answer, I'll give you this to consider:
    Code:
    #include <algorithm>
    #include <vector>
    #include <iostream>
    #include <numeric>
    
    using namespace std;
    
    int main()
    {
       int numScores;
       cin >> numScores;  // the number of scores
       std::vector<int> theScores(numScores);
       for (int num = 0; num < numScores; ++num)
           cin >> theScores[num];   // each score
    
       sort(theScores.begin(), theScores.end());  // sort the scores 
    
       // get the average
       double average = double(accumulate(theScores.begin(), theScores.end(), 0)) / (double)numScores;  
    
       // output the results
       cout << "The average is " << average << "\n";
       cout << "The numbers sorted are: " << "\n";
       copy(theScores.begin(), theScores.end(), ostream_iterator<int>(std::cout, " "));
    }
    Compile and run this program -- see what you get. You'll see that the C++ library, when learned, can do a lot of the things you're doing now "by hand".

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Ordering an Array of ints

    Code:
        cout << "How many test scores do you have?\n";
        cin >> SIZE;
        system("CLS");
        for (int num = 0; num < SIZE; num++)
            {
                input = new int[SIZE];
                cout << "Test Score " << (num + 1) << ": ";
                cin >> input[num];
            }
    The "new" should be outside of the loop. You only allocate once.

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

    Re: Ordering an Array of ints

    Quote Originally Posted by aamir121a View Post
    Just as a pointer have a look at std::vectors or std::list , people here do not usually answer assignment related questions
    Well, it's not for a class, it's from a book I bought Starting Out With C++ (4th Ed.), and I'm in the process of self teaching myself and have no one else to ask
    I also haven't learned how to do that yet, I'm on chapter 8 of 14, and what you're talking about is in chapter 11.

    Quote Originally Posted by Lindley View Post
    So how do you sort? That depends. There is a sorting algorithm available in the standard library. If you'd rather write your own, there are a variety of algorithms available with varying degrees of complexity and speed. Bubble sort is one of the simplest but also the slowest; quicksort is one of the fastest, but is a bit more complex.
    Wow, why would my book ask me to do something it hasn't taught me?

    Quote Originally Posted by Paul McKenzie View Post
    Since you posted an answer, I'll give you this to consider:
    ...
    Compile and run this program -- see what you get. You'll see that the C++ library, when learned, can do a lot of the things you're doing now "by hand".

    Regards,

    Paul McKenzie
    Ya, I saw something like this while googling how to sort in C++, but I'm trying to keep in the confines of my book (if that's possible in this situation), but if I have to I'll use this, thanks anyways.

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

    Re: Ordering an Array of ints

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
        cout << "How many test scores do you have?\n";
        cin >> SIZE;
        system("CLS");
        for (int num = 0; num < SIZE; num++)
            {
                input = new int[SIZE];
                cout << "Test Score " << (num + 1) << ": ";
                cin >> input[num];
            }
    The "new" should be outside of the loop. You only allocate once.
    Dude, I love you (no homo), I totally missed that! I now have the program working and just updated my post

  8. #8
    Join Date
    Aug 2009
    Posts
    440

    Re: Ordering an Array of ints

    I had an assignment in a second semester programming class where we had to compare different sorting algorithms. We went over the algorithms in class, but the teacher didn't have us actually write them. At the time, he felt while we could do some of the more simpler algorithm, quick sort, merge sort, and a couple others would be too difficult at the time. We probably could have figured it out, but I believe in the TAs for the class made some mistakes on the harder algorithms initially and had to fix it.

    The point I am trying to make, is that you are better off using the STL if you can. Whenever I code at home I make extensive use of it. Compared to vectors, dynamic arrays are a "pain" to deal with. I know you are trying to stick with the book, but Google is also an invaluable resource.

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

    Re: Ordering an Array of ints

    Quote Originally Posted by Alterah View Post
    The point I am trying to make, is that you are better off using the STL if you can. Whenever I code at home I make extensive use of it. Compared to vectors, dynamic arrays are a "pain" to deal with. I know you are trying to stick with the book, but Google is also an invaluable resource.
    Very true, without Google my programs wouldn't even run (my book doesn't teach system() commands), but even so I only use Google as a last resort. This is because I want to learn stuff in order, and once I get a good grasp on basic C++ I will start using Google and other sources in my code (so I don't just copy and paste, I want to know what I'm doing).

  10. #10
    Join Date
    Aug 2009
    Posts
    440

    Re: Ordering an Array of ints

    Quote Originally Posted by iiSoMeGuY 7x View Post
    Very true, without Google my programs wouldn't even run (my book doesn't teach system() commands), but even so I only use Google as a last resort. This is because I want to learn stuff in order, and once I get a good grasp on basic C++ I will start using Google and other sources in my code (so I don't just copy and paste, I want to know what I'm doing).
    I see your point. I don't know if others would recommend learncpp.com
    , but I found the site helpful when I looking into a second explanation. Not to mention, the site is structured basically like a book. Might be helpful

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

    Re: Ordering an Array of ints

    Quote Originally Posted by Alterah View Post
    I see your point. I don't know if others would recommend learncpp.com
    , but I found the site helpful when I looking into a second explanation. Not to mention, the site is structured basically like a book. Might be helpful
    Well, it's a little different from I'm used to, but it does look promising, thanks for the tip

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