-4

I am new at this - a project for my Intro to Object Oriented Programming class. Any help would be so appreciated. So I started by creating the class BubbleSort and the member functions. sort is supposed to take an array of integers of length 10 and print them from greatest to smallest. I included a link to a bit of code my teacher gave us to help start us off with the assignment. Sorry if this is too vague, I am sure there are problems with my overall design. Thank you for your time. Also I get the following errors in Xcode Expected '(' for function-style cast or type construction and Unused variable 'nArray' enter image description here

#include <iostream>
using namespace std;

// class declaration
class BubbleSort
{
public:
    BubbleSort(); //initialize nArray to contain set of unsorted numbers
    void sort(int nArray []); //sort array
private:
    int nArray[];  //stores the unsorted numbers
};


BubbleSort :: BubbleSort()  {    //default constructor
    int nArray [] = {256, 734, 201, 3, 123, 40, 99, 257, 7, 609};
}




void BubbleSort :: sort (int nArray []) { //prints the highest number

    int highest = nArray[0]; //highest value of the array
    int index = 0; //index of the highest value of the arrray
    int count = 0; //counts the time you check the array

while (count != 10) {

    for (int i = 0; i < 10; i++) {
        if (nArray[i] > highest) {
            highest = nArray[i];
            index = i;
        }
    }

    cout << "The highest value at that moment: " << highest << endl;
    nArray[index] = 0;
    highest = 0;
    count ++;
}
}



int main() {

    BubbleSort myArray; //create object
    myArray.sort(int myArray[])

return 0;
}
  • SO is not a do-my-homework service. Off-topic. Compile with all warnings and debug info : `g++ -Wall -Wextra -g` then **use the debugger** `gdb` to run your program step by step, query its state, hence understanding what is wrong. Spend more time reading about C++ and about your compiler and your debugger. Look into some [C++ reference](http://en.cppreference.com/w/cpp) – Basile Starynkevitch Oct 18 '17 at 04:44
  • 3
    Yes, the way you go about handling arrays is wrong on a fundamental level. I suggest you read up on that before attempting the task. – DeiDei Oct 18 '17 at 04:45
  • 1
    Why is your sort algorithm a class with a private array as member? That's useless! It should be a function. –  Oct 18 '17 at 05:56

2 Answers2

2

You declare a field which is an array without any specified dimension. This is wrong in C++. (Notice that C has flexible array members, but standard C++ don't have them).

At the very least, declare the dimension as a macro:

#define ARRAY_SIZE 10

or as a constant:

static constexpr int ARRAY_SIZE= 10;

Then declare the field with that size:

int nArray[ARRAY_SIZE];

In genuine C++ better use standard containers. You should want to use std::vector or perhaps std::array ....

Compile with all warnings and debug info : g++ -Wall -Wextra -g with GCC then use the debugger gdb to run your program step by step, query its state, hence understanding what is wrong.

Your sort being a member function don't need to get the array as an argument.

You are declaring several different nArray in several scopes. This is confusing, don't use synonyms (so don't declare several nArray-s, use different names).

Spend more time reading a good C++ programming book, some C++ reference, and the documentation of your compiler and your debugger.

Notice that Xcode is not your compiler, but just an IDE (which is running some external compiler -probably GCC or Clang- underneath). I would recommend to run your compiler in a terminal with a command line.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0
  1. It's not recommended to use using namespace std; — because it imports all the identifiers from std. See this question on Stack Overflow.

  2. As suggested before, it is preferable to use standard containers like std::vector

  3. I understand that the assignement your teacher gave you is part of a learn process, but to sort elements of an array into ascending order you can also use std::sort