Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   SORTING error by using dynamic memory array (http://www.go4expert.com/forums/sorting-error-using-dynamic-memory-array-t29186/)

sheikh 10Oct2012 18:54

SORTING error by using dynamic memory array
 
Code:

#include"stdafx.h"
#include<stdlib.h>
#include <iostream>
#include <conio.h>
using namespace std;
struct rectinfo
{
        int height,width,area,perimeter;
};

void genData (rectinfo*);
void calAreaPer (rectinfo*);
void display (rectinfo*);
void sort(rectinfo *r[],int);
int main()
{
        int n;
cout<<"ENter num please"<<endl;
cin>>n;
       
        rectinfo *shape=new rectinfo[n];
        for(int x=0;x<n;x++)
        {

        genData(&shape[x]);
calAreaPer(&shape[x]);
display(&shape[x]);
}sort(&shape,n);
        delete [] shape;
        return 0;
}


void genData (rectinfo* R)
{

R->height=rand()%21;
R->width=rand()%21;
       
}
void calAreaPer (rectinfo* A)
{
       
        A->area= (A->height*A->width);
        A->perimeter=2*(A->height + A->width);
       
}
void sort(rectinfo *r[],int size)
{
for(int y=0; y <size-1; y++)
{
for ( int k =1; k <size; k++ )
if(r[y]>r[k])
{
rectinfo *temp=r[y];
r[y] = r[k];
r[k] = temp;
}
}
for(int yy=0;yy<size;yy++)
cout<<"area"<<r[yy]<<endl;
}
void display (rectinfo* B)
{
       
        cout<<"Randomnly generated value of height is "<<B->height<<endl;
        cout<<"Randomnly generated value of width is "<<B->width<<endl;
        cout<<"Area of rectangle is :"<<B->area<<endl;
        cout<<"Perimeter of rectangle is :"<<B->perimeter<<endl;

       
}



All times are GMT +5.5. The time now is 03:53.