arranging array in ascending order - c++

I'm trying to arrange a random array. In the code it's just the first step of swapping places by size.
When running the code a get Debug Error and the output after the swap shows that the first number in the array was deleted and the last is a long random number that was in the memory.
It looks like it starts the swapping from i=1, why?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void buildArray(int arr[], int size) {
srand ( (unsigned int)time(NULL) );
for(int i = 0; i < size; ++i)
arr[i] = rand() % 50 + 0;
}
void dispArray(int arr[], int size) {
for(int i = 0; i < size; ++i)
cout << i << ": " << arr[i] << endl;
}
int main()
{
const int size = 5;
int arr[size];
buildArray(arr, size);
dispArray(arr, size);
int swapHolder = -1;
for(int i = 0; i < size; ++i) {
if(arr[i] > arr[i+1]) {
swapHolder = arr[i+1];
arr[i+1] = arr[i];
arr[i] = swapHolder;
cout << endl;
}
}
dispArray(arr, size);
return 0;
}
Output example:
0: 46
1: 15
2: 47
3: 5
4: 19
0: 15
1: 46
2: 5
3: 19
4: -858993460

Here is the problem: if(arr[i] > arr[i+1]), your for loop goes from [0, size - 1], so on the last iteration, lets say size = 5, you would be testing if(arr[5] > arr[6]), accessing the uninitialized arr[6], the right way is to make your for loop go from [0, size - 2]:
for(int i = 0; i < size - 1; ++i) ...

for(int i = 0; i < size; ++i) {
if(arr[i] > arr[i+1]) {
swapHolder = arr[i+1];
arr[i+1] = arr[i];
The last iteration is for i = size -1. Then
if(arr[i] > arr[i+1])
means accessing array out of range. This is undefined behavior.

Related

find frequency in array using vector

How can I change my code to get a count for every element?
With my code everything is okay. And it works, but how can I change only that part?
#include <iostream>
#include <vector>
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
std::vector<bool> visited(n, false);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (visited[i] == true)
continue;
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
std::cout<<count<<" ";
}
}
int main()
{
int n;
std::cin>>n;
int arr[n];
for(int i = 0; i < n; i++){
std::cin>>arr[i];
}
countFreq(arr, n);
return 0;
}
And about the result`
input 10
1 1 2 2 3 3 4 4 5 5
output 2 2 2 2 2
but I want to get
output 2 2 2 2 2 2 2 2 2 2
(for every element)
Your function contains extra code that ends up confusing you. The visited variable is essentially unnecessary. Start the count at 0 and make no special case for the "current" cell and you'll find that some very simple code will do what you need:
void countFreq(int arr[], int n)
{
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Count frequency
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
std::cout << count << " ";
}
}
You need to save the result to an array for each number. Then when you find any processed number then print counter from the saved array.
#include <iostream>
#include <vector>
#include <unordered_map>
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
std::vector<bool> visited(n, false);
std::unordered_map<int, int> counter;
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++)
{
// Skip this element if already processed
if (visited[i] == true)
{
std::cout << counter[arr[i]] << " ";
continue;
}
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
visited[j] = true;
count++;
}
}
counter[arr[i]] = count;
std::cout<<count<<" ";
}
}
int main()
{
int n;
std::cin>>n;
int arr[n];
for(int i = 0; i < n; i++)
{
std::cin>>arr[i];
}
countFreq(arr, n);
return 0;
}
The issue is that you discard the values already visited.
One possibility is instead to memorize the count when the value is visited the first time,
and to memorize the index value of the first value appearance, when a value is visited the 2nd, 3rd ... time.
#include <iostream>
#include <vector>
void countFreq(const std::vector<int>& arr) {
int n = arr.size();
// Mark all array elements as not visited
std::vector<int> mem_count(n, n);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (mem_count[i] != n) {
std::cout << mem_count[mem_count[i]] << " ";
continue;
}
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
mem_count[j] = i;
count++;
}
}
mem_count[i] = count;
std::cout << count << " ";
}
}
int main() {
int n;
std::cin>>n;
std::vector<int> arr(n);
for(int i = 0; i < n; i++){
std::cin >> arr[i];
}
countFreq(arr);
return 0;
}
You can find the frequencies of numbers this way if you know the what is your maximum element in the input array. lets say m is maximum number in your array.
so you have to create a new array of size m. you can simply co-relate them as m buckets. from 0 to m. And each bucket will hold the count of each element in the input array. The index of each bucket will refer to element in the input array. This has time complexity O(1) if we know what is the max element the array.
You can do this way:
std::vector<int> frequencey(std::vector<int>& nums){
auto max = *(std::max_element(nums.begin(), nums.end()));
std::vector<int> frequencies(max + 1, 0);
for(int i = 0; i < nums.size(); ++i){
frequencies[nums[i]] +=1;
}
return frequencies;
}
This is very simple
#include <vector>
#include <map>
#include <iostream>
void main()
{
std::vector<int> v { 1,1,2,2,3,3,4,4,5,5 }; // Your input vector
// Count "frequencies"
std::map<int, int> m;
for (auto i : v)
m[i]++;
// Print output
for (auto i : v)
std::cout << m[i] << " ";
}

Applying selection sort on an array of integers

int arr[] = {7,4,10,8,3,1};
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i<size-1; i++){
int temp = arr[i];
for(int j = i+1; j < size; j++){
if(arr[j] < temp){
temp = arr[j];
}
}
swap(temp, arr[i]);
}
I am trying to apply the selection sort algorithm on the given array, but the output I am getting is only [1,1,1,1,1,1], I am finding the minimum element through the inner loop, Ican't figure out what is going wrong?
Slightly modified your code;
You need to pass reference(address) to both elements to take place of swapping contents
int arr[] = { 7, 1, 10, 8, 3, 11, 0, 12, 5, 8 };
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < size; i++)
{
auto temp = std::min_element( arr + i, arr + size );
std::swap( arr[i], *temp );
}
You have to add algorithm header to use std::min_element
int arr[] = {7,4,10,8,3,1};
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i<size-1; i++){
int temp = arr[i];
int pos = i;
for(int j = i+1; j < size; j++){
if(arr[j] < temp){
temp = arr[j];
pos = j;
}
}
if(pos != i)
std::swap(arr[pos], arr[i]);
}
This should work.
It is suggested not to use using namespace std;. There is a plethora of reasons why you should not; that I will not mention.
By the way I tried to keep some of your variables the same but to be honest, I didn't. It is better to create variable names that explain what the code is doing. It makes your code a lot more legible and readable.
So opt out of one letter variables. It is fine in for loops, however this is a special case.
Now, here is another alternative suggested by #user4581301 & #Swift -Friday Pie. This method is using std::size using c++17.
For example:
#include <iostream>
#include <utility> // to use the swap() function.
#include <iterator> // to use std::size() function.
int main()
{
int arr[] = { 7,4,10,8,3,1 };
// This --> int size = sizeof(arr) / sizeof(arr[0]); is archaic.
const int length = static_cast<int>(std::size(arr)); // Call this something other than "size"; you can run into issues.
// We use static_cast<int> as a implicit conversion, and the obvious std::size(arr)).
// Going through the elements
for (int StartOfIndex = 0; StartOfIndex < length - 1; ++StartOfIndex)
{
// smallest is the index of the smallest element we’ve encountered this iteration
int smallest = StartOfIndex;
// Looking for a smaller element..
for (int current = StartOfIndex + 1; current < length; ++current)
{
// if we found an element smaller than our last; take note.
if (arr[current] < arr[smallest])
smallest = current;
}
// swap StartOfIndex with smallest.
std::swap(arr[StartOfIndex], arr[smallest]);
}
//Prints array.
for (int index = 0; index < length; ++index)
std::cout << arr[index] << " ";
std::cout << "\n";
return 0;
}
Output: 1 3 4 7 8 10
The first mistake you made in writing for loop's condition, don't use swap(temp, array[i]); yet try to get the basics first.
#include <iostream>
using namespace std;
int findsmall(int arr[], int i, int size){
int s, pos, j;
s = arr[i];
pos = i;
for(j = i+1; j < size; j++){
if(arr[j] < s){
s = arr[j];
pos = j;
}
}
return pos;
}
int main() {
int arr[] = {7,4,10,8,3,1};
int size = sizeof(arr) / sizeof(arr[0]);
int smallnum;
int temp;
int count = 0;
cout << "Original array: ";
for(int i = 0; i < size; i++){
if(i < size - 1){
cout << arr[i] << ", ";}
else{
cout << arr[i];
}
}
cout << endl;
for(int i = 0; i < size; i++){
smallnum = findsmall(arr,i, size);
temp = arr[i];
arr[i] = arr[smallnum];
arr[smallnum] = temp;
count++;
}
cout << "Sorted array: ";
for(int i = 0; i < size; i++){
if(i < size - 1){
cout << arr[i] << ", ";}
else{
cout << arr[i];
}
}
cout << endl;
return 0;
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
selectionSort(arr,size);
This should work.

Generating all R-digit numbers among N digits in C++ (combinations, iterative)?

I have a program, where I have to generate all R-digit numbers among N digits in C++, for example for N=3 (all digits from 1 to N inclusive) and R=2 the program should generate 12 13 21 23 31 32. I tried to do this with arrays as follows, but it does not seem to work correctly.
#define nmax 20
#include <iostream>
using namespace std;
int n, r;
void print(int[]);
int main()
{
cin >> n;
cin >> r;
int a[nmax];
int b[nmax];
int used[nmax];
for (int p = 1; p <= n; p++) {
//Filling the a[] array with numbers from 1 to n
a[p] = n;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++) {
b[j] = a[i];
used[j] = 1;
if (used[j]) {
b[j] = a[i + 1];
}
used[j] = 0;
}
print(b);
}
return 0;
}
void print(int k[]) {
for (int i = 0; i < r; i++) {
cout << k[i];
}
}
If I understand your question correctly, you can explore this website where it explains the problem and suggests the solution thoroughly.
Here is a slightly altered code:
Pay attention that time is an issue for bigger N values.
#define N 5 // number of elements to permute. Let N > 2
#include <iostream>
using namespace std;
// NOTICE: Original Copyright 1991-2010, Phillip Paul Fuchs
void PrintPerm(unsigned int *a, unsigned int j, unsigned int i){
for(unsigned int x = 0; x < N; x++)
cout << " " << a[x];
cout << " swapped( " << j << " , " << i << " )\n";
}
void QuickPerm(void){
unsigned int a[N], p[N+1];
register unsigned int i, j, PermCounter = 1; // Upper Index i; Lower Index j
for(i = 0; i < N; i++){ // initialize arrays; a[N] can be any type
a[i] = i + 1; // a[i] value is not revealed and can be arbitrary
p[i] = i;
}
p[N] = N; // p[N] > 0 controls iteration and the index boundary for i
PrintPerm(a, 0, 0); // remove comment to PrintPerm array a[]
i = 1; // setup first swap points to be 1 and 0 respectively (i & j)
while(i < N){
p[i]--; // decrease index "weight" for i by one
j = i % 2 * p[i]; // IF i is odd then j = p[i] otherwise j = 0
swap(a[i], a[j]); // swap(a[j], a[i])
PrintPerm(a, j, i); // remove comment to PrintPerm target array a[]
PermCounter++;
i = 1; // reset index i to 1 (assumed)
while (!p[i]) { // while (p[i] == 0)
p[i] = i; // reset p[i] zero value
i++; // set new index value for i (increase by one)
} // while(!p[i])
} // while(i < N)
cout << "\n\n ---> " << PermCounter << " permutations. \n\n\n";
} // QuickPerm()
int main(){
QuickPerm();
} //main
Here is a list of the modified items from the original code.
N defined to be 5 instead of 12.
A Counter has been added for more informative result.
The original swap instructions reduced by using c++ standard libraries' swap() function.
The getch() has been removed.
The 'Display()' function has been renamed to be 'PrintPerm()'.
The printf() function has been replaced by cout.
Printing number of permutation has been added.

My C++ code goes into infinite loop when array size increases

I am trying to solve the problem at: Cut the Sticks.
My code works fine when the array size <=3 but goes bonkers then the size increases to >=6
#include<iostream>
using namespace std;
int minElement(int a[]){
int min = a[0];
int k;
for (k=1; k < 6; k++){
if (a[k] < min and a[k] > 0)
min = a[k];
}
return min;
}
int main(){
int a[6];
for (int i=0; i<6; i++){
cin >> a[i];
}
int minElem, flag;
while (true){
flag = 0;
minElem = minElement(a);
for (int i = 0; i < 6; i++){
a[i] = a[i] - minElem;
}
for (int j = 0; j < 6; j++){
if (a[j] > 0){
cout<<a[j]<<endl;
flag++;
}
}
if (flag == 0)
break;
};
return 0;
}
The problem is arising in the minElement function, it seems. On print min after assignment, it shows min as empty. Similarly, inside the loop in the function, I am getting blanks for all mins. What could be the issue?
EDIT
Please try the code here: http://cpp.sh/2ti6 with the input as [5,4,4,2,2,8]. It doesn't reach zero, the code starts failing before the first iteration, when the minimum element is called as Nothing is returned from the minElement functionn
Problem is there because of this line in the code
int min = a[0];
you need to check that array element is non-zero
int minElement(int a[]){
int min = 0;
int i=0;
while(i<6) // check the array element is non -zero
{
if(a[i]>0){
min = a[i];
break;
}
i++;
}
//int min = a[0];
int k;
for (k=1; k < 6; k++){
if (a[k] < min && a[k] > 0)
min = a[k];
}
return min;
}
The problem lies in your minElement function.
In the case that a[0] happens to be zero, you'll never assign another value to min and will return zero. Out of the function you will subtract zero to the values and enter the infinite loop.
In the case that a[0] is a negative number, you'll end up subtracting a negative value (and so increase the values instead). Then a[0] will become zero and your program loops.
Initializing min with a[0] is not enough, you have to look for a valid minimum value. You could, for example, initialize it with a very large value (not a very good solution) or search the array for the first valid min value.
EDIT:
I added some prints to your code:
...
while (true){
flag = 0;
minElem = minElement(a);
std::cout << "Min: " << minElem << std::endl;
for (int i = 0; i < 6; i++){
a[i] = a[i] - minElem;
}
std::cout << "Values:";
for (int j = 0; j < 6; j++){
std::cout << " " << a[j];
if (a[j] > 0){
//cout << " " << a[j];
flag++;
}
}
std::cout << std::endl;
if (flag == 0)
break;
getchar();
}
...
Here's the output:
5 4 4 2 2 8
Min: 2
Values: 3 2 2 0 0 6
Min: 2
Values: 1 0 0 -2 -2 4
Min: 1
Values: 0 -1 -1 -3 -3 3
Min: 0
Values: 0 -1 -1 -3 -3 3
(...) Loop forever
I hope that helps you understand now what the problem is.
int minElement(int a[]); change this to int minElement(int a[], int size)
2.
int a[6];
for (int i=0; i<6; i++){
cin >> a[i];
}
change this to
int arr_size=0;
cout << "Enter the array size ";
cin >> arr_size;
int a[arr_size];
for (int i=0; i<arr_size; i++){
cin >> a[i];
}
for (int i = 0; i < 6; i++){
a[i] = a[i] - minElem;
}
You are subtracting the min of the array from each element, so at this point there is only one 0 in the array and rest are positive numbers.
for (int j = 0; j < 6; j++){
if (a[j] > 0){
cout<<a[j]<<endl;
flag++;
}
So at this point flag has to be some positive number, and in this case 5.
if (flag == 0)
break;
This condition will never hit and hence the infinite loop.
it has nothing to do with the dimension.
What happens is that if a[0] is the smallest value => it will run in an infinte loop. Why because a[0] eventually becomes 0.
Why does a[0]=0 run in an infinte loop?
What value does minElement return when a[0] is 0?
try replace int min = a[0]; qith min = (a[0]>0) ? a[0]: 1;
or adding `minElem= (minElem>0) ? minElem: 1;' after 'minElem = minElement(a);'
PS:
Looking at all the answers this seems the simplest minElement that does what you want:
int minElement(int a[]){
int min = a[0];
int k;
for (k=1; k < 6; k++){
if(min==0 && a[k]>0 )
min=a[k];
else
if (a[k] < min and a[k] > 0)
min = a[k];
}
return min;
}
the min will only be zero at iteration N if a[j]==0 for all j+1
I solve this base on the link you provide. I use vector for me to easy remove those minimum length stick.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int noOfInput;
int input;
vector<int> myVec;
cin>>noOfInput;
for (int i=0; i<noOfInput; i++){
cin >> input;
myVec.push_back(input);
}
sort(myVec.begin(),myVec.end(),greater<int>());
while(myVec.size()>0){
int minimum = myVec[myVec.size()-1];
int lengthOfVector = myVec.size();
cout<<myVec.size()<<" ";
for(int i = lengthOfVector-1 ; i >=0 ; i--){
myVec[i]=myVec[i]-minimum;
if(myVec[i]==0){
myVec.pop_back();
}
}
}
return 0;
}

Resizing dynamic array in c++

I have some code that is producing unexpected results. Here is the code:
#include <iostream>
using namespace std;
int **nums;
int size;
void A(int** arr)
{
int **resize;
resize = new int*[size*2];
for(int i = 0; i < size; i++)
resize[i] = new int(*arr[i]);
cout << endl;
arr = resize;
size *= 2;
delete[] resize;
}
int main()
{
size = 10;
nums = new int*[size];
for(int i = 0; i < size; i++)
nums[i] = new int(i);
for(int i = 0; i < size; i++)
cout << *nums[i] << endl;
A(nums);
cout << endl;
for(int i = (size / 2); i < size; i++)
nums[i] = new int(i);
for(int i = 0; i < size; i++)
cout << *nums[i] << endl;
}
The function A(int** arr) works fine as far as I can tell and actually resizes the array. However, in the last for loop in main(), when the array is printing, the first two elements of the array are not 0 and 1 like it is supposed to be. Here is the result I am getting:
0
1
2
3
4
5
6
7
8
9
16331248
16331712
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Those first two ints after the space are different each time the program is executed. After some debugging I found out that the first two elements print correctly until the iterator i=13 in the second to last for loop in main(). Then the first two elements in the array take on some large numbers. I am not sure why this is happening and I have been working on this for a couple of hours now :( Any help is appreciated.
A() is not modifying nums to point at the new array. Even if it were, it is deleting the new array, so nums would end up pointing at invalid memory. You need to declare the arr parameter as a reference, and delete the old array instead of the new array:
void A(int** &arr)
{
int **resize;
resize = new int*[size*2];
for(int i = 0; i < size; i++)
resize[i] = new int(*arr[i]);
cout << endl;
delete[] arr;
arr = resize;
size *= 2;
}
For what you are attempting, I think you have too much indirection. Try removing a level:
#include <iostream>
using namespace std;
int *nums;
int size;
void A(int* &arr)
{
int *resize;
resize = new int[size*2];
for(int i = 0; i < size; i++)
resize[i] = arr[i];
cout << endl;
delete[] arr;
arr = resize;
size *= 2;
}
int main()
{
size = 10;
nums = new int[size];
for(int i = 0; i < size; i++)
nums[i] = i;
for(int i = 0; i < size; i++)
cout << nums[i] << endl;
A(nums);
cout << endl;
for(int i = (size / 2); i < size; i++)
nums[i] = i;
for(int i = 0; i < size; i++)
cout << nums[i] << endl;
delete[] nums;
}
Since you are using C++, you should be using a std::vector instead of a raw array, then you can eliminate A() altogether:
#include <iostream>
#include <vector>
using namespace std;
vector<int> nums;
int main()
{
nums.resize(10);
for(int i = 0; i < nums.size(); i++)
nums[i] = i;
for(int i = 0; i < nums.size(); i++)
cout << nums[i] << endl;
nums.resize(nums.size()*2);
cout << endl << endl;
for(int i = (nums.size() / 2); i < nums.size(); i++)
nums[i] = i;
for(int i = 0; i < nums.size(); i++)
cout << nums[i] << endl;
}
First of all, your function, A, does not resize anything. It prints a newline character to standard output, it multiplies the global size variable by 2, and then it leaks some memory. That's it.
Now, because it multiplies size by 2 (going from 10, to 20), you run into a problem, here:
for(int i = (size / 2); i < size; i++)
nums[i] = new int(i);
Here, you are trying to access elements 10 through 19 of the array which nums points to. But the array which nums points to only has 10 elements (numbered 0 through 9), so your code has undefined behavior.