ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to run my program that distributes grades and puts it in a table. The code compiles but when I run it I get a Segmentation Fault. I'm still new to arrays, but I think the issue is where I'm trying to make the table. Here is my code:
Code:
#include <stdio.h>
#include "constant.h"
int readValues(int[]);
int findMedian(int[], int);
extern void bubblesort(int[], int);
int main()
{
int i,j;
int median[EXAMSCORES],meanExam[EXAMSCORES],meanStudent[NUMBERofSTUDENTS];
int grade[NUMBERofSTUDENTS],counts[101];
int scores[NUMBERofSTUDENTS][EXAMSCORES];
int sum=0;
for ( i = 0; i < NUMBERofSTUDENTS; i++);
{
for(j=0;j<EXAMSCORES;j++)
{
sum+=scores[i][j];
}
}
for(i=0;i<EXAMSCORES;i++)
{
for(j=0;j<NUMBERofSTUDENTS;j++)
{
grade[j]=scores[j][i];
}
bubblesort(grade,NUMBERofSTUDENTS);
median[i]=grade[NUMBERofSTUDENTS/2];
}
for(i=0;i<NUMBERofSTUDENTS;i++)
{
for(j=0;j<EXAMSCORES;j++)
{
sum+=scores[i][j];
}
meanStudent[i]=sum/EXAMSCORES;
}
for(i=0;i<EXAMSCORES;i++)
{
for(j=0;j<NUMBERofSTUDENTS;j++)
{
sum+=scores[j][i];
}
meanExam[i]=sum/NUMBERofSTUDENTS;
}
for(i = 0; i < EXAMSCORES; i++)
{
for(j=0;j<NUMBERofSTUDENTS;j++)
{
counts[scores[j][i]]++;
}
printf("EXAM GRADE DISTRIBUTION:\n");
printf("Student | Exam %d | Std Avg\n",i);
printf("Student %d | %d | %d\n",counts[i],j,meanStudent[NUMBERofSTUDENTS]);
printf("Exam Avg | %d | %d\n",meanExam[EXAMSCORES]);
printf("Exam Median | %d | %d\n",median[EXAMSCORES]);
//Are the variables correct?
for(i=0;i<101;i++)
{
if(counts[i]!=0)
{
printf("Score %d : %d - %d above, %d below ",i,counts[i],grade[NUMBERofSTUDENTS],grade[EXAMSCORES]);
//Are these variables correct?
}
}
printf("\n");
}
return 0;
}
Hint: You are printing values that are beyond the range of your grade array. And upon analyzing your code further, it appears that you have this same error elsewhere.
Last edited by dwhitney67; 04-20-2011 at 07:35 AM.
There are several errors. The most serious is that you never initialized the contents of scores[][], so those will all be garbage numbers, which will not be limited to the range 0 through 100.
I expect you were supposed to call readValues() somewhere near the beginning of main.
Also, it appears that you assume the array counts[] has been initialized to zeroes. Local variables in C don't work that way. If you want it initialized, your code needs to do that.
Also take a closer look at all your uses of the variable sum. You are not doing what you intend.
There are several errors. The most serious is that you never initialized the contents of scores[][], so those will all be garbage numbers, which will not be limited to the range 0 through 100.
I expect you were supposed to call readValues() somewhere near the beginning of main.
Also, it appears that you assume the array counts[] has been initialized to zeroes. Local variables in C don't work that way. If you want it initialized, your code needs to do that.
Also take a closer look at all your uses of the variable sum. You are not doing what you intend.
And I had readValues in there but the TA said not to include it :| lol. I'm talking to my professor today to see if he can help me so I'll try and work it out, and post later what I can get. On the bright side I got rid of the Segmentation Fault and learned the value of the gdb.
It's really not that hard and there are plenty of good resources online. You can easily teach yourself to program just using an online search engine like Google. That's how I learned it.
Maybe part of the problem is my CRT, but I can't read a single number in that image and I can make out very few of the words.
Can't you find a way to copy/paste the text of your output instead of taking a photo of the screen?
Quote:
I had readValues in there but the TA said not to include it
Despite my incredibly low opinion of those typically involved in formal teaching of computer science, I still doubt the above statement.
Maybe you misunderstood what the TA said.
Quote:
I'm talking to my professor today to see if he can help me so I'll try and work it out, and post later what I can get.
Did you look at all the comments in this thread about various errors in your program?
I didn't think you were far enough off track to need help from the professor.
You do need to get the data input your scores array, so if you are still struggling with the TA's wrong or misunderstood instructions, that may need some clarification from the professor.
Beyond that, it is best to read your own code carefully and think about what actually happens in each step of the program. Try to set aside the knowledge of what you want to have happen in each step in order to see what would actually happen. Which variables' prior values are depended on by that step? Did the earlier parts of the program prepare all those values? Does this step do the right thing with those values?
The TA commented out the readValue so I figured it wouldn't be important. I haven't been able to really get to the program today because of math homework and the professor wasn't there during his office hours. I'm doing my best to get this all worked out and I apologize for my noobness =D.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.