Friday, 5 July 2013

Reversing Integer Array + Dynamic Memory Allocation

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
clrscr();
int num,*ar,init=0,fin,temp;
  cout<<"Enter the Number of Elements for Array:";
  cin>>num;
  fin=num-1;
  ar=(int *)malloc(sizeof(int));  // Dynamic Memory Allocation
  for(int i=0;i<num;i++)
  {
    cout<<"Enter the number:";
    cin>>ar[i];
  }
 for( i=1;i<=num/2;i++)
 {
   temp=ar[init];
   ar[init]=ar[fin];
   ar[fin]=temp;
   init++;
   fin--;
 }
 cout<<"After Reversing\n";
 for( i=0;i<num;i++)
 {
  cout<<ar[i]<<" ";
 }
getch();
}

No comments:

Post a Comment