answersLogoWhite

0

How do you reverse an array in c?

Updated: 8/10/2023
User Avatar

Wiki User

8y ago

Best Answer

#include <iostream>

using namespace std;

int main()

{

const int max=26;

char c[max];

int i, j;

// Initialise array with letters of the alphabet (ascending order).

for(i=0; i<max; ++i )

c[i]='a'+i;

// Print array.

for(i=0; i<max; ++i)

cout<<c[i];

cout<<endl;

// Reverse array:

// work from both ends, swapping elements as we go,

// stopping when we reach the middle elements.

for(i=0, j=max-1; i<j; ++i, --j)

// Swap values without using a temporary variable.

c[i]^=c[j]^=c[i]^=c[j];

// Print array.

for(i=0; i<max; ++i)

cout<<c[i];

cout<<endl;

return(0);

}

Output:

abcdefghijklmnopqrstuvwxyz

zyxwvutsrqponmlkjihgfedcba

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

#include

using std::cin;
using std::cout;
using std::endl;

int main()
{
const int arraySize = 128;
char myString[arraySize] = {'0'};
cout << endl << "Enter a string: " << endl;
cin.getline(myString, 128, '\n');

int numberOfChars = 0;
while ((numberOfChars < (arraySize - 1)) && (myString[numberOfChars] != NULL))
{
++numberOfChars;
}
cout << endl << "You've entered:"
<< endl << myString << endl;


char myStringReversed[arraySize] = {'0'};
int index = 0;
while ((index <= numberOfChars) && (myString[index] != NULL))
{
myStringReversed[numberOfChars - index - 1] = myString[index];
++index;

}
cout << endl << "Reversed string is: "
<< endl << myStringReversed << endl;


system("PAUSE");
return 0;

}


This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Working from each end of the array to the middle element(s), swap the corresponding elements.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

for (i=maxindex; i>=0; i--) printf("index: %d value: %d\n", i, array[i]);

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Point to both ends of the array and swap the elements. Work your way towards the middle, swapping as you go. When the pointers meet or cross each other, you are done.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Point to both ends of the array. Work towards the middle swapping values as you go. When the pointers meet or cross each other, the array is completely reversed.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you reverse an array in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i &lt; array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }


What is the array of string in c?

A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.


Sort array in ascending descending order in python?

Using sorted(array,reverse=True)


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


How do you make a C plus plus program that arrange the the numbers in ascending order?

Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include &lt;iostream&gt; using namespace std; int main(int argc, const char* argv) { int arraysize = 5; //Unsorted array size int array [] = { 5, 3, 4, 2, 1 }; //The array of numbers itself //Display the unsorted array cout &lt;&lt; "Before: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop &lt;= (arraysize - 1); loop++) { for (int c=0; c &lt;= (arraysize - 1); c++) //The sort loop { if (array[c] &gt; array[c + 1]) { //Swaps the two values in the array tmp = array[c]; array[c] = array[c + 1]; array[c + 1] = tmp; //Cleanup tmp = 0; } } } //Display the sorted array cout &lt;&lt; "After: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; return 0; }


How do you declare a string array and add elements to it in C plus plus?

You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.


What is the difference between array in c and c sharp language?

The syntax to access a particular element in an array are the same in both languages: For example: assume array is an array of 10 int(egers): to get the first element: array[0] (both are 0 based indexing] int i = 0; while (i &lt; array.Length) { // do something to array[i] } int i = 0; int length = sizeof(array) / sizeof(int); while (i &lt; length) { // do something to array[i] } However, an array in C# is also enumerable (C does not have this feature) in C#, you may loop thru the array by: foreach (int number in array) { // do something to array[i] } Plus, C# is an Object-Oriented Language, so that an array may be of some object types, not just those primitiives data types in C: object[] objectArray; // any object derived from Object may be placed into objectArray, not just struct. In another variation, an array may be of Delegate type in C#(sort of like function pointers in C)


How do you reverse the order of the elements in the array?

Start by pointing to each end of the array. Work your way towards the middle of the array, swapping elements as you go. When the pointers meet or pass each other, the array is completely reversed.


How do you use an array to show the commutative property?

If the array consists of r rows and c column, and the total number of cells in the array are n = r*c, then r*c = n and c*r = n so that r*c = c*r : which is commutativity of multiplication.


C program to copy one matrix to another matrix?

#include main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location number %d and it's value is %d.\n", location, minimum); return 0; }


How do you use in array?

cod a program student degree array in c language


How do you create a program that will let the user to enter 10 numbers and arrange it in ascending and descending?

#include "stdio.h" #define ARRAY_SIZE 10 void fill(float* array, int size); void spill(float* array, int size, char* delimiter); void bubble_sort(float* array, int size); void reverse(float* array, int size); void swap(float* a, float* b); int main(int argc, char* argv[]) { float numbers[ARRAY_SIZE]; fill(numbers, ARRAY_SIZE); bubble_sort(numbers, ARRAY_SIZE); spill(numbers, ARRAY_SIZE, " "); reverse(numbers, ARRAY_SIZE); spill(numbers, ARRAY_SIZE, " "); return 0; } void fill(float* array, int size) { int i = 0; while (i &lt; size) fscanf(stdin, "%f", array + (i++)); } void spill(float* array, int size, char* delimiter) { int i = 0; while (i &lt; size) fprintf(stdout, "%f%s", array[i++], delimiter); fputc('\n', stdout); } void bubble_sort(float* array, int size) { int i, j; for (i = 0; i &lt; size; i++) for (j = i; j &lt; size; j++) if (array[i] &gt; array[j]) swap(array + i, array + j); } void reverse(float* array, int size) { int i; for (i = size / 2; i &gt;= 0; i--) swap(array + i, array + (size - (i + 1))); } void swap(float* a, float* b) { float c = *a; *a = *b; *b = c; } fill gets the numbers from input spill sends them to output bubble sort will sort the array in ascending order reverse will reverse the list so that it is in descending order swap is used to swap two floats You can change float to double or int depending on which datatype you want to use.