Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 1.52 KB | Hits: 22 | Expires: Never
Copy text to clipboard
  1. // ---------------------------------------------------------------
  2. //
  3. // This program reads a list of numbers from the command line and
  4. // determines whether or not they are in descending order.
  5. //
  6. // usage:
  7. //    inlabAa  number [number ...]
  8. // ---------------------------------------------------------------
  9.  
  10. #include <cstdlib>
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. const int SIZE = 10                // size of the array
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     int i;                // loop counter
  19.     bool descending;      // true if the list is in descending order
  20.     int list[size];       // array to hold the list
  21.     int lstSize;          // the number of integers in the list
  22.  
  23.     // Set listSize to the number of integers on the command line,
  24.     // or the size of the array, whichever is smaller.
  25.     if ( argc-1 > SIZE )
  26.         listSize = SIZE;
  27.     else
  28.         listSize = argc - 1;
  29.  
  30.  
  31.     // convert the numbers on the command line from ASCII to
  32.     // integer and store them in the list.
  33.     for (i = 1; i < listSize; i++)
  34.         list[i] = atoi(argv[i]);
  35.  
  36.     // Check each pair of adjacent integers to see if the one with
  37.     // the lower index has a lower value.
  38.     for (i = 0, descending = true; (i < listSize) && descending; ++i)
  39.         descending = (list[i] > list[i+1]);
  40.  
  41.     // Inform the user of the result
  42.     if (descending)
  43.         cout << "The list is in descending order.\n";
  44.     else
  45.         cout << "The list is NOT in descending order.\n";
  46.  
  47.     return(descending);
  48. }