LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-22-2005, 05:21 PM   #1
huno
Member
 
Registered: May 2002
Distribution: Slackware 9.1
Posts: 161

Rep: Reputation: 30
ascending order


how can i check if the array element in ascending order using recursive ?
 
Old 07-22-2005, 06:34 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Shall we just a pick a programming language at random and hope that's the one you're asking about?
 
Old 07-22-2005, 07:23 PM   #3
coldAndUgly
Member
 
Registered: Dec 2003
Location: New Zealand
Distribution: Slackware 10.2
Posts: 36

Rep: Reputation: 15
Quote:
Shall we just a pick a programming language at random and hope that's the one you're asking about?
Why not?
Here's an implementation in C++.
Code:
#include <iostream>

using std::cout;
using std::endl;

bool isInAscendingOrder(const int*, const unsigned int);
static bool checkArray(const int*, const int*, const int* const);

int main()
{
    const unsigned int s = 10;
    int a[s] = { 0, 1, 2, 4, 3, 5, 6, 7, 8, 9 };

    if (isInAscendingOrder(a, s))
        cout << "Array \"a\" is in ascending order." << endl;
    else
        cout << "Array \"a\" is not in ascending order." << endl;

    return 0;
}

bool isInAscendingOrder(const int* arr, const unsigned int size)
{
    if (size == 0)
        return false;

    const int* e1 = arr;
    const int* e2 = arr + 1;
    const int* end = arr + size;        // First address beyond the end of the array is always valid.

    return checkArray(e1, e2, end);
}

static bool checkArray(const int* a, const int* b, const int* const c)
{
    if (b == c)                         // We've checked every element of the array and it's in order.
        return true;
    else if (*b < *a)                   // We've found two elements that aren't in order.
        return false;

    return checkArray(++a, ++b, c);     // Check the next element.
}

Last edited by coldAndUgly; 07-22-2005 at 07:24 PM.
 
Old 07-22-2005, 07:42 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
And here's a C one that even tells you were the order breaks and uses one less function than that C++ version
Code:
#include <stdio.h>

int chk_ascending(int *array, int index, int size)
{
  if(index >= size - 1)
    return 0;
  if(array[index] > array[index + 1])
    return index + 1;

  return chk_ascending(array, index + 1, size);
}

int main(void)
{
  int a[] = { 0, 1, 2, 4, 3, 5, 6, 7, 8, 9 };
  int num_a = 10;
  int rv;

  if(!(rv = chk_ascending(a, 0, num_a)))
    puts("Array \"a\" is in ascending order.");
  else
    printf("Array \"a\" breaks ascending order at index %d.\n", rv);

  return 0;
}

Last edited by itsme86; 07-22-2005 at 07:45 PM.
 
Old 07-22-2005, 08:03 PM   #5
coldAndUgly
Member
 
Registered: Dec 2003
Location: New Zealand
Distribution: Slackware 10.2
Posts: 36

Rep: Reputation: 15
Well done, I like your version better.
My coding skills are a little rusty
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bootloader order prh Ubuntu 1 09-16-2005 02:23 AM
What order and how do I ..... adds2one Slackware 4 09-01-2005 12:53 AM
The first step to ascending newb status, acknowledging you're a newb :P LordRaven LinuxQuestions.org Member Intro 1 08-24-2004 06:05 PM
which to order? i m gr8 Linux - Newbie 6 04-17-2004 09:17 AM
Set-up order ?? Justinw Linux - Networking 3 04-20-2001 03:06 AM


All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration