Hi,
I am getting above mentioned errors. Checked msdn, read the comments but I fail to implement it. Please help.
Error is regarding using the new and delete operators. Although I do write both, the compiler is not matching them and I guess there is a memory leak.
Program hangs after main returns, thats weired to me.
Thanks for all the help. Really appreciate it.
// 139_6_class.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "demography.h"
int main(int argc, char* argv[])
{
//User interface
printf("\nHomework-1 COP 3530 Summer 2007: by Anjana Patil\n");
printf("****************************************** ***********************************\n");
printf("\nThis program determines the population of each of the 3 regions\nwho's data is represented in an array of 3x3.");
printf("\n\nNOTE: This program doesn't have the ability of sizing the array.\n");
printf("\n**************************************** *************************************\n");
printf("\nThe array is 3 x 3 as shown below:\n\n");
printf("\t\tUrban\tSuburban\tExurban\n");
printf("Urban\t\t*\t+\t\t+\n");
printf("Suburban\t+\t*\t\t+\n");
printf("Exurban\t\t+\t+\t\t*\n");
printf("\n * internal growth rate and + annual migration in percentage\n\n");
printf("\nNOTE: User requires to key-in data for 3 arrays\n\n");
/////////////////////////////////////////////////////////////////////////////////////////
//nothrow version of new
//////////////////////////////////////////////////////////////////////////////////////////
Demograph* pObjDemograph = 0;
pObjDemograph = new(std::nothrow) Demograph();//Default Constructor is called
if (pObjDemograph == 0)
{
printf("\nAllocation failed");
exit(1);
}
else
{
pObjDemograph->set_score_array(); //User populates the score_array
pObjDemograph->set_zero_population(); //User populates the zeroth_population array
}
//////////////////////////////////////////////////////////////////////////////////////////
//pObjDemograph->set_score_array(); //User populates the score_array
pObjDemograph->display_score_array(); //Displays the array
//pObjDemograph->set_zero_population(); //User populates the zeroth_population array
pObjDemograph->population_size(); //Displays the final results
delete pObjDemograph;
pObjDemograph=0;
return 0;
}
//demography.h
//Header File for demography.cpp
//************************************************** **
//Array size is not specified at run-time.
//Built-in array size is specified at compile time.
//************************************************** **
#include <iostream>
#include <new>
#include <stdio.h>
#include <math.h>
using namespace std;
#define MAX_NUM 3
class Demograph
{
public:
Demograph();//Default constructor.
//***No argument constructor, since the class data member is an array. Arrays are constructed only using the default constructor. ***//
// Other member functions
void set_score_array(); //User populates the array
void display_score_array(); //Displays the contents of an array
void set_zero_population();//User inputs the initial population
void population_size(); //Calculates the population growth rate and their size in 'N' years
private:
//Data members
double score_array[MAX_NUM][MAX_NUM];
double zeroth_year[MAX_NUM]; //Population value at zeroth year*
Demograph* pObjDemograph;
}; //end of class declaration
#include "stdafx.h"
#include "demography.h"
/////////////////////////////////////////////////////////////////////////////////////////////
//Default constructor.
//Precondition: Array(1): size 3x3, Name: score_array[], stores demographic data for 3 regions.
// Array(2): size 1x3, Name: zeroth_year[], stores initial population in 3 regions.
//The arrays are not initialized.
//Postcondition:Both Array(1) and Array (2) are initialized to 0.0.
/////////////////////////////////////////////////////////////////////////////////////////////
Demograph::Demograph()
{
cout << "Default constructor is called. score_array[] and zeroth_year[]" << endl << "are initialized to zero.";
for (unsigned int i=0;i<MAX_NUM;i++)
for (unsigned int j=0;j<MAX_NUM;j++)
score_array[i][j]=0.0;
for (i=0;i<MAX_NUM;i++)
zeroth_year[i]=0.0;
}
///////////////////////////////////////////////////////////////////////////////
//Storing the keyed-in values to score_array[].
//Precondition: score_array[], cannot be initialized to data given in the problem.
//The array is not an empty set but it's initialized to zero.
//Postcondition: score_array[], is populated by the user.
///////////////////////////////////////////////////////////////////////////////
void Demograph::set_score_array()
{
cout << endl << endl << "First Input: ***Key-in their 9 data values to score_array[].***" << endl << endl;
for (unsigned int i=0;i<MAX_NUM;i++)
for (unsigned int j=0;j<MAX_NUM;j++)
{
cout << "Keyin score_array[" << i << "][" << j << "]=";
cin >> score_array[i][j];
}
}
//////////////////////////////////////////////////////////////////////////////
//Storing the keyed-in values to zeroth_year[].
//Precondition: zeroth_year[], cannot be initialized to data given in the problem.
//The array is not an empty set but it's initialized to zero.
//Postcondition: zeroth_year[], is populated by the user.
///////////////////////////////////////////////////////////////////////////////
void Demograph::set_zero_population()
{
cout << endl << "***Second Input: Key-in initial population of 3 regions to zeroth_year[].***" << endl;
for (unsigned int i=0;i<MAX_NUM;i++)
{
cout << "Key-in population in zeroth_year[" << i << "]=";
cin >> zeroth_year[i];
}
}
///////////////////////////////////////////////////////////////
//Displaying values from the score_array[]
//Precondition: The score_array[] is populated.
//Postcondition: score_array[] values are displayed using printf
///////////////////////////////////////////////////////////////
void Demograph::display_score_array()
{
cout << endl << "You entered the following score_array[] values." << endl;
for (unsigned int i=0;i<MAX_NUM;i++)
for (unsigned int j=0;j<MAX_NUM;j++)
{
cout << "Value of score_array[" << i << "][" << j << "]=";
cout << score_array[i][j] << endl;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Calculating population
//Precondition: score_array[] and zeroth_year[] are populated.
//The years for which the population size is to be estimated is to set to a 1D array, nth_year[].
//Post condition: Net rate of population growth is calculated and displayed in array, net_rate[].
//Estimated population size for years as said in the array (nth_array) is stored in array, popu_size[].
/////////////////////////////////////////////////////////////////////////////////////////////////////
void Demograph::population_size()
{
int nth_year[5]={10, 20, 30, 40, 50}; //Number of year for which population is to be calculated*
int response;
double net_rate[MAX_NUM]; //net annual growth rate of a region
static double popu_size[MAX_NUM][5];//Final answer is stored here.
cout << endl << "Key in 1 to input the years, for which you want to estimate the population size.";
cout << endl << "Key in 2 to test the data provided in the problem.";
cin >> response;
if (response == 1)
{
cout << endl << "***Third Input: Key in 5 values for the years, you want to estimate" << endl << "the population size.***";
for (int i=0;i<5;i++)
{
cout << endl << "Key in nth_year[" << i << "] = ";
cin >> nth_year[i];
}
}
//Following two "for" loops calulate the net_rate of population growth for each region.
//First "for" loop subtracts the outgoing population
//Second "for" loop adds the incoming population
double temp=0;
double sum=0;
for (int i=0; i<MAX_NUM; i++)
{
for (int j=0; j<MAX_NUM; j++)
{
if (i == j)
temp = score_array[i][j];
else
temp = -score_array[i][j]/100; //Non diagonal values are divided by 100 since they are %
sum += temp;
}
net_rate[i]=sum;
sum=0;
}
for (int j=0; j<MAX_NUM; j++)
{
for (int i=0; i<MAX_NUM; i++)
{
if (i!=j)
{
temp = score_array[i][j]/100; //Non diagonal values are divided by 100 since they are %
net_rate[j] = net_rate[j] + temp;
}
}
printf("net_rate of region[%d] is %f \n\n", j, net_rate[j]);
}
printf("\n**************************************** *************************************\n");
//This "for" loop calulates and displays the population size
try
{
for (i=0; i<MAX_NUM; i++)
{
for (int j=0; j<5; j++)
{
popu_size[i][nth_year[j]] = zeroth_year[i]*pow((net_rate[i]),nth_year[j]); // Population size = zeroth year pouplation * (net_rate)^year
printf("Population of %d %d-th year: %e \n", i, nth_year[j], popu_size[i][nth_year[j]]);
}
}
}
catch(const out_of_range& ex)
{
cout << endl << "out_of_range exception caught" << ex.what();
}
printf("\n0-th region is Urban, 1-th region is Suburban, 2-th region is Exurban");
printf("\n**************************************** *************************************\n");
printf("\nThank you and have a nice day\n");
}