i am doing a shell sort program but there's some problem passing the array into my function, i have searched for some post about passing array into the function but i still don't understand.
#include <iostream>
#include <cmath>
using namespace std;
int shellsort(int arr[]){
int gap = floor(sizeof(arr)/2);
for(int gap = floor(sizeof(arr)/2); gap < 0; gap=gap/2){
for(int i = gap; i < sizeof(arr); i++){
if(arr[i] < arr[i-gap]){
int temp = arr[i];
arr[i] = arr[i-gap];
arr[i-gap] = temp;
if(gap == 1){
if(arr[i-1] < arr[i-2]){
int temp = arr[i-1];
arr[i-1] = arr[i-2];
arr[i-2] = temp;
}
}
}
}
}
return arr;
}
int main(){
int numcount;
cin>>numcount;
int numbers[numcount];
for(int i; i<numcount; i++){
cin>>numbers[i];
}
int numbers = shellsort(numbers);
cout<<numbers;
}
Beware of what you have promised to return and what you have returned.
int shellsort(int arr[]){
//....
return arr;//<--- ****not an int****
}
Now, either change to use a vector:
std::vector<int> shellsort(std::vector<int> arr){
int gap = floor(arr.size()/2);
for(int gap = floor(arr.size()/2); gap < 0; gap=gap/2){
//as above
}
return arr;
}
or send in the size
int * shellsort(int * arr, size_t size){
int gap = floor(size/2);
for(int gap = floor(size/2); gap < 0; gap=gap/2){
//as above
}
return arr;
}
The problem is that inside your function, the actual size of int arr[] is not known, and arr decays to a pointer, hence sizeof(arr) == sizeof(int*).
I suggest you rewrite your code to use std::vector:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
std::vector<int> shellsort(std::vector<int> arr){
int gap = floor(arr.size()/2);
for(int gap = floor(arr.size()/2); gap < 0; gap=gap/2){
for(int i = gap; i < arr.size(); i++){
// ...as before...
}
}
return arr;
}
int main(){
int numcount;
cin>>numcount;
std::vector<int> numbers(numcount);
for(int i; i<numcount; i++){
cin>>numbers[i];
}
numbers = shellsort(numbers);
for (std::size_t i = 0u; i < numbers.size(); ++i)
cout<<numbers[i];
}
Related
You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
You don't need to print or return anything, just change in the input array itself.
#include <iostream>;
using namespace std;
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i]<<i;
}
void UpdateArr(int arr[], int n)
{
int i = 0, j = n - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
cout<<' printArr(arr[], n)';
}
int main()
{
int t;
cin>> t;
int n;
cin>> n;
int input[100];
for(int i=0; i<n; i++) {
cin >>input[i];
}
int arr[100] ;
n = sizeof(arr) / sizeof(arr[0]);
UpdateArr(arr, n);
return 0;
}
I'm not sure what are you exactly expecting the output to be (pls edit it and show the expected output) but I think this is what you need to do
#include <iostream>
#include <iomanip>
using namespace std;
void UpdateArray(int Arr[], size_t n) {
for (size_t i = 0; i < n / 2; i++) {
int Holder = Arr[i];
Arr[i] = Arr[~i + n];
Arr[~i + n] = Holder; } }
int main() {
int Arr[7] = { 1,2,3,4,5,6,7 };
UpdateArray(Arr, 7);
for (int i = 0; i < 7; i++) {
std::cout << Arr[i] << "\n"; }
return 0; }
size_t is like an int but it can't go into negative, but it can take bigger positive numbers, you can replace it with int, it shouldn't make a difference.
so we loop through half the array, replacing first items with last, the [~i + n] flips the value to the other side, so like index 4 in a array size of 20 will become 15
I'm fairly new to programming and was trying to create a program which creates a one dimensional array with random numbers from a certain range and then prints it out. I managed to make a function to create the array but I'm having trouble actually printing out the array I made. I have a general idea of what the problem might be but no clue as to how to fix the code.
Here is the code in question:
#include <iostream>
using namespace std;
int *create(int n)
{
int *arr = new int [n];
for (int i = 0; i > n; i++)
{
arr[i] = rand() % 100;
}
}
int main ()
{
int n = 12;
int *arr = create(n);
cout << "this is the array: ";
for (int i = 0; i > n; i++)
{
cout << arr[i] << " ";
};
delete[] arr;
return 0;
}
There are two errors in your code:
You are not returning your array from create()
Your loop condition is incorrect.
Fixed code:
#include <iostream>
using namespace std;
int *create(int n)
{
int *arr = new int [n];
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100;
}
return arr;
}
int main ()
{
int n = 12;
int *arr = create(n);
cout << "this is the array: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
};
delete[] arr;
return 0;
}
I dont see how you code compiles.
You are not returning anything from function.
both your loop conditions should be <n
This way is works but your design is very poor, unless you are just learning handling pointers.
#include <iostream>
using namespace std;
int* create(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100;
}
return arr;
}
int main()
{
int n = 12;
int* arr = create(n);
cout << "this is the array: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
};
delete[] arr;
return 0;
}
The only missing part in your code - you are not returning your arr variable from your create function.
int *create(int n)
{
int *arr = new int [n];
for (int i = 0; i > n; i++)
{
arr[i] = rand() % 100;
}
return arr;
}
This way it will work:)
You made a mistake in the loop. you used '>' instead of '<' line 22 should be.
for (int i = 0; i < n; i++)
The loop stopped before the first iterations because i=0 was smaller than n=12.
I've been struggling with one issue, I have to build funtions that have to create and fill and print 2d dynamic array.
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
using namespace std;
void create_and_fill(int **T, int m, int n)
{
T = new int *[m];
for (int i = 0; i < m; i++)
{
T[i] = new int[n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
T[i][j] = -100 + rand() % 201;
}
}
}
void print(int **T, int m, int n )
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << T[i][j] << "\t";
}
cout << endl;
}
}
int main()
{
const int m = 5;
const int n = 6;
int **A = NULL;
create_and_fill(A, m, n);
print(A, m, n);
int **B = NULL;
create_and_fill(B, m, n);
return 0;
}
creating and filling works well and if I put some cout inside create_and_fill functions it also prints array. But if I try to print it using print function, there are some exceptions about forbidden actions.
I simply don't understand why some functions can do this and other don't and how to fix it. Thanks!
The problem is you are passing the pointer by value. You allocate and fill the array then you leak, because the changes are not stored in the original pointer you passed to the function. If you want to modify the pointer itself you need to pass it by reference:
void create_and_fill(int **&T, int m, int n)
You don’t delete the array anywhere in your code, so you have memory leak. Please note that every new should come with a delete.
I have it so that the user defines the size of the vector and then a vector is filled. Then that vector is sorted using bubble sort (homework assignment). However, when the "sorted" vector is displayed, there are different numbers in it than what were in the original creation. How do I first create the vector, display it, then sort it and display it??
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
size = data.size();
//Sorting
void bubbleSort(vector<int> & data);
{
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
First off:
make sure to include all necessary header files, e.g. stdlib.h for your used rand() function.
get rid of all unused variables, like average, median and size.
declare your bubbleSort function outside of main function, and add additional checkup code to prevent sort if list has not more than one element.
The sort problem is related to this code snippet of yours:
for (int i = 0; i<size -1 - k; i++) { ... }
Simply remove -1
To fix your sort problem, and for better output, use following code:
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
using namespace std;
void bubbleSort(vector<int>& data)
{
if(data.size() <= 1)
return;
for(int i=1; i<data.size(); i++)
{
for(int j=0; j<data.size()-i; j++)
{
if(data.at(j) > data.at(j+1))
{
int temp = data.at(j+1);
data.at(j+1) = data.at(j);
data.at(j) = temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout << "Vector Length?: ";
cin >> n;
// Filling vector
for(int i=0; i<n; i++)
data.push_back(rand()%10+1);
cout << "Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
// Sorting
bubbleSort(data);
cout << endl << "Sorted Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
return 0;
}
Hope this helps ;)
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
void printVector(vector<int> & data)
{
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
//Sorting
void bubbleSort(vector<int> & data);
{
size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
}
}
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
printVector(data);
bubbleSort(data);
printVector(data);
}
If you are planning to define function void bubbleSort(vector & data) later you need to declare it before calling it.\
void bubbleSort(vector<int> & data);
int main()
{
// Here your code
bubbleSort(data);
//Here your code
}
You need to define variables only just before you need it. And if you declare and never use it, you will get unused variable warnings. So better you can comment all these variables. You can un-comment whenever you need.
//double average=0.0;
//int median = 0;
You should call your function bubbleSort() from main() as follows:
bubbleSort(data);
You are not using the iterator indexes properly to sort the elements of vector. If you change your function bubbleSort as follows it will work
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
complete program for your reference:
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
int main()
{
int n;
//double average=0.0;
//int median = 0;
//double size = 0.0;
//int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
// int n =10;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
bubbleSort(data);
std::cout<<"sorted vector"<<"\n";
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
I'm writing a program that will shift the the array in this code by two, For example, a[5] = {0,1,2,3,4} and output {3,4,0,1,2} I wrote code already, but I'm missing something.. Appreciate any help!
#include <iostream>
using namespace std;
void circularShift(int a[], int size)
{
for (int i = size-2; i >=0; i--)
{
int temp = a[i+1];
a[i+1] = a[i];
}
}
int main()
{
int a[5] = {0,1,2,3,4};
int size = 5;
circularShift(a, 5);
for (int i=0; i < size; i++)
{
cout << a[i]<< " ";
}
return 0;
}
Using std::rotate:
void circularShift(int a[], int size)
{
assert(size >= 2);
std::rotate(a, a + size - 2, a + size);
}
Demo
void circularShift(int a[], int size)
{
int tmp1 = a[size - 1];
int tmp2 = a[size - 2];
for (int i = size-3; i >=0; i--)
{
a[i+2] = a[i];
}
a[1] = tmp1;
a[0] = tmp2;
}
try this -
void circularShift(int a[], int size)
{
int tmp = a[0];
for (int i = 0; i < size-1 ; i++)
{
a[i]=a[i+1];
}
a[size-1] = tmp;
}
Your function is not right:
It accesses a[5] in the 1st iteration
If you need to shift right twice then you need to calculate the new indexes for the elements:
newIndex = (oldIndex+2)%5
It will make sure you have a circular shift in the array.
Although it is high complexity method but it still works for your problem.
#include <iostream>
using namespace std;
void circularShift(int a[], int size)
{
int no;
int rotate_no = 2;
for(int j=0; j<rotate_no; j++)
{
no = a[size-1];
for (int i=0 ; i<size ; i++)
{
int temp = a[i];
a[i] = no;
no = temp;
}
}
}
int main()
{
int a[5] = {0,1,2,3,4};
int size = 5;
circularShift(a, 5);
for (int i=0; i < size; i++)
{
cout << a[i]<< " ";
}
return 0;
}
Try this one
void circularShift(int a[], int size, int rotations)
{
int temp = a[0];
for (int i = 0; i <size; i++)
{
int temp1 = a[((i+1)*rotations)%size];
a[((i+1)*rotations)%size] = temp;
temp = temp1;
}
}