How do I swap elements in the given code since I get message that expression must a modifiable lvalue? I need to swap function value and array element in a for loop.
int niz[2] = { 2,1 };
int temp;
for (int i = 0;i < 1;i++)
{
temp = niz[i];
niz[i] = minimum(niz, 2, i);
minimum(niz, 2, i) = temp;
}
for (int i = 0;i < 2;i++)
{
cout << niz[i] << endl;
}
#include <iostream>
using namespace std;
void swap(int arr[], int size);
int main(void) {
int x[2] = {2, 1};
cout << "Before: ";
for (int i = 0; i <= 1; i++) {
cout << x[i] << ", ";
}
swap(x, 2);
cout << endl << "After: ";
for (int i = 0; i <= 1; i++) {
cout << x[i] << ", ";
}
return 0;
}
void swap(int arr[], int size) {
for (int i = 0; i < size / 2; i++) {
arr[i] ^= arr[size - 1 - i];
arr[size - 1 - i] ^= arr[i];
arr[i] ^= arr[size - 1 - i];
}
}
Hope it helps you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.