u

Sunday, 3 June 2012

C++ Program that reads numbers into in array and sorts them in descending order

C++ Program that reads numbers (until -1 entered or more then 1000) into in array and sorts them in descending order


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

typedef char String[80];

int leesgetal(String info)
{
  int returnvalue, getal;

  do
  {
    cout << info;
    returnvalue=cscanf("%d", &getal);
    getch();
    cout << "\n";
   } while (!returnvalue);

   return getal;
}

int sort(const void *s, const void *t)
{
  return (*(int*)s - *(int*)t);
}

int main(void)
{
  clrscr();
  const int aantal=1000;
  int getallenrij[aantal];
  int teller = 0;
  int linepos;

  while (teller<1000 && getallenrij[teller-1]!=-1)
  {
    getallenrij[teller]=leesgetal("Enter array element [-1 to quit]");
    teller++;
  }
  qsort(getallenrij, teller, sizeof(int), sort);

  linepos = teller % 23;

  for (teller--; teller>0; teller--)
  {
    cout << getallenrij[teller] << "\n";
    if (teller % 23 == linepos)
    {
      cout << "*******";
    }
  }
cout<<endl<<"Press any key to continue"<<endl;
getch();
  return 0;
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More