-1

I am writing this partition code and there seems to be some problems with my RandomInRange function

Does anyone know how to write this Swap function? Ok,so I got the first few bugs fixed, the last problem is (I think) with my Swap() function.


#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <exception>
using namespace std;

int RandomInRange(int start,int end)
{
    return (rand()%(end-start+1)+start);
}

void Swap(int* a,int* b)
{
    int* temp=a;
    a=b;
    b=temp;
    return;
}


int Partition(int data[],int length,int start,int end)
{
    if(data==NULL || length<=0 || start<0 || end>=length)
    {//throw new exception("Invalid Parameters.");

    }

    int index=RandomInRange(start,end);
    Swap(&data[index],&data[end]);

    int small = start-1;
    for(index = start; index < end; ++index)
    {
        if(data[index]<data[end])
        {
            ++small;
            if(small!=index)
                Swap(&data[index],&data[small]);
        }

    }
    ++small;
    if(small != index)
        Swap(&data[index],&data[small]);

    return small;

}


int main(void)
{
    int a[]={9,0,-4,23,5,21,3,-1};
    Partition(a,8,1,8);
    for(int i=0;i<8;i++)
        cout<<a[i];
    return 0;
}

//results={9,0,-4,23,5,21,3,-1}, so it's not actually sorted..

And emm,just a tiny question. Has anyone read the STL source code? Does it help to learn C++ and data structure? I was hoping to achive higher grades for my finals.

Also it says:

No matching constructor for initialization of 'std::exception'

when I try the code:

throw new exception("Invalid Parameters.");

That's why I commented that line.

Rachel_Miller
  • 2,093
  • 2
  • 6
  • 9
  • Very few people except the authors have read the source code for the standard library. It's not of much use unless you already know a great deal. Reading a good book and doing the exercises is a better use of time. – molbdnilo Mar 30 '20 at 07:08
  • Tip: always use curly braces with loops and conditionals. Always. – molbdnilo Mar 30 '20 at 07:09
  • are there any great books you guys would reccommend? I already have the copy of C++ primer and Effective C++. – Rachel_Miller Mar 30 '20 at 07:10
  • Thanks for reminding me! I will remember to use curly braces. – Rachel_Miller Mar 30 '20 at 07:11
  • [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Mar 30 '20 at 07:15

2 Answers2

0
if(data==NULL || length<=0 || start<0 || end>=length)
        //throw new exception("Invalid Parameters.");

    int index=RandomInRange(start,end);
    Swap(&data[index],&data[end]);

When you commented the code line next after if you get the code equivalent

if(data==NULL || length<=0 || start<0 || end>=length) {
        //throw new exception("Invalid Parameters.");

    int index=RandomInRange(start,end);
}
    Swap(&data[index],&data[end]);

Thus the error in the last line claims about not defined index (arrays subscript is not an integer). Also when this line reached, the scope of integer is finished without any use it (Unused variable 'index').

273K
  • 29,503
  • 10
  • 41
  • 64
0

In the below code

int Partition(int data[],int length,int start,int end)
{
    if(data==NULL || length<=0 || start<0 || end>=length)
        //throw new exception("Invalid Parameters.");

    int index=RandomInRange(start,end);
    Swap(&data[index],&data[end]);

after

if(data==NULL || length<=0 || start<0 || end>=length)

you have commented throwing the exception. Hence the program control moves to the next statement which calls the method RandomInRange() whose arguments start and end are invalid. Hence this error.

Die-Bugger
  • 166
  • 8