The program is supposed to pen and read a file with student’s scores for a semester.  Display a chart with the average and median scores for each section and each lab.

Example: CS 111 this semester has 7 sections and 10 labs.  The output will be a chart with 7 rows and 20 columns.  Seven rows for sections 1 to 7.  Two columns (average and median) for each of the 10 labs.

Note: The average is rounded up to the nearest integer you must write your own function int myCeil(double n) that takes say 1.01 and returns 2 or takes 1.0 and returns 1

My problem is that i'm having trouble coming up with the median function, the average function might be giving the seg fault. Anway I can't figure it out. You should find an attachment for the in.txt file at the bottom of this message.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

// Prototypes for Program
void getArrayValues(int scores [] [50], int labs [], int numsections, int numlabs);
void average(int scores [] [50], int labs [], int numsections, int numlabs, int stAve[]);
//void median(int scores [] [50], int labs []);
void BubbleSort(int scores [] [50], int labs [], int numsections, int numlabs);
int main() {

// Declare Variables to Store Array Information
int scores [10] [50];
int labs [10];
int numsections;
int numlabs;
int stAve[numsections];


getArrayValues(scores, labs, numsections, numlabs);
average(scores, labs, numsections, numlabs, stAve);
//median(scores,labs);
BubbleSort(scores,labs,numsections,numlabs);
return 0;

}

void getArrayValues(int scores [] [50], int labs [], int numsections, int numlabs) {

// Declare Stream and Open Text File
ifstream inStream;
inStream.open("in.txt");

if (inStream.fail()) {
	cerr << "Can't open file!\n";
exit(1);
}
else {
	while (!inStream.eof()){
//"Garbage" Variable to Get Ride of Student ID Number
int id;
// Get Number of Sections and Labs from Text File
inStream >> numsections >> numlabs;
// Nested For Loops to Read Info from Text File into Array
for (int i = 0; i < numsections; i++) {
	inStream >> labs[i];
	for (int j = 0; j < labs[i]; j++) {
		inStream >> id;
			for(int k = 0; k < numlabs; k++){
				inStream >> scores[k][i];
			}
		}
	}
	// Close File
	 inStream.close();
	 		}
	 	}

}

void average(int scores [] [50], int labs [], int numsections, int numlabs, int stAve[])
{
	for(int stId = 0;  stId <= numsections;  stId++)
	{
		int sum = 0;
		for(int labNum = 1; labNum <= numsections; labNum++)
			sum = sum + scores[stId-1][labNum-1];
		stAve[stId-1] = sum/numlabs;
	}
}

/*void median(int scores [] [50], int labs [])
{

	int n = 0; //use
	long hold = 0;
	long other = 0;
	int holder = 0; //use
	int m = 0; //use
	if (labs % 2) //If number is odd means the middle number is median
	{
		n = (labs/2); //finds the very middle number
		n += .5; //it will return a #.5 so we add another .5 to counter that
		hold = static_cast<int>(n); // we convert #.0 to just #
		holder = scores[hold]; //holder = the # stored at scores[hold]
	}
	else
	{
		hold = (labs/2); //if choice is even then hold is equal to the higher middle #
		other = (hold - 1);//other is the lower middle number
		n = scores[hold]; //assign the value in hold to n
		m = scores[other];//asign the value in other to m
		holder = ((n+m)/2);//find the average of the 2 middle numbers
	}
}
*/
void BubbleSort (int scores [] [50], int labs [], int numsections, int numlabs) {

int i, j, k, temp;

for(i=0; i<10; i++)
	for(j=0; j<20; j++)
		for(k=0; k<20; k++){
			if(scores[i][j] < scores[i][k]){
			temp = scores[i][j];
			scores[i][j] = scores[i][k];
			scores[i][k] = temp;
                                                }
                                        }
for(i=0; i<10; i++)
	for(j=0; j<20; j++){
		if(scores[i][0] < scores[j][0]){
			for(k=0; k<20; k++){
			labs[0] = scores[i][k];
			scores[i][k] = scores[j][k];
			scores[j][k] = labs[0];
			}
		}
	}
}

attached is the in.txt file

Attachments
7 10
31
7702 72 57 97 99 87 63 96 94 66 73 
8046 60 69 72 96 66 95 84 55 56 74 
3924 63 58 81 96 73 100 59 97 69 89 
5257 60 54 82 99 63 87 69 56 87 92 
8990 93 78 50 59 60 65 66 87 88 70 
5794 76 81 67 67 50 56 97 95 91 60 
6368 90 76 59 68 76 82 69 53 66 87 
4414 93 81 50 93 62 84 83 71 77 84 
5020 66 68 58 77 94 52 82 100 85 69 
5372 51 66 53 81 58 86 72 62 83 96 
4573 63 99 98 80 51 74 96 78 95 79 
2883 50 64 67 56 52 54 66 92 85 50 
5800 91 58 66 87 73 71 71 78 72 96 
1748 93 89 74 91 87 57 70 95 60 80 
4660 52 56 80 50 80 72 52 99 96 78 
8120 62 86 60 83 81 90 51 98 65 61 
9484 72 54 52 94 75 92 99 91 64 81 
2023 75 76 90 92 85 51 62 53 99 94 
5633 91 84 57 85 99 59 69 60 72 57 
6170 57 70 87 67 67 90 54 75 68 59 
7133 79 54 70 80 83 97 100 64 86 57 
3866 93 98 93 53 50 81 83 99 79 58 
2417 83 57 77 90 68 66 55 68 97 77 
2263 92 58 92 82 82 85 100 69 56 59 
5709 92 65 71 98 54 77 59 100 77 87 
3832 65 72 90 78 51 65 70 75 78 58 
6069 70 88 91 98 70 76 88 96 88 86 
1123 69 69 65 86 76 89 79 66 99 71 
4172 93 89 80 94 71 89 70 92 75 78 
3834 95 66 75 94 87 55 52 73 87 82 
2443 74 67 93 77 91 86 90 51 100 90 
21
4422 79 60 54 67 82 80 52 72 63 72 
2641 99 50 94 89 70 54 83 89 90 66 
3746 94 68 87 71 69 69 55 58 52 91 
3132 77 56 77 70 74 71 81 59 87 99 
4671 92 80 77 57 90 80 71 73 81 58 
6605 51 65 69 92 51 78 68 95 85 50 
7947 83 65 99 53 58 76 66 69 52 60 
3785 79 84 61 52 65 77 68 85 78 97 
7097 57 76 79 77 64 58 96 60 71 55 
9391 57 60 66 69 96 84 78 88 78 78 
4004 98 96 85 62 64 53 76 72 55 54 
2625 68 88 50 65 67 95 88 62 87 77 
5019 56 84 57 66 86 82 71 53 77 64 
9627 64 97 76 55 57 80 57 74 53 78 
2835 65 54 62 79 76 76 67 82 63 67 
5845 98 74 80 60 60 78 84 65 66 91 
3616 67 71 56 59 81 99 91 79 61 85 
3654 99 94 71 78 77 73 59 54 97 76 
8986 96 94 91 73 72 91 70 62 68 57 
8749 95 86 69 93 56 69 57 66 57 92 
3258 76 95 60 79 61 96 63 54 51 87 
48
8137 77 67 51 76 76 64 71 63 63 80 
4473 74 57 89 73 79 64 86 90 58 85 
5907 56 87 78 78 91 78 90 75 82 72 
1145 69 80 93 95 78 83 61 84 91 95 
3268 76 72 62 51 73 92 54 56 72 94 
1734 96 66 100 96 81 67 62 70 75 59 
5877 79 96 98 83 63 53 81 76 94 67 
2624 57 75 71 98 77 92 56 98 50 50 
7985 85 90 51 81 66 95 60 78 96 82 
9662 69 57 77 93 82 72 68 66 90 73 
2445 96 74 65 81 66 81 50 51 64 68 
9456 84 67 71 75 91 78 63 68 68 66 
6443 80 81 80 52 69 84 63 73 77 55 
3665 86 68 99 79 54 77 51 97 69 64 
1593 64 74 87 91 92 75 95 89 60 93 
7929 62 54 62 51 97 76 90 61 73 99 
4683 95 60 97 53 52 80 53 98 83 76 
3642 61 76 54 99 51 81 92 59 71 58 
5040 97 72 91 90 55 88 51 73 99 89 
4679 54 95 80 57 83 90 97 72 64 96 
3503 72 62 69 65 53 53 72 73 52 74 
3209 83 51 56 88 71 56 86 65 56 53 
5164 84 65 54 54 60 50 57 65 80 75 
7236 94 98 84 83 66 91 96 84 94 51 
4218 67 95 74 95 63 61 54 85 74 62 
2529 68 85 61 66 82 55 57 97 59 53 
4024 76 62 54 76 96 59 84 66 96 88 
5026 90 99 89 90 97 74 74 69 70 100 
3467 98 63 87 53 76 80 50 99 50 89 
5635 69 99 90 54 88 66 64 70 58 85 
3359 87 64 84 59 51 65 64 100 85 72 
2232 69 73 70 97 53 58 51 51 89 85 
2402 65 96 95 59 73 80 76 83 69 78 
1220 55 75 89 67 63 81 83 81 50 57 
1735 51 100 89 85 97 82 88 83 98 51 
1894 67 58 77 54 78 90 63 93 56 77 
7664 99 94 50 87 55 85 57 79 52 100 
9327 72 65 87 57 53 97 53 77 67 93 
8998 71 97 90 67 59 62 57 63 83 75 
8545 82 52 70 69 52 73 76 75 71 55 
6256 85 57 91 86 97 54 65 90 90 69 
3041 77 88 81 60 61 51 76 61 61 54 
4201 75 64 69 70 99 89 73 86 53 84 
5575 77 86 96 59 62 74 65 54 90 87 
4845 63 96 90 81 60 53 93 59 59 60 
4277 56 98 51 90 68 77 82 64 87 91 
6667 66 84 64 72 94 78 50 53 53 69 
7371 84 70 54 84 65 96 69 88 68 60 
35
8316 73 89 65 74 95 85 90 82 60 95 
1373 71 77 59 76 84 78 69 59 62 67 
9651 83 68 90 75 63 52 94 74 70 96 
3906 76 60 54 78 59 99 71 52 68 63 
1609 90 59 99 77 64 97 93 93 84 69 
8327 76 87 73 72 96 99 58 69 62 72 
2033 84 89 58 59 60 81 81 74 61 76 
6676 78 100 98 68 82 92 66 97 54 74 
3342 78 94 100 90 65 85 72 78 67 96 
4956 57 58 53 62 77 68 58 98 71 76 
8430 97 54 83 60 77 76 90 72 86 77 
8521 76 80 88 73 77 93 74 59 94 82 
8838 70 62 82 65 72 53 81 58 56 88 
7689 52 84 76 76 94 65 65 99 85 61 
9227 98 74 70 55 59 99 66 67 56 52 
7124 52 57 72 61 81 73 99 85 71 55 
7991 80 88 89 75 99 51 86 72 88 96 
9254 87 79 91 66 70 89 55 60 86 63 
5841 99 65 83 71 63 85 54 85 52 92 
3904 89 96 50 86 100 61 90 97 72 68 
2250 66 79 69 72 61 55 63 91 85 71 
3557 57 97 95 65 63 83 85 69 80 62 
4010 80 62 70 98 79 84 77 64 55 53 
3757 88 68 84 52 82 83 97 80 82 96 
9248 69 55 99 89 53 52 99 73 85 74 
4743 79 89 88 56 69 75 69 67 88 74 
8206 99 51 88 81 70 89 70 63 64 100 
3301 98 84 93 50 93 72 89 100 53 95 
8160 51 59 52 75 71 74 77 85 85 90 
1591 64 79 90 75 82 77 80 70 86 79 
3256 94 89 76 60 87 89 59 93 100 85 
9178 50 90 77 79 76 82 62 52 68 59 
1120 93 81 74 72 65 95 94 72 93 98 
1572 73 80 85 82 75 100 68 58 65 77 
5262 82 93 95 57 61 92 75 59 63 69 
1
2800 73 86 98 95 81 84 70 74 91 94 
2
1394 83 96 89 97 64 85 64 69 55 57 
5656 90 66 100 70 55 99 55 61 50 55 
7
6010 94 91 91 96 66 60 55 77 88 83 
6281 56 52 64 65 94 70 78 99 58 81 
7449 70 79 70 78 71 72 59 55 95 50 
4003 74 54 89 100 95 96 74 70 62 61 
6284 76 55 86 81 87 58 63 95 100 57 
9910 62 66 52 72 67 79 58 68 100 51 
6282 56 77 88 53 54 83 67 94 91 52

>Seg Fault
Check your array indices. Use a debugger, or sprinkle the code liberally with debug prints statements. If you don't even know the general area that the segmentation fault is coming from then you aren't debugging properly, and debugging is a huge part of programming so you'd better start learning how to do it and get used to it.

^^
Exactly. You need to find where the seg fault is hitting. When I have that problem, I usually fill my program with stupid couts like "entering _____ function", "about to enter so and so loop", "got out of so and so loop" blah blah blah

This article has been dead for over six months. Start a new discussion instead.