2009-05-06 05:32:50 UTC
It works on the first run, but when the program repeats it adds up the average or the allocated memory between runs. When I try clearing the memory using 'delete [] prt' I get an error:
"Windows has triggered a breakpoint in MovieStat.exe.
This may be due to a corruption of the heap, which indicates a bug in MovieStat.exe or any of the DLLs it has loaded."
#include
#include
using namespace std;
void selsort(int *, int);
int statmode(int *, int);
int countof(int *,int, int);
int main()
{
char again=0;
int Stud; //Stores number of array elements
int average;
int avertotal=0; //find total for average
int *studprt; //pointer that holds array
int mode=0;
do{
cout<<"how many students were surveyed?"<
studprt=new int(Stud);
cout<<"Enter the number of movies seen for each student."<
cout<<"How many movies has Student "<<(i+1)<<" seen?"<
avertotal+=*(studprt+i);
}
average=avertotal/Stud;
cout<<"Average: "<
statmode(studprt,Stud);
delete [] studprt; <------This gives me an error.
studprt=0; <------I try to reset the pointer
if(average>0)
cout<<"size is off"<
cout<<"size is okay"<
cout<<"Start Over? (y or n)"<
}while (again != 'n');
system("pause");
return 0;
}
void selsort(int *arra,int size)
{
int val;
int index;
double median=0.0;
double middiv=0.0;
int midmod=0;
int evencal=0.0;
double oddcal=0.0;
for(int scan=0;scan<(size-1);scan++)
{
index=scan;
val=arra[scan];
for(int count=scan+1;count
if (arra[count]
val=arra[count];
index=count;
}
}
arra[index]=arra[scan];
arra[scan]=val;
}
//for(int i=0;i
//cout<<*(arra+i)<<" ";}
//cout<
cout<
{
middiv=size/2;
evencal=middiv;
median=(*(arra+evencal)+*(arra+(evencal-1)))/2.0;
}
else //odd
{
middiv=static_cast
oddcal=middiv+0.5;
median=oddcal;
}
cout<<"Median: "<
int statmode(int *arra, int size)
{
//for(int i=0;i
//cout<<*(arra+i)<<" ";}
//cout<
int i;
for (i=0;i
if (countof(arra,size,*(arra+i))>count)
{
mode = *(arra+1);
count = countof(arra,size, *(arra+i));
}
}
cout<<"Mode: "<
}
int countof(int *arr, int size,int value)
{
int i;
int count = 0;
for (i = 0; i < size; ++i)
{
if (*(arr+i) == value)
count++;
}
return count;
}
OUTPUT:
how many students were surveyed?
2
Enter the number of movies seen for each student.
How many movies has Student 1 seen?
2
How many movies has Student 2 seen?
2
Average: 2
Median: 2.0
Mode: 2
Start Over? (y or n)
y
how many students were surveyed?
2
Enter the number of movies seen for each student.
How many movies has Student 1 seen?
1
How many movies has Student 2 seen?
1
Average: 3
Median: 1.0
Mode: 1
Start Over? (y or n)
y
how many students were surveyed?
4
Enter the number of movies seen for each student.
How many movies has Student 1 seen?
2
How many movies has Student 2 seen?
1
How many movies has Student 3 seen?
1
How many movies has Student 4 seen?
1
Average: 2
Median: 1.0
Mode: 1
Start Over? (y or n)