C++ Program to Reverse the Contents of an Array, Exercises of Data Structures and Algorithms

This c++ document provides the code for a program that reverses the contents of an array. The program initializes an integer array 'a' with ten elements, creates two pointers 'p' and 'q' that point to the first and last elements of another integer array 'b' respectively. The 'for' loop is used to swap the elements of 'a' and 'b' by dereferencing the pointers and incrementing/decrementing them accordingly. The reversed array 'b' is then displayed using the 'cout' statement.

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<conio.h>
#include<iostream.h>
void main (void)
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
int b[10];
int *p,*q;
clrscr();
p=&a[0];
q=&b[9];
for (int i=0;i<10;i++)
{
*q=*p;
p++;
q--;
}
for (i=0;i<10;i++)
{
cout<<" "<<b[i];
}
getch();
}
docsity.com

Partial preview of the text

Download C++ Program to Reverse the Contents of an Array and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include<conio.h> #include<iostream.h> void main (void) { int a[10]={1,2,3,4,5,6,7,8,9,10}; int b[10]; int p,q; clrscr(); p=&a[0]; q=&b[9]; for (int i=0;i<10;i++) { q=p; p++; q--; } for (i=0;i<10;i++) { cout<<" "<<b[i]; } getch(); }

docsity.com