Trouble displaying two dimentional array in descending order

This is a discussion on Trouble displaying two dimentional array in descending order within the C++ Programming forums, part of the General Programming Boards category; My os: win2000 My compiler: MS Visual C++ 6.0 My computer: Desktop No matter what I do I cannot get ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Angry Trouble displaying two dimentional array in descending order

    My os: win2000
    My compiler: MS Visual C++ 6.0
    My computer: Desktop

    No matter what I do I cannot get this program to display the result in descending order. I want to display the result in order from the highest value to the lowest including the corresponding month.
    My codes below:

    // last updated Jan 06, 2002

    // Purpose of this program: display a list of months and values,
    // sorted the order from highest value to lowest

    #include <iostream>
    using namespace std;

    void main(void)
    {

    char months[12][10] = {"January", "February", "March", "April",
    "May", "June", "July", "August",
    "September", "October", "November", "December"};

    float r_fall[12]; // store data in each element of the array

    for(int index=0; index<12; index++)
    {
    cout<<"Please enter rainfall amount for " <<months[index] <<": ";
    cin>>r_fall[index];
    while (r_fall[index] < 0)
    {
    cout<<"The data you entered is not a positive value: ";
    // flush bad data input
    // put a flush stmt. on next line

    cout<<"re-enter a rainfall amount for "<<months[index] << ": ";
    cin>>r_fall[index];

    } // end of "while"
    } // end of "for"
    // hold on to this code --->Sort_Rainfalls(r_fall, months);

    // sort rainfalls and produce a report
    // sort the data in each element of the array
    // need codes for the sort . Bubble sort might not work
    // cout.precision(2); //optional
    // cout.setf(ios::fixed|ios::showpoint); // optional

    cout << "\nThe rainfall in descending order (from highest to lowest)" << endl;
    for(int count=0; count<12; count++) //in ascending order
    //original for(int count=12; count>0; count--)
    {
    cout << "Rainfall for" << " " << months[count] << ": " << r_fall[count] << " inches" << endl;

    }

    } // end of main

    // ERROR list
    // none

    ============================
    The result that I got:

    Please make sure you enter a positive whole or decimal value
    Please enter rainfall amount for January: 5
    Please enter rainfall amount for February: 3
    Please enter rainfall amount for March: 8
    Please enter rainfall amount for April: .88
    Please enter rainfall amount for May: .44
    Please enter rainfall amount for June: .33
    Please enter rainfall amount for July: 7
    Please enter rainfall amount for August: 12
    Please enter rainfall amount for September: 32
    Please enter rainfall amount for October: 45
    Please enter rainfall amount for November: 33
    Please enter rainfall amount for December: 5

    The rainfall in descending order (from highest to lowest)
    Rainfall for January: 5 inches
    Rainfall for February: 3 inches
    Rainfall for March: 8 inches
    Rainfall for April: 0.88 inches
    Rainfall for May: 0.44 inches
    Rainfall for June: 0.33 inches
    Rainfall for July: 7 inches
    Rainfall for August: 12 inches
    Rainfall for September: 32 inches
    Rainfall for October: 45 inches
    Rainfall for November: 33 inches
    Rainfall for December: 5 inches
    Press any key to continue

    as you can see the values and the months are only in ascending order.

    Help please !!!!!!!!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    cout << "\nThe rainfall in descending order (from highest to lowest)" << endl;
    for(int count=0; count<12; count++) //in ascending order
    //original for(int count=12; count>0; count--)
    {
    cout << "Rainfall for" << " " << months[count] << ": " << r_fall[count] << " inches" << endl;


    You have to sort rainfall.

    With your code you only print monts in descending order or in ascending order.
    You have to sort r_fall array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 dimentional array
    By theone1 in forum C++ Programming
    Replies: 10
    Last Post: 11-11-2007, 01:30 PM
  2. 2 dimentional array of pointers
    By Cerber4s in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2003, 07:56 PM
  3. Need codes for displaying figures in descending mode.
    By rbaba in forum C++ Programming
    Replies: 0
    Last Post: 12-14-2001, 08:47 PM
  4. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21