Counting Sort infinite loop - c++

I am writing a counting sort function and when I run it, a window pops up saying "filename.exe has stopped working". After debugging it looks like it is getting stuck in the second for loop. What really confuses me, is if I set maxInt to any number greater than 130000 it works, but if its 130000 or lower than I get that error message. The file I'm using to sort only has about 20 numbers.
#include <iterator>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
std::string file = "";
std::vector<int> numbers;
void CountingSort(vector<int> &numbers);
int main()
{
std::cout << "Which file would you like to sort?\n";
std::cin >> file;
std::ifstream in(file.c_str());
// Read all the ints from in:
std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(),
std::back_inserter(numbers));
CountingSort(numbers);
// Print the vector with tab separators:
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\t"));
std::cout << std::endl;
return 0;
}
struct CalcMaxInt
{
int maxInt;
CalcMaxInt () : maxInt(0) {}
void operator () (int i) { if (i > maxInt) maxInt = i; }
};
void CountingSort(vector<int>& numbers)
{
CalcMaxInt cmi = std::for_each(numbers.begin(), numbers.end(), CalcMaxInt());
//int maxInt = cmi.maxInt + 1;
int maxInt = 130001;
vector <int> temp1(maxInt);
vector <int> temp2(maxInt);
for (int i = 0; i < numbers.size(); i++)
{
temp2[numbers[i]] = temp2[numbers[i]] + 1;
}
for (int i = 1; i <= maxInt; i++)
{
temp2[i] = temp2[i] + temp2[i - 1];
}
for (int i = numbers.size() - 1; i >= 0; i--)
{
temp1[temp2[numbers[i]] - 1] = numbers[i];
temp2[numbers[i]] = temp2[numbers[i]] -1;
}
for (int i =0;i<numbers.size();i++)
{
numbers[i]=temp1[i];
}
return;
}

You are trying to access an element out of proper range.
temp2 has range [0...maxInt-1] but the following code uses temp2[maxInt] which is out of range.
for (int i = 1; i <= maxInt; i++)
{
temp2[i] = temp2[i] + temp2[i - 1];
}
You'll have to fix temp2 to have maxInt+1 elements or i < maxInt to not to see the error.

Isn't the whole point of you doing this:
CalcMaxInt cmi = std::for_each(numbers.begin(), numbers.end(), CalcMaxInt());
To get the max element?
I'd change your code to the following.
void CountingSort(vector<int>& numbers)
{
CalcMaxInt cmi;
std::for_each(numbers.begin(), numbers.end(), cmi);
int maxInt = cmi.maxInt;
vector <int> temp1(maxInt);
vector <int> temp2(maxInt);
// then the rest the same starting with the for loops
// but with the fix that #kcm1700 mentioned to the for loop
}

Shouldn't temp1 be dimensioned numbers.size()+1?

Related

Find the missing numbers in the given array

Implement a function which takes an array of numbers from 1 to 10 and returns the numbers from 1 to 10 which are missing. examples input: [5,2,6] output: [1,3,4,7,8,9,10]
C++ program for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
// Initialize diff
int diff = arr[0] - 0;
for (int i = 0; i < N; i++) {
// Check if diff and arr[i]-i
// both are equal or not
if (arr[i] - i != diff) {
// Loop for consecutive
// missing elements
while (diff < arr[i] - i) {
cout << i + diff << " ";
diff++;
}
}
}
}
Driver Code
int main()
{
// Given array arr[]
int arr[] = { 5,2,6 };
int N = sizeof(arr) / sizeof(int);
// Function Call
printMissingElements(arr, N);
return 0;
}
How to solve this question for the given input?
First of all "plzz" is not an English world. Second, the question is already there, no need to keep writing in comments "if anyone knows try to help me".
Then learn standard headers: Why should I not #include <bits/stdc++.h>?
Then learn Why is "using namespace std;" considered bad practice?
Then read the text of the problem: "Implement a function which takes an array of numbers from 1 to 10 and returns the numbers from 1 to 10 which are missing. examples input: [5,2,6] output: [1,3,4,7,8,9,10]"
You need to "return the numbers from 1 to 10 which are missing."
I suggest that you really use C++ and get std::vector into your toolbox. Then you can leverage algorithms and std::find is ready for you.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
std::vector<int> missingElements(const std::vector<int> v)
{
std::vector<int> missing;
for (int i = 1; i <= 10; ++i) {
if (find(v.begin(), v.end(), i) == v.end()) {
missing.push_back(i);
}
}
return missing;
}
int main()
{
std::vector<int> arr = { 5, 2, 6 };
std::vector<int> m = missingElements(arr);
copy(m.begin(), m.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
return 0;
}
If you want to do something with lower computational complexity you can have an already filled vector and then mark for removal the elements found. Then it's a good chance to learn the erase–remove idiom:
std::vector<int> missingElements(const std::vector<int> v)
{
std::vector<int> m = { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (const auto& x: v) {
m[x] = -1;
}
m.erase(remove(m.begin(), m.end(), -1), m.end());
return m;
}
By this approach we are using space to reduce execution time. Here the time complexity is O(N) where N is the no of elements given in the array and space complexity is O(1) i.e 10' .
#include<iostream>
void printMissingElements(int arr[], int n){
// Using 1D dp to solve this
int dp[11] = {0};
for(int i = 0; i < n; i++){
dp[arr[i]] = 1;
}
// Traverse through dp list and check for
// non set indexes
for(int i = 1; i <= 10; i++){
if (dp[i] != 1) std::cout << i << " ";
}
}
int main() {
int arr[] = {5,2,6};
int n = sizeof(arr) / sizeof(int);
printMissingElements(arr, n);
}
void printMissingElements(int arr[], int n,int low, int high)
{
bool range[high - low + 1] = { false };
for (int i = 0; i < n; i++) {
if (low <= arr[i] && arr[i] <= high)
range[arr[i] - low] = true;
}
for (int x = 0; x <= high - low; x++) {
if (range[x] == false)
std:: cout << low + x << " ";
}
}
int main()
{
int arr[] = { 5,2,6,6,6,6,8,10 };
int n = sizeof(arr) / sizeof(arr[0]);
int low = 1, high = 10;
printMissingElements(arr, n, low, high);
return 0;
}
I think this will work:
vector<int> missingnumbers(vector<int> A, int N)
{ vector<int> v;
for(int i=1;i<=10;i++)
v.push_back(i);
sort(A.begin(),A.end());
int j=0;
while(j<v.size()) {
if(binary_search(A.begin(),A.end(),v[j]))
v.erase(v.begin()+j);
else
j++;
}
return v;
}

Set of Pairs program in C++

I am making a set of pairs of Max and Min elements of Every Subset in an Array.But its giving me these errors. And at last I need Size of set.
(Edited with some suggestions)
In function 'int main()':
27:12: error: 'max_element' was not declared in this scope
27:12: note: suggested alternative: 'max_align_t'
28:12: error: 'min_element' was not declared in this scopeIn function 'int main()':
Code:
#include <iostream>
#include <set>
#include <vector>
#include <utility>
typedef std::pair<int,int> pairs;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, max, min;
set<pairs> s;
cin >> n;
int a[n];
for(int i=0;i<n;i++) {
cin >> a[i];
}
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
vector<int> v;
v.push_back(a[j]);
if(v.size() > 1) {
max = *max_element(v.begin(),v.end());
min = *min_element(v.begin(),v.end());
pairs p1 = make_pair(max, min);
s.insert(p1);
max = 0;
min = 0;
}
}
}
cout << s.size() << endl;
}
typedef pair<int,int> pairs;
should be
typedef std::pair<int,int> pairs;
(Or you could move using namespace std; so that it is before your typedef).
Plus typedefing a single pair as the plural pairs is a really really bad idea, that is going to confuse you and anyone else reading your code for the rest of this programs existence. If you want a typedef for a pair of ints, then call it that
typedef std::pair<int,int> pair_of_ints;
To make your last programme works, it was needed to move the declaration of std::vector<int> v;
Moreover, your code has a complexity O(n^3). In practice, it is possible to get a complexity O(n^2), by calculating
iteratively the max and min values.
This code compares your code and the new one. The results are identical. However, I cannot be sure
that your original code does what you intended to do.
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
typedef std::pair<int,int> pairs;
//using namespace std;
void print (const std::set<pairs> &s) {
for (auto& p: s) {
std::cout << "(" << p.first << ", " << p.second << ") ";
}
std::cout << "\n";
}
int count_pairs_op (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
std::vector<int> v;
for(int j = i; j < n; j++) {
v.push_back(a[j]);
if(v.size() > 1) {
max = *std::max_element(v.begin(), v.end());
min = *std::min_element(v.begin(), v.end());
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
}
print (s);
return s.size();
}
int count_pairs_new (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
min = a[i];
max = a[i];
for(int j = i+1; j < n; j++) {
max = std::max (max, a[j]);
min = std::min (min, a[j]);
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
print (s);
return s.size();
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_op(a) << std::endl;
std::cout << count_pairs_new(a) << std::endl;
}
It appears that there was a mistake in the understanding of the problem.
For each subarray, we have to consider the maximum and the second maximum.
Moreover, we know that all elements are distinct.
As the size can be up to 10^5, we have to look for a complexity smaller than O(n^2).
In practice, each element can be the second element of two subarrays,
if there exist a greater element before and after it.
We just have to check it.
This can be perfomed by calculating, for each index i, the maximum value before and after it.
Total complexity: O(n)
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
int count_pairs_2nd_max (const std::vector<int>& a) {
int n = a.size();
int count = 0;
std::vector<int> max_up(n), max_down(n);
max_up[0] = -1;
for (int i = 1; i < n; ++i) {
max_up[i] = std::max(max_up[i-1], a[i-1]);
}
max_down[n-1] = -1;
for (int i = n-2; i >= 0; --i) {
max_down[i] = std::max(max_down[i+1], a[i+1]);
}
for(int i = 0; i < n; ++i) {
if (max_up[i] > a[i]) count++;
if (max_down[i] > a[i]) count++;
}
return count;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_2nd_max(a) << std::endl;
}

How to print to the console after every swap using any sorting algorithm?

In my Intro to Computer Science class I am beginning to learn the basics of sorting algorithms. So far, we have gone over Bubble, Selection, and Insertion Sort.
After class today, the instructor has requested us to "enhance" the program by adding code to print out the vector/array after every swap during the sorting. I am at a complete loss as to how I would make this happen. I'm thinking something like :
if (swapped) { cout << vec << " "; }
but without even trying, I'm certain this wouldn't work. Any help is very much appreciated. Here's my code so far:
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> createVec(int n) {
unsigned seed = time(0);
srand(seed);
vector<int> vec;
for (int i = 1; i <= n; ++i) {
vec.push_back(rand() % 100 + 1);
}
return vec;
}
void showVec(vector<int> vec) {
for (int n : vec) {
cout << n << " ";
}
}
void bubbleSort(vector<int> &vec) {
int n = vec.size();
bool swapped = true;
while (swapped) {
swapped = false;
for (int i = 1; i <= n-1; ++i) {
if (vec[i-1] > vec[i]) {
swap(vec[i-1], vec[i]);
swapped = true;
}
}
}
}
void selectionSort(vector<int> &vec) {
int n = vec.size();
int maxIndex;
for (int i = 0; i <= n-2; ++i) {
maxIndex = i;
for (int j = i+1; j <= n-1; ++j) {
if (vec[j] < vec[maxIndex]) {
maxIndex = j;
}
}
swap(vec[i], vec[maxIndex]);
}
}
int main()
{
vector<int> numbers = createVec(20);
showVec(numbers);
cout << endl;
//bubbleSort(numbers);
selectionSort(numbers);
showVec(numbers);
return 0;
}
For example in the called function selectionSort substitute this statement
swap(vec[i], vec[maxIndex]);
for the following statement
if ( i != maxIndex )
{
swap(vec[i], vec[maxIndex]);
showVec( vec );
cout << endl;
}
Also the function showVec should declare the parameter as having a constant referenced type
void showVec( const vector<int> &vec) {
for (int n : vec) {
cout << n << " ";
}
}

Generate different random numbers

I want to generate different random numbers . I used srand and rand , but in my output some numbers are identical .
This is my output :
How to do with srand to generate different numbers ?
#include<iostream>
#include<time.h>
#include <windows.h>
int main(){
time_t t;
std::vector<int> myVector;
srand((unsigned)time(NULL));
for (int i = 0; i < 40; i++){
int b = rand() % 100;
myVector.push_back(b);
std::cout << myVector[i] << std::endl;
}
Sleep(50000);
}
One easy way is to add all numbers from 0-99 to a vector and shuffle it, then you can get as many (up to 100) non repeating random numbers as you require.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
int main(void) {
std::vector<int> numbers;
for(int i=0; i<100; i++) // add 0-99 to the vector
numbers.push_back(i);
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(numbers.begin(), numbers.end(), std::default_random_engine(seed));
for(int i=0; i<40; i++) // print the first 40 randomly sorted numbers
std::cout << numbers[i] << std::endl;
}
You could use a set:
std::set<int> numbers;
while (numbers.size() < 40)
{
numbers.add(rand() % 100);
}
and then copy it into a vector if necessary.
srand number generator can give identical numbers.
You could implement a solution which deletes duplicates not adding them to the vector. For example:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> myVector;
srand((unsigned)time(NULL));
while(myVector.size() < 40)
{
int b = rand() % 100;
if ( !(std::find(myVector.begin(), myVector.end(), b) != myVector.end()))
{
myVector.push_back(b);
std::cout << myVector.at(myVector.size()-1) << std::endl;
}
}
Sleep(50000);
return 0;
}
An easy way of getting rid of duplicates is using std::unique in <algorithm>.
Here is an example of that in use:
#include <vector>
#include <iostream>
#include <algorithm>
#include <random>
int ran(int min, int max)
{
std::random_device r;
std::mt19937 gen(r());
std::uniform_int_distribution<> dis(min, max);
return dis(gen);
}
int main()
{
const int fill_size = 10;
const int min = 1; // min random number
const int max = 100; // max random number
std::vector<int> vec;
while (vec.size() != fill_size) {
vec.emplace_back(ran(min, max)); // create new random number
std::sort(begin(vec), end(vec)); // sort before call to unique
auto last = std::unique(begin(vec), end(vec));
vec.erase(last, end(vec)); // erase duplicates
}
std::random_shuffle(begin(vec), end(vec)); // mix up the sequence
for (const auto& i : vec) // and display elements
std::cout << i << " ";
}
As for me, the idea of using set is not so good, because the generating time of every new value increases. If you have enough memory it seems that usage of an array can be preferable.
In the next code, I don't use shuffle, instead, I use a random function just size times to choose one value. I add it to the destination vector, then in the source array, swap the value with the last element and decrease arr_size.
/*
* Return random unsigned int value using intrinsic
* */
unsigned getRandom() {
unsigned val;
_rdrand32_step(&val);
return val;
}
/*
* Return a vector<int> of uniq numbers in a range of [min ... max).
*
* #param min - min value.
* #param max - max value.
* #param size - amount of uniq numbers (size <= max-min).
* */
vector<int> getUniqNumbers(int min, int max, unsigned size) {
int arr_size = max - min;
int *a = new int[arr_size];
for (int i = 0; i < arr_size; i++) {
a[i] = min + i;
}
vector<int> numbers(size);
for (int i = 0; i < size; i++) {
unsigned u_rand = getRandom() % arr_size;
numbers[i] = a[u_rand];
a[u_rand] = a[--arr_size];
}
delete[] a;
return numbers;
}
You can easily achieve a unique set of random numbers writing:
#include<iostream>
#include<vector>
int main(){
std::vector<int> myVector;
srand((unsigned)time(NULL));
for (int i = 0; i < 40; i++) {
int b = rand() % 100;
if(!std::find(std::begin(myvector),std::end(myvector),b)) {
myVector.push_back(b);
std::cout << myVector[i] << std::endl;
}
}
}
This is a statistical (mathematical) issue. Random numbers may be identical to eachother. If you need unique numbers, you must check to see if they are used before. For example like this:
for (int i = 0; i < 40; i++){
int b = rand() % 100;
for (int j = 0; j < i; j++){
if(myVector[j]==b)i--;
else{
myVector.push_back(b);
std::cout << myVector[i] << std::endl;
}
}
}

C++ Assertion Failed on vector at runtime Expression: vector subscript out of range

im gettin this really annoying error message. I know Im only new to this but it seems the type of thing I could figure out. Can anyone show me where im going wrong please?
The message at run time is:
Debug Assertion Failed!
Program:
....
File: c:\program files\microsoft visual studio 10.0\vc\include\vector
Line: 932
Expression: Vector subscript out of range
and the code is
#include "VectorIntStorage.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void VectorIntStorage::Read(istream& r)
{
char c[13];
r >> c;
r >> NumberOfInts; //gets number of ints for vector
//numberVector = new std::vector<int> numberVector;
for(int i = 0; i < NumberOfInts; i++)
{
r >> numberVector[i];
cout << numberVector[i] << endl;
if(_sortRead) //true
{
for(int k = 0; k < i; k++)
{
if(numberVector[i] < numberVector[k])
{
int temp = numberVector[k];
numberVector[k] = numberVector[i];
numberVector[i] = temp;
}
}
}
}
}
void VectorIntStorage::Write(ostream& w)
{
for(int i = 0; i < NumberOfInts; i++)
{
w << numberVector[i] << endl;
cout << numberVector[i] << endl;
}
}
void VectorIntStorage::sortStd()
{
sort(numberVector.begin(), numberVector.end());
}
void VectorIntStorage::sortOwn()
{
quickSort(0, NumberOfInts - 1);
}
void VectorIntStorage::setReadSort(bool sort)
{
_sortRead = sort;
}
void VectorIntStorage::quickSort(int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = numberVector[(left + right) / 2];
while (i <= j)
{
while (numberVector[i] < pivot)
i++;
while (numberVector[j] > pivot)
j--;
if (i <= j)
{
tmp = numberVector[i];
numberVector[i] = numberVector[j];
numberVector[j] = tmp;
i++;
j--;
}
}
if (left < j)
{
quickSort(left, j);
}
if (i < right)
{
quickSort(i, right);
}
}
VectorIntStorage::VectorIntStorage(const VectorIntStorage& copying)
{
//int *duplicate = new int[(copying.NumberOfInts)];
//vector<int> *duplicate = new vector<int>;
//std::copy(numberVector.begin(), numberVector.end(), duplicate);
//numberVector = duplicate;
//NumberOfInts = copying.NumberOfInts;
}
VectorIntStorage::VectorIntStorage(void)
{
}
VectorIntStorage::~VectorIntStorage(void)
{
}
We don't have enough information to say for sure, but I suspect the failing line is r >> numberVector[i]. I suppose you meant to say int j; r >> j; numberVector.push_back(j);
The problem is precisely what the error message says: your vector subscript (i) is out of range. Specifically, you never increase the size of your vector, so it is always of size 0. Thus, any use of operator[] is going to reference an out-of-range element.
You can't just use numberVector[i] without calling numberVector.resize() first.
vector<int> vec;
vec[1] = 0; // fails - vec is empty so [1] is out of range
vec.resize(100);
vec[1] = 5; // ok, you can access vec[0] .. vec[99] now
vec.push_back(11); // Now the size is 101 elements, you can access vec[0] .. vec[100]
r >> NumberOfInts; //gets number of ints for vector
From the above comment, it seems you need a vector of size NumberOfInts. But leaving the line as commented -
//numberVector = new std::vector<int> numberVector;
You are declaring the vector as -
std::vector<int> numberVector; // The size of the vector is 0
To perform the operation of [] on numberVector, it's size should be mentioned and should be in the valid range while declaration. Since it not mentioned while declaration, you need to do a push_back operation to dynamically increase the size of the vector.
for(int i = 0; i < NumberOfInts; i++)
{
r >> numberVector[i]; // Size isnot initially mentioned while declaration
// of the vector to do an `[]` operation
cout << numberVector[i] << endl;
// ....