473,414 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

pointer problem

5
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

Expand|Select|Wrap|Line Numbers
  1. /*********************************************************************************/
  2.  
  3. #include <list>
  4. #include<iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. //////////////////////////////////////////
  10. double size(double* begin,double* end)
  11. {
  12.     return ((end-begin)+1);
  13. }
  14. //////////////////////////////////////////
  15. void swap_ptr(double* a, double* b) {
  16.     double temp = *a;
  17.     *a = *b;
  18.     *b = temp;
  19. }
  20.  
  21. //////////////////////////////////////
  22.  
  23. void sort(double *begin, double *end)
  24. {
  25.  
  26.     for(int i=0;i<size(begin,end);i++)
  27.  
  28.     {
  29.  
  30.         if(*(begin+i)>*(begin+i+1))
  31.  
  32.         { 
  33.  
  34.         swap_ptr((begin+i),((begin+i+1)));       
  35.  
  36.         }
  37.  
  38.     }
  39.  
  40. }
  41. ///////////////////////
  42. int main()
  43. {
  44.     double* arr = new double[5];
  45.  
  46.     for(int j=0;j<5;j++){
  47.      cin >> arr[j];
  48.  
  49.     sort(arr,(arr)+4);
  50.     }
  51.     cout << "the Sorting array is " << arr << endl;
  52.     delete[] arr;
  53.     return 0;
  54. }
  55.  
  56. /*********************************************************************************/
Mar 27 '08 #1
8 1219
Laharl
849 Expert 512MB
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?
Mar 27 '08 #2
DOOO
5
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
Mar 28 '08 #3
Banfa
9,065 Expert Mod 8TB
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.
Mar 28 '08 #4
DOOO
5
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,bubble ,...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?
Mar 28 '08 #5
Banfa
9,065 Expert Mod 8TB
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?
Mar 28 '08 #6
DOOO
5
[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
Mar 28 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 28 '08 #8
DOOO
5
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <list>
  3. #include<iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. void sort(double* begin,double* end)
  9.  
  10. {  
  11.  
  12. for(int j=0;j<(end-begin)+1;j++)
  13. {
  14.      for(int i=0;i<(end-begin)+1;i++)
  15.     {
  16.         if(*(begin+i)>*(end-i))
  17.         { 
  18.     //    swap(*(begin+i),*(end-i));
  19.             double temp = *(begin+i);
  20.             *(begin+i) = *(end-i);
  21.             *(end-i) = temp;
  22.  
  23.  
  24.  
  25.         }else {continue;}
  26.     }
  27. }    
  28. }
  29.  
  30. int main()
  31. {
  32. double* arr = new double[6];
  33.  
  34. for(int j=0;j<6;j++){
  35. cin >> arr[j];
  36.  
  37. sort(arr,(arr)+5);
  38. }
  39.  
  40.  
  41. //delete[] arr;
  42. return 0;
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
Mar 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.