i'm new here. i searched the existing questions for the answer to this problem, and it did help me progress but the code i have is still returning '0' instead of the actual position in the index of the smallest value in the array.
any help would be appreciated, thanks.
#include<iostream>
using namespace std;
int smallestIndex(int arr[], int size);
int main()
{
int arr[6] = {500, 29, 36, 4, 587, 624};
cout << "Index position for smallest element in array: "
<< smallestIndex(arr, 6) << endl;
return 0;
}
int smallestIndex(int arr[], int size)
{
int temp;
int n = arr[0];
for (int i = 0; i > size; i++)
{
if (arr[i] < n)
n = arr[i];
temp = i;
}
return temp;
}
The condition i > size is wrong. It should be i < size.
Don't forget to initialize temp. Write it as int temp = 0; because the initial value of n is the 0th element of the array.
You forget to use a block and the value of temp will be wrong.
Fixed code:
int smallestIndex(int arr[], int size)
{
int temp = 0;
int n = arr[0];
for (int i = 0; i < size; i++)
{
if (arr[i] < n)
{
n = arr[i];
temp = i;
}
}
return temp;
}
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
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.
As array name acts like a pointer to the starting address of the array, so when passed to a function, why the array is not modifying. When I used a static pointer which just storing the address of the array. After that returning the array by using its name is not causing any problem. Why is it so?
#include<iostream>
using namespace std;
int main()
{
int a[10]={2,16,19,20,2,9,18};
int* bubble(int [],int);
cout<<"the sorted array is ";
int n=10;
int *ma=bubble(a,n);
for(int i=0;i<10;i++)
{
cout<<ma[i]<<'\n';
}
return 0;
}
int* bubble(int *a,int n)
{
int no_of_comparisons;
int ptr,temp;
static int *ma=a;
while(no_of_comparisons<=n-1-1)
{
ptr=0;
while(ptr<=n-1-no_of_comparisons-1)
{
if(a[ptr]>a[ptr+1])
{
temp=a[ptr];
a[ptr]=a[ptr+1];
a[ptr+1]=temp;
}
ptr+=1;
}
no_of_comparisons+=1;
}
return a;
}
Bubble sort is the simplest algorithm to implement and the slowest algorithm on very large inputs, by the way. The basic idea is, just to loop through array from i=0 to n and swap adjacent elements if they are out of order. Below I re-wrote your code, so that it is more readable, clear, and short. I hop it helps.
#include<iostream>
int* bubble(int [], int);
int main()
{
int arr[10] = {2, 16, 19, 20, 28, 9, 18, 22, 32,1};
int arr_size = 10;
std::cout << "Original Array: \n";
for(int i = 0; i < 10; i++)
std::cout << arr[ i ]<< '\n';
bubble(arr, arr_size);
std::cout << "Sorted Array: \n";
for(int i = 0; i < 10; i++)
std::cout << arr[ i ]<< '\n';
return 0;
}
int* bubble(int *a, int n)
{
for(int i = 0; i < n; i++)
{
for(int j = n - 1; j > i; j--)
{
if( a[j] < a[j - 1] )
{
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
return a;
}
The main problem in your code was, first the no_of_comparisons variable were not initialized. In your case, it should be 0 I think.
I have an assignment that I have been stuck on, and unable to figure out after quite a bit of research.
I need to sort an array of size 20 with values between 1 and 101, using a function that returns the smallest value in the array.
Here is my function that returns the smallest value in the array.
int problem5(int *arr, int size, int &m, int &n){//Definition Problem 5
int smallest = 101;
int smallestindex;
for ( int i=0; i < size; ++i ){
if ( arr[i] < smallest ){
smallest = arr[i];
smallestindex = i;
}
}
m=smallest;
n=smallestindex;
cout<<"Smallest value is "<<m<<endl;
cout<<"It's index is "<<n<<endl;
return n;
}`
And here is my function, I am trying to switch the index of the first value in the array with the index of the smallest value, then have the array not include the first value(smallest value) in the new array. Here is that code:
void problem8(int *x, int size){
int m = 101;
int n = 101;
int tmpsize = size;
problem4(x,20);
for(int i =0; i<size; i++){
swap(x[i],x[problem5(&x[i],tmpsize, m, n)]);
tmpsize = tmpsize - 1;
}
}`
For the first few loops it will not change the array, but will correctly identify the smallest value. Thank you in advance for the help.
swap(x[i], x[problem5(&x[i], tmpsize, m, n)]);
should be
problem5(&x[i], tmpsize, m, n);
swap(x[i], x[n]);
as currently you probably have out of bound access.
I suggest also to rename your variable n, m in problem8 into something like smallIndex, smallestValue. to result to something like:
int find_min(const int* arr, int size, int& value, int& index)
{
int smallest = 101;
int smallestindex;
for (int i = 0; i < size; ++i) {
if (arr[i] < smallest) {
smallest = arr[i];
smallestindex = i;
}
}
value = smallest;
index = smallestindex;
return n;
}
int problem5(int* arr, int size, int& m, int& n)
{
int res = find_min(arr, size, m, n)
std::cout << "Smallest value is "<< m << std::endl;
std::cout << "It's index is " << n << std::endl;
return res;
}
void selection_sort(int* x, int size)
{
int value = 101;
int index = 101;
int tmpsize = size;
for(int i = 0; i<size; i++) {
problem5(&x[i], tmpsize, value, index);
swap(x[i], x[index]);
tmpsize = tmpsize - 1;
}
}
void problem8(int* x, int size){
problem4(x, 20); // better name ?
selection_sort(x, size);
}
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;
}
}