SORTING error by using dynamic memory array

Discussion in 'C' started by sheikh, Oct 10, 2012.

  1. sheikh

    sheikh New Member

    Joined:
    Oct 9, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;
    
    	
    }
     
    Last edited by a moderator: Oct 10, 2012

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice