Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a program that reads integers from file, stores it in vector and use count sort to sort the integers and write them in new file. My problem ocurs when i run Countsort function, the breakpoint happens at this for loop, all the numbers are counted correctly as i can see in the breakepoint

this is the loop that breakpoint occurs:

for (int i = 0; i < C.size(); i++) {
    C[A[i]] = C[A[i]] + 1;
}


my code looks like this:

C++
//COUNT SORT
void countsort() {

    int max = A[0];

    for (int i = 1; i < A.size(); i++) {
        if (A[i] > max) { 
            max = A[i];
        }
    }

    vector<int> C(max + 1);
    vector<int> B(A.size()); 

    for (int i = 0; i < C.size(); i++) {
        C[A[i]] = C[A[i]] + 1;
    }

    for (int i = 1; i < C.size(); i++) {
        C[i] = C[i] + C[i + 1];
    }

    for (int i = C.size() - 1; i >= 0; i--) {
        B[C[A[i]] - 1] = A[i];
        C[A[i]] = C[A[i]] - 1;
    }

    for (int i = 0; i < A.size(); i++) {
        A[i] = B[i];
    }
}


[Update]
ok im gonna post you full code here, comments are not in english thats why i deleted them:

C++
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>

using namespace std;

vector<int> A;
vector<int> C;
vector<int> B;

//MENU
void menu() {
	cout << endl;
	cout << "<opcija> - 0 = Beri iz vhoda" << endl;
	cout << "<opcija> - 1 = Counting sort" << endl;
	cout << "<opcija> - 2 = Roman sort" << endl;
	cout << "<opcija> - 3 = Izhod podatkov v txt" << endl;
	cout << "<opcija> - 4 = IZHOD" << endl;
	cout << endl;
}

//BRANJE VHODA
void beri() {

	ifstream file;
	file.open("primer_vhoda.txt"); //branje datoteke vhod.txt
	int st; //stevec stevil

	if (!file.is_open()) { //ce datoteke ne odpre
		cout << "Datoteke ni mogoce najti!" << endl; //izpise napako
	}

	while (file >> st) { //medtem ko bere stevila
		A.push_back(st); //jih shranjuje v vektor
	}
}

//COUNT SORT
void countsort() {

	int max = A[0]; //vrednosti inicializiramo na 0

	//poiscemo najvecje stevilo v A
	for (int i = 1; i < A.size(); i++) { //sprehodimo se po A
		if (A[i] > max) { 
			max = A[i];
		}
	}
	//cout << "NAJVECJE STEVILO JE: " << max << endl; SAMO ZA TEST
	vector<int> C(max + 1); //pomozno polje velikosti max + 1 --> max je najvecje stevilo v A
	vector<int> B(A.size()); //koncno polje B enake velikosti kot polje A

	for (int i = 0; i < C.size(); i++) {
		C[A[i]] = C[A[i]] + 1;
	}

	for (int i = 1; i < C.size(); i++) {
		C[i] = C[i] + C[i + 1];
	}

	for (int i = C.size() - 1; i >= 0; i--) {
		B[C[A[i]] - 1] = A[i];
		C[A[i]] = C[A[i]] - 1;
	}

	for (int i = 0; i < A.size(); i++) {
		A[i] = B[i];
	}


	/*for (int i = 0; i < C.size(); i++) { //vrednosti v C inicializiramo na 0
		C[i] = 0;
	}

	for (int i = 0; i < A.size(); i++) {
		C[A[i]] = C[A[i]] + 1;
	}

	for (int i = C.size() - 1; i > 0; i-- ) {
		C[i] = C[i] + C[i - 1];
	}

	for (int i = A.size() - 1; i >= 0; i--) {
		B[C[A[i]] - 1] = A[i];
		C[A[i]] = C[A[i]] - 1;
		cout << B[i] << " ";
	}*/
}

//IZPIS STEVIL V TXT
void izpisi() {
	int st; //stevec stevil
	ofstream file("izhod.txt"); //naredimo novo datoteko
	if (file.is_open()) { //ko je datoteka odprta
		for (int i = 0; i < B.size(); i++) { //preletimo vektor
			file << B[i] << " "; //zapisemo stevila v datoteko
		}
	}
}

//MAIN
int main()
{
	int izbira = 0;
	vector <int> A;
	vector <int> B;


	do {
		menu();
		cout << endl << "Izbira: ";
		cin >> izbira;
		switch (izbira) {
		case 0:
			beri();
			break;
		case 1:
			countsort();
			break;
		case 2:
			//izpisivector();
		case 3:
			izpisi();
			break;
		case 4:
			break;
		default:
			cout << "Napacna izbira!";
			break;
		}
	} while (izbira != 4);
	{
		system("PAUSE");
		return EXIT_SUCCESS;
	}
}

</vector></fstream></cstdlib></iostream>


What I have tried:

I searched, but couldnt find my answer.
Posted
Updated 12-Mar-16 13:18pm
v2

C++
for (int i = 0; i < C.size(); i++) {
    C[A[i]] = C[A[i]] + 1;
}

How do you know that A.size() is at least as big as C.size() ?
Because the code fails when if A.size() < C.size().

[update]
correction for this part
C++
for (int i = 0; i < A.size(); i++) {
    C[A[i]] = C[A[i]] + 1;
}
 
Share this answer
 
v2
Comments
vidusi 12-Mar-16 17:15pm    
hmm, i guess i dont know that, how can i get rid of that error in this case?
Patrice T 12-Mar-16 17:20pm    
A is define in code you didn't provide!
Can you explain what this code is doing ?
vidusi 12-Mar-16 18:22pm    
A is a global vector, my program is reading integers from a file and stores them in vector A, then i do countsort of those numbers.
Patrice T 12-Mar-16 18:51pm    
Need to see a complete piece of code to understand what you do.
Comments in code may help too
vidusi 12-Mar-16 19:02pm    
ok im gonna post you full code here, comments are not in english thats why i deleted them:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>

using namespace std;

vector<int> A;
vector<int> C;
vector<int> B;

//MENU
void menu() {
cout << endl;
cout << "<opcija> - 0 = Beri iz vhoda" << endl;
cout << "<opcija> - 1 = Counting sort" << endl;
cout << "<opcija> - 2 = Roman sort" << endl;
cout << "<opcija> - 3 = Izhod podatkov v txt" << endl;
cout << "<opcija> - 4 = IZHOD" << endl;
cout << endl;
}

//BRANJE VHODA
void beri() {

ifstream file;
file.open("primer_vhoda.txt"); //branje datoteke vhod.txt
int st; //stevec stevil

if (!file.is_open()) { //ce datoteke ne odpre
cout << "Datoteke ni mogoce najti!" << endl; //izpise napako
}

while (file >> st) { //medtem ko bere stevila
A.push_back(st); //jih shranjuje v vektor
}
}

//COUNT SORT
void countsort() {

int max = A[0]; //vrednosti inicializiramo na 0

//poiscemo najvecje stevilo v A
for (int i = 1; i < A.size(); i++) { //sprehodimo se po A
if (A[i] > max) {
max = A[i];
}
}
//cout << "NAJVECJE STEVILO JE: " << max << endl; SAMO ZA TEST
vector<int> C(max + 1); //pomozno polje velikosti max + 1 --> max je najvecje stevilo v A
vector<int> B(A.size()); //koncno polje B enake velikosti kot polje A

for (int i = 0; i < C.size(); i++) {
C[A[i]] = C[A[i]] + 1;
}

for (int i = 1; i < C.size(); i++) {
C[i] = C[i] + C[i + 1];
}

for (int i = C.size() - 1; i >= 0; i--) {
B[C[A[i]] - 1] = A[i];
C[A[i]] = C[A[i]] - 1;
}

for (int i = 0; i < A.size(); i++) {
A[i] = B[i];
}


/*for (int i = 0; i < C.size(); i++) { //vrednosti v C inicializiramo na 0
C[i] = 0;
}

for (int i = 0; i < A.size(); i++) {
C[A[i]] = C[A[i]] + 1;
}

for (int i = C.size() - 1; i > 0; i-- ) {
C[i] = C[i] + C[i - 1];
}

for (int i = A.size() - 1; i >= 0; i--) {
B[C[A[i]] - 1] = A[i];
C[A[i]] = C[A[i]] - 1;
cout << B[i] << " ";
}*/
}

//IZPIS STEVIL V TXT
void izpisi() {
int st; //stevec stevil
ofstream file("izhod.txt"); //naredimo novo datoteko
if (file.is_open()) { //ko je datoteka odprta
for (int i = 0; i < B.size(); i++) { //preletimo vektor
file << B[i] << " "; //zapisemo stevila v datoteko
}
}
}

//MAIN
int main()
{
int izbira = 0;
vector <int> A;
vector <int> B;


do {
menu();
cout << endl << "Izbira: ";
cin >> izbira;
switch (izbira) {
case 0:
beri();
break;
case 1:
countsort();
break;
case 2:
//izpisivector();
case 3:
izpisi();
break;
case 4:
break;
default:
cout << "Napacna izbira!";
break;
}
} while (izbira != 4);
{
system("PAUSE");
return EXIT_SUCCESS;
}
}
The problem is your vector definiton

C++
vector<int> C(max + 1);


consider you have 3 items in vector A
10,20,100

Here you define a max value
C++
int max = A[0];
 
for (int i = 1; i < A.size(); i++) {
    if (A[i] > max) 
    { 
        max = A[i];
    }
}


now size of vector is 3 , and the variable max contains 100
and your vector with 101 items (preallocated)
vector<int> C(max + 1);


and your code as following
C++
vector<int> C(max + 1);
vector<int> B(A.size()); 
 
for (int i = 0; i < C.size(); i++) 
{
    C[A[i]] = C[A[i]] + 1;
}


size of C is 101 whereas size of A 3 , which causing access violation

you vector definition should be like below
vector<int> C(A.size());
vector<int> B(A.size()); </int>
 
Share this answer
 
Comments
vidusi 12-Mar-16 19:26pm    
even tho i change the vectors like you mentioned i am getting the same error... at the same for loop. The thing is i need that max + 1 values for C array as i am doing counting sort.
Serkan Onat 12-Mar-16 19:49pm    
i have pointed out the general issue in your code , so you have scope errors
i am not going to fix your homework sorry

but consider following ,according to my example 10,20,100

i = 2; which points to value of 100 and size of C is 3 (even after you made the change i mentioned)

C[A[i]] = C[A[i]] + 1;

translates to

C[100] = C[100] + 1;

but there is no room at index 100 , currently C contains only 3 items and max index is 2 (zero based)

you can use temporary vectors to, fix your problems
vidusi 12-Mar-16 19:56pm    
i never considered that you guys will be fixing my errors, i just wanted an explanation why this error is happening as i havent found something similar.. I apreciate your help though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900