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

Recommended Answers

All 2 Replies

>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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.