pointer problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DOOO
    New Member
    • Mar 2008
    • 5

    pointer problem

    hello

    I have an error in my cod which is trying to sort an arry using pointer i'd be grateful if any one help me to solve my problem and here is my code

    [CODE=cpp]/*************** *************** *************** *************** *************** ******/

    #include <list>
    #include<iostre am>
    #include <algorithm>

    using namespace std;

    //////////////////////////////////////////
    double size(double* begin,double* end)
    {
    return ((end-begin)+1);
    }
    //////////////////////////////////////////
    void swap_ptr(double * a, double* b) {
    double temp = *a;
    *a = *b;
    *b = temp;
    }

    //////////////////////////////////////

    void sort(double *begin, double *end)
    {

    for(int i=0;i<size(begi n,end);i++)

    {

    if(*(begin+i)>* (begin+i+1))

    {

    swap_ptr((begin +i),((begin+i+1 )));

    }

    }

    }
    ///////////////////////
    int main()
    {
    double* arr = new double[5];

    for(int j=0;j<5;j++){
    cin >> arr[j];

    sort(arr,(arr)+ 4);
    }
    cout << "the Sorting array is " << arr << endl;
    delete[] arr;
    return 0;
    }

    /*************** *************** *************** *************** *************** ******/[/CODE]
    Last edited by Ganon11; Mar 27 '08, 08:50 PM. Reason: Please use the [CODE] tags provided.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    I have one question for you: If you're using <algorithm>, a) why not use sort() to do this for you and b) why not use the provided swap function, passing in a double pointer, if this is an academic excercise?

    Comment

    • DOOO
      New Member
      • Mar 2008
      • 5

      #3
      Originally posted by Laharl
      I have one question for you: If you're using <algorithm>, a) why not use sort() to do this for you and b) why not use the provided swap function, passing in a double pointer, if this is an academic excercise?
      yup it is a task i asked to do it

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by DOOO
        I have an error in my cod which is trying to sort an arry using pointer i'd be grateful if any one help me to solve my problem and here is my code
        What is the error, we can fix a problem if we don't know what the problem is!

        One of the reasons your sorting algorithm is not working is that it is not a sorting algorithm, try looking up some sorting algorithms on Wikipedia.

        Comment

        • DOOO
          New Member
          • Mar 2008
          • 5

          #5
          Originally posted by Banfa
          What is the error, we can fix a problem if we don't know what the problem is!

          One of the reasons your sorting algorithm is not working is that it is not a sorting algorithm, try looking up some sorting algorithms on Wikipedia.

          actually i have a quite good knowledge about sorting algorithms (merg,heap,bubb le ,...etc) but my program aimed to sort an array using pointer it is an application on pointer not on sorting algorithm and the problem is when i enter array to be sorted the program give me address in the memory . is it clear now?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by DOOO
            the problem is when i enter array to be sorted the program give me address in the memory . is it clear now?
            Not really, your program contains no arrays. It contains a pointer you appear to be using a bit like an array but that is all.

            Do you mean you are getting a SegFault/Memory Access Exception?

            Comment

            • DOOO
              New Member
              • Mar 2008
              • 5

              #7
              [QUOTE=Banfa]Not really, your program contains no arrays. It contains a pointer you appear to be using a bit like an array but that is all.

              Do you mean you are getting a SegFault/Memory Access Exception?[/QUO

              DAMGE:after normal block (#45)at 0x00491d20

              that is the error ,i can't understand it

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Look at your sort.

                You use begin+i+1. Assume a 5 element array and you are on the last element so i is 4. That makes begin+4+1, which is begin+5 and that is outside your array bounds.

                Comment

                • DOOO
                  New Member
                  • Mar 2008
                  • 5

                  #9
                  Originally posted by weaknessforcats
                  Look at your sort.

                  You use begin+i+1. Assume a 5 element array and you are on the last element so i is 4. That makes begin+4+1, which is begin+5 and that is outside your array bounds.

                  i am try to avoid this error but still have a problem

                  Code:
                  #include <list>
                  #include<iostream>
                  #include <algorithm>
                  
                  using namespace std;
                  
                  void sort(double* begin,double* end)
                  
                  {  
                  
                  for(int j=0;j<(end-begin)+1;j++)
                  {
                   	for(int i=0;i<(end-begin)+1;i++)
                  	{
                  		if(*(begin+i)>*(end-i))
                  		{ 
                  	//	swap(*(begin+i),*(end-i));
                  			double temp = *(begin+i);
                              *(begin+i) = *(end-i);
                              *(end-i) = temp;
                  
                  			
                  
                  		}else {continue;}
                  	}
                  }	
                  }
                  
                  int main()
                  {
                  double* arr = new double[6];
                  
                  for(int j=0;j<6;j++){
                  cin >> arr[j];
                  
                  sort(arr,(arr)+5);
                  }
                  
                  
                  //delete[] arr;
                  return 0;
                  }
                  Last edited by Banfa; Mar 28 '08, 05:27 PM. Reason: Added the missing Es

                  Comment

                  Working...