#include <iostream>
using namespace std;
int* reverse(int* list, int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
// Swap list[i] with list[j]
int temp = list[j];
list[j] = list[i];
list[i] = temp;
}
return list;
}
void printArray(const int* list, int size)
{
for (int i = 0; i < size; i++)
cout << list[i] << " ";
}
int main(){
int list[6] = { 1,2,3,4,5,6 };
int *ptr = reverse(list, 6);
printArray(list,6);
}
// CPP program to reverse array
// using pointers
#include <iostream>
using namespace std;
// Function to swap two memory contents
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Function to reverse the array through pointers
void reverse(int array[], int array_size)
{
// pointer1 pointing at the beginning of the array
int *pointer1 = array,
// pointer2 pointing at end of the array
*pointer2 = array + array_size - 1;
while (pointer1 < pointer2) {
swap(pointer1, pointer2);
pointer1++;
pointer2--;
}
}
// Function to print the array
void print(int* array, int array_size)
{
// Length pointing at end of the array
int *length = array + array_size,
// Position pointing to the beginning of the array
*position = array;
cout << "Array = ";
for (position = array; position < length; position++)
cout << *position << " ";
}
// Driver function
int main()
{
// Array to hold the values
int array[] = { 2, 4, -6, 5, 8, -1 };
cout << "Original ";
print(array, 6);
cout << "Reverse ";
reverse(array, 6);
print(array, 6);
return 0;
}
Code Example |
---|
Cpp :: break in c++ |
Cpp :: c++ cstring to string |
Cpp :: c++ code for bubble sort |
Cpp :: back() in c++ |
Cpp :: could not find the task c c++ active file |
Cpp :: int main() { |
Cpp :: c++ lambda |
Cpp :: c++ modulo positive |
Cpp :: overload of << c++ |
Cpp :: long to string cpp |
Cpp :: c++ vector resize |
Cpp :: how do you wait in C++ |
Cpp :: how to get the time in c++ as string |
Cpp :: c++ get maximum value unsigned int |
Cpp :: implementing split function in c++ |
Cpp :: how to empty string c++ |
Cpp :: sum of row s2 d array c++ |
Cpp :: c++ string conversion operator |
Cpp :: sort vector from largest to smallest |
Cpp :: check if element in dict c++ |
Cpp :: cuda shared variable |
Cpp :: cpp ifdef |
Cpp :: for loop in cpp |
Cpp :: C++ String Compare Example |
Cpp :: Traversing a C++ Array |
Cpp :: c++ data types |
Cpp :: c++ check if key exists in map |
Cpp :: bfs sudocode |
Cpp :: cpp ignore warning in line |
Cpp :: find the graph is minimal spanig tree or not |