| | |
bubble sort function w/ array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2006
Posts: 73
Reputation:
Solved Threads: 0
The assignment is to write a C++ function using "pointer notation" that will write out the elements of an array of int in reverse order.
The problem I'm having is with the sortAscend function and what the parameters should be to accept an array. I'm getting an error cause I'm passing an object and the function expects an int pointer.
The problem I'm having is with the sortAscend function and what the parameters should be to accept an array. I'm getting an error cause I'm passing an object and the function expects an int pointer.
C++ Syntax (Toggle Plain Text)
class arrayList { public: void print() const; //Function to output the elements of the list //Postcondition: Elements of the list are output // on the standard output device. void sortAscend(int *list, int length); //Function to sort the array in Ascending order //Postcondition: Elements of the list are sorted // in Ascending order void insertEnd(int insert); //Function to insert elements at the end of list //Postcondition: list[length] = insert; length++; arrayList(int size = 10); //constructor //Creates an array of the size specified by the //parameter size. The default size is 10. //Postcondition: The list points to the array, // length = 0, and maxSize = size; ~arrayList(); //destructor //Deallocate the memory occupied by the array. private: int *list; //array to hold the list elements int length; //variable to store the length of the list int maxSize; //variable to store the maximum size of the list };
C++ Syntax (Toggle Plain Text)
#include "arrayList.h" using namespace std; void arrayList::print() const { for (int i = 0; i < length; i++) cout << list[i] << " "; cout << endl; } //end print function void arrayList::sortAscend(int *list, int length) { for(int i = 0; i < length; i++) { for(int j = 0; j < length - i; j++) { if(list[i] > list[j + 1]) { int temp = list[j]; //swap list[j] = list[j + 1]; list[j + 1] = temp; } } } } //end sortAscend function void arrayList::insertEnd(int insert) { if (length >= maxSize) //the list is full cout << "Cannot insert in a full list." << endl; else { list[length] = insert; // insert the item at the end of the list length++; //increment the length } } //end function insert arrayList::arrayList(int size) { if (size <= 0) { cout << "The array size must be positive. Creating " << "an array of the size 10." << endl; maxSize = 10; } else maxSize = size; length = 0; list = new int[maxSize]; } //end constructor arrayList::~arrayList() { delete [] list; } //end destructor
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "arrayList.h" using namespace std; int main() { arrayList list(10); int number; cout << "Enter 10 integers: "; for (int count = 0; count < 10; count++) { cin >> number; list.insertEnd(number); } cout << "Before sorting, array[10]: "; list.print(); cout << endl; list.sortAscend(*list, 10); }
Since you are calling the function of the object (ie the member function) you don't need to pass anything to the function, since the object contains all that is needed to sort its elements. Just don't forget to update the length of the internal list and it should be fine.
Also write another function which checks whether the Object contains atleast one element before sorting so as to avoid crashes.
Something like:
Oh, and btw, your sorting logic is flawed. A simple fix would be:
Also write another function which checks whether the Object contains atleast one element before sorting so as to avoid crashes.
Something like:
c Syntax (Toggle Plain Text)
void arrayList::sortAscend () { // here length and list are the member variables of the object // & and hence can be directly accessed for(int i = 0; i < length; i++) { for(int j = 0; j < length - i; j++) { if(list[i] > list[j + 1]) { int temp = list[j]; //swap list[j] = list[j + 1]; list[j + 1] = temp; } } } } //end sortAscend function
Oh, and btw, your sorting logic is flawed. A simple fix would be:
c Syntax (Toggle Plain Text)
for(int i = 0; i < length - 1; i++) { for(int j = i + 1; j < length; j++) { if(list[i] > list[j]) { int temp = list[i]; //swap list[i] = list[j]; list[j] = temp; } } }
Last edited by ~s.o.s~; Feb 11th, 2007 at 2:56 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Well from what I can dig out of my very shallow pool of knowledge it seems that in your prototype you are telling it that you want it to accept a pointer but when you call the function you are passing by value instead of by reference IE. &list
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
It would really be interesting to know which book you are referring.
A book with flawed algorithm of such a simple sort is not a book worth reading....
A book with flawed algorithm of such a simple sort is not a book worth reading....
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Jul 2006
Posts: 73
Reputation:
Solved Threads: 0
C++ Programming: Program Design Including Data Structures 3rd ed. by D.S. Malik
I am going to copy the algorithm again maybe I screwed it up.
I am going to copy the algorithm again maybe I screwed it up.
C++ Syntax (Toggle Plain Text)
void bubbleSort(elemType list[], int length) { for (int i = 1; i < length; i++) { for (int j = 0; j < length - i; j++) { if (list[j] > list[j + 1]) { elemType temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } }
![]() |
Similar Threads
- Bubble sort (C++)
- Sort a dynamic array of structs (Visual Basic 4 / 5 / 6)
- Bubble sort & File output jibrish errors??? (C)
Other Threads in the C++ Forum
- Previous Thread: hw help needed - dont want code
- Next Thread: file problem
Views: 6026 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






