I\'m trying to find the root of the problem with this program. Could someone hel
ID: 3657306 • Letter: I
Question
I'm trying to find the root of the problem with this program. Could someone help me?
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubblesort(int*, int);
double getAverage(int*, int);
int main()
{
int size, i;
int *scores;
double avg;
//get size
cout << "Please input the number of scores ";
cin >> size;
//allocate size memory to scores
scores = new int[size];
for ( i=0; i<size; i++ )
{
cout << "Please enter a score ";
cin >> *scores;
scores++;
}
scores -= size; //get scores back to pointing first element
//bubble sort
bubblesort(scores, size);
//sum array
avg = getAverage(scores, size);
//print infomation
cout << "The average of the scores is " << avg;
cout << " Here are the scores in ascending order ";
for ( i=0; i<size; i++ )
{
cout << *scores << endl;
scores++;
}
//deallocate memory
delete[] scores;
return 0;
}
void bubblesort(int *arr, int size) //bubblesort accending order
{
int temp;
for ( int i=0; i<size-2; i++ )
{
for ( int j=0; j<size-1; j++ )
{
if ((*arr) > *(arr + 1))
{
temp = (*arr);
*arr = *(arr + 1);
*(arr + 1) = temp;
}
arr++;
}
arr -= (size - 1); //make size point to arr[0] again
}
return;
}
double getAverage(int* arr, int size)
{
double sum = 0;
for ( int i=0; i<size; i++ )
{
sum += *arr;
arr++;
}
return sum / size;
}
Explanation / Answer
What does mean a Debug assertion? A debug build of an app can have extra checks. In MFC (that I presume you're using), that means various preconditions are enforced by stopping the app and displaying a message. The environment "asserts" that something is true, if it isn't, it stops dead in it's tracks so you can fix it. How can I debug it? Run your app in the debugger as a debug build. I should stop on the line of code that's unhappy with you. The fail comes from AfxGetInstanceHandle() and I don't understand the meaning of the fail. You've probably failed to initialise the library or some object in the library correctly. If the fail comes from Library... how can I plot a simple graphic in C++/CLI? Dunno, it depends on the library.