Help, my code keeps crashing with a code 3 on code blocks

I was trying to learn c++ and this piece of code keeps crashing again and again, any help is appreciated.
P.S. I am completely new to programming...
P.P.S I initially wrote this in Visual Studio, where it crashed too, with an access read violation


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include <vector>
#include <string>
#include "Source1.h"
using namespace std; 
int main(){
	Source1 s;
	cout << "Type of sort please:" << endl;
	char type;
	cin >> type;
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i<n; i++) {
		cin >> a[i];
	}
	switch (type) {
	case 'b' : s.bubbleSort(n, &a); break;
	case 'm': s.modBub(n, &a); break;
		}
	return 0;
}



Source1.cpp
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
#include <iostream>
#include <vector>
#include <string>
#include "Source1.h"

using namespace std;
void swap(int* a, int* b) {
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
void Source1::bubbleSort(int n, vector<int>* a) {

	for (int p = 0; p<n; p++) {
		for (int j = p+1; j<n; j++) {
			if (*(a+p)>*(a+j))
				swap(*(a + p), *(a + j));
		}

	}
	for (int k = 0; k<n; k++) {
		cout << *(&a+k) << " ";
	}
}
void Source1::modBub(int n, vector<int> *a) {
	for (int p = 0; p<n; p++) {
		int flag = 0;
		for (int j = p+1; j < n; j++) {
			if (a[p] > a[j]) {
				swap(*(a + p), *(a + j));
				flag += 1;
			}
		}
		if (flag == 0) break;
	}
	for (int k = 0; k<n; k++) {
		cout << *(&a + k) << " ";
	}
}




Source1.h

1
2
3
4
5
6
7
8
9
10
#pragma once
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class Source1 {
public: void bubbleSort(int n, vector<int>* a);
public: void modBub(int n, vector<int> *a);
};
Last edited on
Well, I'm not sure what the goal is here. There is a lot of use of pointers where references would have been better.

As it stands, the bubbleSort() function could be written like this, making generous use of pointers wherever possible. Note that to access the elements within the array, the vector member function data() can be used, it returns a pointer to the start of the data. I don't recommend this, I only show it in order to get the current code to function correctly
http://www.cplusplus.com/reference/vector/vector/data/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Source1::bubbleSort(int n, vector<int>* a) 
{    
    for (int p = 0; p<n; p++) 
    {
        for (int j = p+1; j<n; j++) 
        {
            if ( *((*a).data() +p)  >  *((*a).data() +j) ) 
                swap(  ((*a).data() + p), ((*a).data() + j)  );
        }
    }
    
    for (int k = 0; k<n; k++) 
    {
        cout << *((*a).data() + k) << " ";
//      cout << *(a->data() + k) << " ";   // alternative notation
    }
}



Now using references and not using pointers it might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void swapRef(int& a, int& b) 
{
    int temp = a;
    a        = b;
    b        = temp;
}

void Source1::bubbleSortRef(vector<int> & a) 
{    
    for (size_t p = 0; p<a.size(); p++) 
    {
        for (size_t j = p+1; j<a.size(); j++) 
        {
            if ( a[p]  >  a[j] ) 
                swapRef( a[p], a[j]  );
        }
    }
    
    for (size_t k = 0; k<a.size(); k++) 
    {
        cout << a[k] << " ";
    }
}

Topic archived. No new replies allowed.