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
Related
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;
}
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.
I have a question about the exercise from my course:
Write a program that takes array of real numbers as parameter and replaces each element that is smaller than average of the first and last element, by this average. This is my code:
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i; i < 6; i++) {
cout << "Enter the numbers" << endl;
cin >> arr[i];
}
int f = arr[0];
int l = arr[n - 1];
double av = f + l / 2;
for (int i; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n;
int arr[n];
replaverage(arr, n);
cout << arr << " " << endl;
return 0;
}
Code is working, however as an output, it is giving some kind of address "0x7fff2306a5c0". I'm beginner so I apologize, maybe my error is stupid but I don't know how to fix it. Thanks for helping!
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Enter the number: ";
cin >> arr[i];
cout << endl;
}
int f = arr[0];
int l = arr[n - 1];
double av = (f + l) / 2;
for (int i = 0; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n = 6; // Making 6 since you had it hardcoded
int arr[n];
replaverage(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
First problem: Initialize your loop counters to 0;
Second problem: Initialize n in main being passed as parameter to
something
Third problem: Your average calculation is incorrect. It should be (f+l) / 2. Otherwise it will be doing l/2 + f, which is incorrect.
Fourth problem: You need to loop over your array to see all the
elements
I have error, which is highlighted "cout << array[i] << endl;" in this section. The line is under array[i]. The error is "argument list for class template "std::array" is missing ". i need a function to display the contents of an array, using an insertion sort. If this code is incorrect, does anyone know the code to output the contents of the array, using linear search.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
for (int i = 0; i < 8; i++)
{
cout << array[i] << endl;
}
system("pause");
}
const int SIZE = 8;
void insertionSort(int numbers[], int arraySize)
{
int i, j, insert;
for (i = 1; i < arraySize; i++)
{
insert = numbers[i];
j = i;
while ((j > 0) && (numbers[j - 1] > insert))
{
numbers[j] = numbers[j - 1];
j = j - 1;
}
numbers[j] = insert;
}
}
You didn't call your function insertionSort(int numbers[], int arraySize) in main(). Therefore nothing will happen to the original array.
Note that you need a return 0; statement inside int main(). And that you need to use numbers[i] instead of array[i]. And you need to set your insertionSort() to return "something" or to pass your numbers[] as a reference. Also not to forget about the function prototype before main().
This should work:
const int SIZE = 8;
void insertionSort(int [], int);
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
insertionSort(numbers, SIZE);
for (int i = 0; i < 8; i++)
cout << numbers[i] << endl;
system("pause");
return 0;
}
void insertionSort(int MyArray[], int size)
{
int i, j, insert;
for (i = 1; i < size; i++){
insert = MyArray[i];
j = i;
while ((j > 0) && (MyArray[j - 1] > insert)){
MyArray[j] = MyArray[j - 1];
j = j - 1;}
MyArray[j] = insert;}
}
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;
}
}