Hi:
I would like a little help with debugging this program.
I have ran it and the error message is printed at the botum(segmentation fault?). There may be several problems with the code.
Please help!
//This program will except numbers and return the median
#include <iostream.h>
int BubbleSort(int [], int);
int main()
{
int numgrades;
int nums[numgrades];
int i, sum, moves;
moves = BubbleSort(nums, numgrades);
cout << "Enter the number of grades to be processed: ";
cin >> numgrades;
int *grade = new int[numgrades]; //Create the array
for(i = 0; i < numgrades; i++)
{
cout << "\bEnter a grade: ";
cin >> grade;
}
cout << "The sorted list is: ";
for(i = 0; i < numgrades; i++)
cout << " " <<nums;
cout <<"\n" <<moves << " were made\n";
return 0;
}
int BubbleSort(int num[], int numgrades)
{
int i, j, temp, numel, moves = 0;
for (i = 0; i < (numel - 1); i++)
{
for(j = 1; j < numel; j++)
{
if (num[j] < num[j-1])
{
temp = num[j];
num[j] = num[j-1];
num[j-1] = temp;
moves++;
}
}
}
}
int findmedian(int a[], int n)
{
int mid;
if(n%2 == 0)
mid =n/2;
else
mid =n/2 +1;
cout <<"The median is: "<<mid;
cout <<endl;
return a[mid];
}
python:~$ g++ median.cpp
python:~$ a.out
Segmentation fault
python:~$ exit
Script done on Tue Apr 24 23:50:02 2001
Thanks for any help provided
rego
I would like a little help with debugging this program.
I have ran it and the error message is printed at the botum(segmentation fault?). There may be several problems with the code.
Please help!
//This program will except numbers and return the median
#include <iostream.h>
int BubbleSort(int [], int);
int main()
{
int numgrades;
int nums[numgrades];
int i, sum, moves;
moves = BubbleSort(nums, numgrades);
cout << "Enter the number of grades to be processed: ";
cin >> numgrades;
int *grade = new int[numgrades]; //Create the array
for(i = 0; i < numgrades; i++)
{
cout << "\bEnter a grade: ";
cin >> grade;
}
cout << "The sorted list is: ";
for(i = 0; i < numgrades; i++)
cout << " " <<nums;
cout <<"\n" <<moves << " were made\n";
return 0;
}
int BubbleSort(int num[], int numgrades)
{
int i, j, temp, numel, moves = 0;
for (i = 0; i < (numel - 1); i++)
{
for(j = 1; j < numel; j++)
{
if (num[j] < num[j-1])
{
temp = num[j];
num[j] = num[j-1];
num[j-1] = temp;
moves++;
}
}
}
}
int findmedian(int a[], int n)
{
int mid;
if(n%2 == 0)
mid =n/2;
else
mid =n/2 +1;
cout <<"The median is: "<<mid;
cout <<endl;
return a[mid];
}
python:~$ g++ median.cpp
python:~$ a.out
Segmentation fault
python:~$ exit
Script done on Tue Apr 24 23:50:02 2001
Thanks for any help provided
rego