function for elements of the array multiples of the minimum element - c++

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;
}

Related

Can't find the minimum in an array with respect to maximum from the same array

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.

Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted. occurred. What am I doing wrong?

The code will compile, but I gets a error saying the stack around the array was corrupted. I am mainly wanting to get what is in the main() to do a loop, but this error is making it just copy the same thing down instead of repeating the procedures. I am not sure how to fix the error, (I am pretty new to C++)
int main(void)
{
int array[10];
fillArray(array, 10, 1, 100);
srand(time(NULL));
int kthElement = 1 + (rand() % 10);
int largestElem;
largestElem = findKthLargest(array, 10, kthElement);
cout << "The largest element is " << largestElem << "\n" << "\n";
return 0;
}
void fillArray(int list[], int numElements, int minNum, int maxNum)
{
srand(time(NULL));
for (int i = 0; i < numElements; i++)
{
list[i] = minNum + (rand() % maxNum);
}
return;
}
int findKthLargest(int list[], int size, int element)
{
int largest;
int comparisons = 0;
for (int i = 1; i < size; i++)
{
largest = list[i];
int largestLocation = 1;
for (int j = 2; j < size - (i - 1); j++)
{
if (list[j] > largest)
{
largest = list[j];
largestLocation = j;
comparisons++;
}
}
swap(list[size - (i - 1)], list[largestLocation]);
}
cout << "Number of comparisons: " << comparisons << "\n";
return largest;
}

Bubble sort with array size

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;
}

Bubble Sort Algorithm in c++

#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.

Efficiency and array crashing

Whenever I run this code and enter a number over 500,000, the program crashes. Also is there away to make this more simple/efficient without using: vectors, multiplication, division and %. Thanks a lot!
#include <iostream>
using namespace std;
int get_primes(int array[], int num);
void sieve(int array[], int num);
void goldbach(int primes[], int nprimes, int num);
void show(int array[], int num);
int main()
{
int num;
cout << "Enter a number to calculate up to." << endl;
cin>>num;
if ( num < 2 )
return 0;
int array[num];
array[0]= array[1]= 0;
for ( int i= 2; i < num; ++i )
array[i]= i;
int nprimes = get_primes(array, num);
show(array, nprimes);
goldbach(array, nprimes, num);
return 0;
}
void show(int array[], int num)
{
for (int i=0; i<num; i++)
if (array[i] > 0)
cout << array[i] << " "<< endl;
cout << endl;
}
int get_primes(int array[], int num)
{
sieve(array, num);
int pos = 0;
for (int i = 2; i < num; ++i)
if (array[i] > 0)
array[pos++] = array[i];
return pos;
}
void sieve( int array[], int num )
{
for ( int i= 0; i < num; ++i )
{
if ( array[i] > 0 )
{
for ( int j = i+i; j < num; j += i )
{
array[j] = 0;
}
}
}
}
void goldbach(int primes[], int nprimes, int num)
{
int a;
for (int a = 4; a<=num; a+=2)
{
bool found = false;
int i, j;
for (i = 0; !found && i < nprimes && primes[i]; ++i)
for (j = 0; !found && j < nprimes && primes[i]; ++j)
{
found = a == (primes[i] + primes[j]);
if (found)
cout << a << '\t' << primes[i] << " + " << primes[j] <<endl;
}
if (!found)
cout << a << "\tnot found" << endl;
}
}
Saying int array[num] is saying "allocate num * sizeof(int) bytes from the stack."
So it might be that your stack is only one megabyte in size, and when you ask for more you hit an underflow condition.
Change
int array[num];
to
int* array = new int[num];
and change all the function arguments from using
int array[]
to
int* array
you will still be able to access the pointers normally like array[i].
I would recommend you to look up stack vs heap memory in C++ to understand why this works, and to read up on pointers too.