stack around the variable "counter" corrupted?

i have no idea y that debugging error keeps coming up
the code runs fine just as i hit enter to close the screen when it says press any key this debugging error comes up saying"stack around variable counter was corrupted" runtime check failure #2

[code]
#include
#include
#include
using namespace std;


void print(int [], int);
void mean (const int[],int);
void mode (int[],int[],int);
void median (int[],int);
void bubble(int *array);


int main ( )
{
const int Size = 201;
int TestScores[Size] = {0};
int counter[50] = {0};
srand ((unsigned) time (0) );


for (int i = 0;i < 50;i++)
{
counter[i]=0;
}
for (int i = 0;i < Size;i++)
{
TestScores[i] = (rand() % 50)+1;
}

mean(TestScores,Size);
mode(counter,TestScores,Size);
median(TestScores,Size);


//print(TestScores,Size);


system("pause");
return 0;
}


void mode(int count[], int temp[], int size)
{
int mostoccurred = 0;
int mode = 0;

for (int i = 0;i < size;i++)
{
++count[temp[i]];
}
for (int rating = 1;rating <= 50;rating++)
{
if(count[rating] > mostoccurred)
{
mostoccurred = count[rating];
mode = rating;
}
}
cout<<"The mode is "<<mode<<" which occurs "<<mostoccurred<<" times"<<endl;
}


void mean(const int temp[],int Size)

{
int total = 0;
for (int i = 0;i < Size;i++)
{
total += temp[i];
}

cout<<"The mean is "<<static_cast<double>(total)/Size<<endl;

}

void print( int a[],int InputSize)
{
cout<<"
";
for (int i = 0;i < InputSize;i++)
{
cout<<a[i]<<" ";
}
}

void bubble(int array[], int size ) //Bubble sort function
{

for(int a = 0;a < size;a++)
{
for(int b = 0;b < a;b++)
{
if(array[a] > array[b])//ascending placement
{
int temp = array[a]; //swaping
array[a] = array[b];
array[b] = temp;
}

}

}

}

void median(int temp[],int size)
{
bubble(temp,size);
cout<<"The median is "<<temp[size/2]<<endl;
}


[/code]

Comments

  • [color=Blue]This error is a sign of an array getting overrun. Example: say you have an array of:[/color]
    [code]int array [50];[/code]
    [color=Blue]The possible indexes for this array would be in range 0..49. If you have code which says:[/color]
    [code]array [[color=Red]50[/color]] = ;[/code]
    [color=Blue]This line will cause the error you described. The issue in your code is a following expression:[/color]
    [code](rand()%50)+1[/code]
    [color=Blue]This expression will produce values in range 1..50 and if being used as an index - it will cause that error.[/color]
  • thank you very much error is gone
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories