Index sorting program problem

I have to bubblesort an array, and compare the bubblesorted array with the non-bubblesort array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0, passes = 0;
       bool swapped = true;
    do {swapped = false;
            passes++;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            }
size--;} while (swapped == true);
} 

int GetArray (int array[], size_t array_size)
{
    int a = 0;
    while(a < 1 || a > array_size)
    {
       //number of elements in array
        cin >> a;
    }
    int i;
    for(i = 0; i < a; i++)
    {
        cin >> array[i];
    }
    return i;
}

int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    int otherarray[MAX_ARRAY];
    memcpy(array, otherarray, sizeof(array));
    Bubblesort(number_of_elements, array); 
    cout<<array<<" "<<otherarray;
}


Output: 0x7ffefe55f180 0x7ffefe560120
What???

[code before also included the following and only outputted multiple "1"'s]:
[code]
void CheckArray (int otherarray[], int array[], size_t array_size)
{ //testing our NEW array through, and see each value's corresponding location in the OLD array
for (int b=0; b<array_size; b++) //check every number in the "new array"
{ int a =0;
for (int i=0; i<array_size; i++)
{if (array[b]==otherarray[a])
cout<<a+1<<" ";
else
a++;
}
}
}

[main...]
CheckArray(otherarray, array, number_of_elements);
[/code]
Last edited on
1
2
const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];


This is not standard c++. An array has to have a fixed size at compile time. If you want to do this you should allocate memory, or just use std::vector.

Regarding the weird output.

cout<<array<<" "<<otherarray;

That's not how you print out an array, something you would have quickly realized if you would have taken 20 seconds to google "how to print array c++".

You'll need a for-loop that loops for as many elements there are.
1
2
3
4
for(int i = 0; i < number_of_elements; i++)
{
    cout << array[i] << " " << otherarray[i];
}



Last edited on
I'm not sure how I would do either (allocating memory, using vector in an array?). In addition, strangely enough both arrays display the following:
0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0
A very confusing output.
The problem is your call to memcpy()
memcpy(array, otherarray, sizeof(array));
You basically copy the uninitialized otherarray to array. All the data you entered is lost.
This should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
  const size_t MAX_ARRAY = 1000;
  int array[MAX_ARRAY];

  size_t number_of_elements = GetArray(array, MAX_ARRAY);
  int otherarray[MAX_ARRAY];
  memcpy(otherarray, array, sizeof(array));
  Bubblesort(number_of_elements, array); 

  cout << "Sorted array: " << endl;
  for (int i = 0; i < number_of_elements; i++)
    cout << array[i] << " ";

  cout << "Unsorted array: " << endl;
  for (int i = 0; i < number_of_elements; i++)
    cout << otherarray[i] << " ";

  cout << "\n\n";
  system("pause");
}


@TarikNeaj
I think in C++11 it is allowed. At least my VC++2010 compiles it.
1
2
const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];

@Thomas1965,TarikNeaj -
I think in C++11 it is allowed. At least my VC++2010 compiles it.
1
2
const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];

In this example, because MAX_ARRAY is a const, the value is known at compile time and C++ is perfectly fine with it.

What you are thinking of is this:
1
2
3
  int a;
  cin >> a;
  int arr[a];

This is not part of the standard, although some compilers do allow it as a non-standard extension.
I see, thanks for clearing that up @AbtractionAnon.
Topic archived. No new replies allowed.