I want to do bubble sort for n elements of arraylist. Instead of declaring the arraylist first like below, I want to create the arraylist using for loops.
So this is the code of declaring the arraylist first:
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
int comparisons=0;
for (i = 0; i < n-1; i++) {
swapped = false;
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
{
comparisons++;
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
swapped = true;
}
}
if (swapped == false)
break;
}
cout << "Number of comparisons = " << comparisons << endl;
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
}
which I do not want to do.
I have made this, but I do not know why it does not work. I'm still quite new with C++. May I know what's the problem. Thank you so much.
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
int comparisons=0;
for (i = 0; i < n-1; i++) {
swapped = false;
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
{
comparisons++;
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
swapped = true;
}
}
if (swapped == false)
break;
}
cout << "Number of comparisons = " << comparisons << endl;
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
There are two problems:
Arrays are 0-indexed.
Variable length array which is not really legal in C++.
So write main like:
// Driver code
int main()
{
int n = 5;
int* arr = new int[n];
for(int i=0; i<n; i++){
arr[i]=i+1;
cout<<arr[i];
cout<<endl;
}
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
delete[] arr;
}
Related
I wanted to write a program to take two arrays as input and convert the first array such that the difference of maximum value and minimum value from the first array gives the smallest possible number.
I tried to write a code to find a smaller number most closest to the maximum from the array in C++, but the function for finding the minimum works on Codelite, but not on other compilers;
Is there any fix to solve this, either to the code or the compiler?
Here is the code I tried:
#include <iostream>
using namespace std;
void swap(int A[], int B[], int n)
{
int x, y, temp;
for(x=0;x<n;++x)
{
for(y=0;y<n;++y)
{
if(A[x]>B[y])
{
temp = A[x];
A[x] = B[y];
B[y] = temp;
}
}
}
}
void sortas(int A[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
if (A[i] > A[j+1])
{
temp = A[i];
A[i] = A[j+1];
A[j+1] = temp;
}
}
}
}
int maxfind(int A[], int n)
{
int z, a;
a = A[0];
for(z=0;z<n;++z)
{
if(a<A[z])
{
a = A[z];
}
}
cout << "Max value in A is" << a << endl;
return a;
}
int minfind(int A[], int n, int amax, int amin)
{
int z, maxi;
maxi = amax;
for(z=0;z<n;++z)
{
if(maxi >= A[z])
{
amin = A[z];
}
else
{
maxi = maxi-1;
}
}
cout << "Mix value in A is" << amin << endl;
return amin;
}
int main() {
int z, t;
cout << "Enter number of test cases: ";
cin >> t;
int n, i, j, amax, amin;
for(z=0;z<t;++z)
{
cout << "Enter size of array" << endl;
cin >> n;
int A[n], B[n];
cout << "Enter Array A values:" << endl;
for(i=0;i<n;++i)
{
cin >> A[i];
}
cout << "Enter Array B values:" << endl;
for(j=0;j<n;++j)
{
cin >> B[j];
}
swap(A, B, n);
sortas(A, n);
cout << "Swapped and sorted array is: " << endl;
for(i=0;i<n;++i)
{
cout << A[i] << "\t" << B[i] << endl;
}
amax = 0;
amin = 0;
amax = maxfind(A, n);
amin = minfind(A, n, amax, amin);
}
return 0;
}
Here is the output to that code:
1 1 1 3 4 2
Max value in A is 1 Min value in A is 1
1 2 2
-1882830412 4 3 6 3
Max value in A is 2 Min value in A is -1882830412
The problem is with your bubble sort:
void sortas(int A[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++) // why not start at i + 1 ??
{
if (A[i] > A[j+1]) // j + 1 is out of bounds when j == n - 1
{
temp = A[i];
A[i] = A[j+1]; // <-- Some random value is written to A.
A[j+1] = temp; // <-- Overwriting some variable on the stack
// of main() (possibly B ?)
}
}
}
}
A correct bubble sort (this is not the pedantic bubble sort), this is probably the most used.
void sortas(int A[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (A[i] > A[j])
std::swap(A[i], A[j]);
}
}
}
The actual bubble sort algorithn (the "pedantic" bubble sort), swaps only occur on neighboring values.
void sortas(int A[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < (n - i) - 1; ++j)
{
if (A[j] > A[j + 1])
std::swap(A[j], A[j + 1]);
}
}
}
Use one or the other, for integers, the performance is identical.
The question I am trying to solve is the following:
Write a function that traverses (and prints) the element of an array with stride =7. To do this the update part in the loop will be i= (i+7) % n, where n is the array size.
Would this function visit all elements of the array? Try different array sizes to check when it is impossible to traverse all elements.
The code that I wrote below doesn't print the correct values in the arry even if the value of i is correct.
Can anyone help, I would really appreciate it.
#include <fstream>
#include <stdlib.h>
using namespace std;
int* CreateArray(int n);
void StrideArray(int arr[], int n);
int main()
{
int* arr = new int[3];
arr = CreateArray(3);
cout << "The Elements In The Array Are: " << endl;
for (int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
cout << endl;
StrideArray(arr, 3);
cout << "The Elements In The Array Stride 7 Are: " << endl;
for (int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
delete[] arr;
return 0;
}
int* CreateArray(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (rand() % 100);
}
return arr;
}
void StrideArray(int arr[], int n)
{
int i = 0;
for (int j = 0; j < n; j++)
{
i = (i + 7) % n;
arr[j] = arr[i];
}
}
The problem is in StrideArray you read back the modified values of arr.
void StrideArray(int arr[], int n)
{
int i = 0;
int puffer=new int[n];
for (int j = 0; j < n; j++)
{
i = (i + 7) % n;
puffer[j] = arr[i];
}
for (int j = 0; j < n; j++){
puffer[j] = arr[j];
}
debete[] puffer;
}
Is a good way to write the function.
Also it visits all element only if n isn't dividable by 7. So if n is not 7,14,21,...
Also to use cout you have to #include <iostream>
Your StrideArray function needs fixing; you are iterating over j but using i to index, which remains constant; and you are reassigning value at one index to another where as you are supposed to print it:
void StrideArray(int arr[], int n)
{
int i = 0;
for (int j = 0; j < n; j=j+7)
{
cout << arr[j] << endl;
}
}
I modified the rest of your code for demo:
#include <iostream>
#include <stdlib.h>
using namespace std;
int* CreateArray(int n);
void StrideArray(int arr[], int n);
int main()
{
int* arr = new int[3];
arr = CreateArray(21);
cout << "The Elements In The Array Are: " << endl;
for (int i = 0; i < 21; i++)
{
cout << arr[i] << " ";
}
cout << endl;
StrideArray(arr, 21);
delete[] arr;
return 0;
}
int* CreateArray(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (rand() % 100);
}
return arr;
}
Task: Array B should consist of numbers from array A, in which there are 2 identical consecutive numbers. Make the search itself for 2 consecutive numbers as a function. Faced the problem that I cannot indicate the correct value of the values after finding 2 consecutive characters.It turns out that cells that have no value are initialized.
#include <iostream>
using namespace std;
int* read_array(int N) // Dynamic A
{
int* read_array = new int[N];
for (int i = 0; i < N; i++)
{
read_array[i] = rand() % 3;
}
return read_array;
}
int* check_read_array(int arr[],int N) // Dynamic B from A with following digits
{
int* check_read_array = new int[N];
int Count = 0;
for (int j = 1; j < N; j++)
{
if (arr[j] == arr[j - 1])
{
check_read_array[Count++] = arr[j];
}
}
return check_read_array;
}
void print_array(int arr[], int N) // Array A
{
cout << "\nArray A" << endl;
for (int i = 0; i < N; i++)
{
cout << arr[i] << endl;
}
}
void print_array_checked(int checked_array[], int N) // Array B
{
cout << "\nArray B" << endl;
for (int i = 0; i < N; i++)
{
cout << checked_array[i] << endl;
}
}
int main()
{
setlocale(LC_ALL, "Russian");
//bool asc = true;
int N = 0;
cout << "Введите размерность и количество элементов массива" << endl;
cin >> N;
cout << "Введенное число = " << N << endl;
int* arr = read_array(N); //Array A
int* checked_array = check_read_array(arr, N); //Array B
print_array(arr, N); //Print Array A
print_array_checked(checked_array, N); //Print Array B
//Clear dynamic memory
delete[] arr;
delete[] checked_array;
}
#include <iostream>
using namespace std;
int list [50];
void bsort(int list[], int n);
void printArray(int list[], int n);
int main ()
{
for (int i = 100; i > 50; i--)
{
list[i] = i;
cout << list[i] << endl;
}
int n = sizeof(list) / sizeof(list[0]);
bsort(list, n);
printArray(list, n);
return 0;
}
void bsort(int list[], int n)
{
int i, j;
for (i = 0; i <= 48; i++)
{
for (j = i+1; j <= 49; j++)
{
int temp;
if (list[i] > list[j])
{
temp = list[i];
list[i] = list [j];
list[j] = temp;
}
}
}
}
void printArray(int list[], int n)
{
for (int i = 0; i < n; i++)
cout << list[i] << " ";
cout << endl;
}
I'm in an intro-level computer science course and I am currently trying to write a program that calls the function 'bsort' to arrange the elements of the array 'list' in increasing order but my output is 49 zeros and I am not sure why? My professor wanted us to initialize the array 'list' starting at 100 and ending with 51 (in decreasing order).
Your initialisation loop is incorrect. Try like this
for (int i = 0; i < 50; i++)
{
list[i] = 100 - i;
cout << list[i] << endl;
}
Your version did list[100] = 100, list[99] = 99, etc but the array only has size 50.
When I run the following only the large numbers come out but, the numbers for "Multiples of the minimum elements" doesn't show.
I can't see what's wrong with the code yet. Advice appreciated.
int A[15];
void fill(int arr[], int n, int left, int right)
{
for (int i = 0; i < n; i++)
arr[i] = rand() % (right - left + 1) + left;
}
void output(int arr[], int n, int w)
{
for (int i = 0; i < n; i++)
{
cout << setw(w) << arr[i];
}
cout << endl;
}
int findMinElem(int arr[], int n)
{
int minelem = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i] % minelem == 0) minelem = arr[i] ;
return true;
}
return false;
}
int main()
{
int n = 15;
fill(A, n, 0, 50);
output(A ,n , 15);
if (findMinElem(A, n ))
cout << "Multiples of the minimum elements =\n";
else
cout << "No multiples of the minimum element elements\n";
system("pause");
return 0;
}