LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2011, 11:06 PM   #1
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Rep: Reputation: 0
Exclamation Keep Getting Segmentation Fault


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;
}
Thanks in advance for the help.
EDIT: Here's a pic of what is "supposed" to come out with the number of tests and exams being defined in the constant.h file,
http://i1216.photobucket.com/albums/...e/Untitled.png

Last edited by MrxNine; 04-19-2011 at 11:17 PM.
 
Old 04-20-2011, 12:23 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Step through it under the debugger to determine exactly *where* you're getting the segmentation violation.

PS:
I looked at your array definitions and your loops - superfically, things look OK. My guess is the problem's occurring in your sort routine.

But the debugger will tell you for sure
 
Old 04-20-2011, 12:31 AM   #3
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
Step through it under the debugger to determine exactly *where* you're getting the segmentation violation.

PS:
I looked at your array definitions and your loops - superfically, things look OK. My guess is the problem's occurring in your sort routine.

But the debugger will tell you for sure
My professor hasn't really shown us the debugger. How can I get to it?
 
Old 04-20-2011, 12:34 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by MrxNine View Post
My professor hasn't really shown us the debugger. How can I get to it?
Code:
gcc -g fileName.c
gdb a.out
r
You may need to install gdb through your package manager.
 
Old 04-20-2011, 01:48 AM   #5
GVrooman
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 45

Rep: Reputation: 11
If you don't have a debugger, you can insert print statements in various parts of your program to see how far it gets before it crashes.
 
Old 04-20-2011, 03:51 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MrxNine View Post
My professor hasn't really shown us the debugger. How can I get to it?
How about entering into your favorite web search engine the word

debugger

?

And probably more specifically,

linux debugger

?

...

Yes, 'printf' statements are quite a good way, and better yet consider

Code:
fprintf(stderr, ...);
- if you are on Linux, read

man 3 fprintf
.
 
Old 04-20-2011, 07:15 AM   #7
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Original Poster
Rep: Reputation: 0
I ran the debugger and it said the error was at main c:58
58 counts[scores[j][i]]++;
 
Old 04-20-2011, 07:33 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
This line of code is fishy:
Code:
printf("Score %d : %d - %d above, %d below ",i,counts[i],grade[NUMBERofSTUDENTS],grade[EXAMSCORES]);
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.
 
Old 04-20-2011, 07:41 AM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It would seem that you are not initializing the scores array; you define it, then use it... but never initialize it.

Undoubtedly, since there are "garbage" values in the scores array, your app may find itself addressing the count array in an odd manner here:
Code:
counts[scores[j][i]]++;
Perhaps if you posted all of the code (specifically one that calls readValues()), you might get further along.
 
Old 04-20-2011, 07:47 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 04-20-2011 at 07:52 AM.
 
Old 04-20-2011, 11:04 AM   #11
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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.
Yeah when I ran it, it came out like this:

http://i939.photobucket.com/albums/a...e/Untitled.png

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.
 
Old 04-20-2011, 01:05 PM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
http://www.google.com/search?client=...UTF-8&oe=UTF-8

http://www.google.com/search?client=...UTF-8&oe=UTF-8

http://www.google.com/search?client=...UTF-8&oe=UTF-8

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.

Last edited by MTK358; 04-20-2011 at 01:06 PM.
 
Old 04-20-2011, 01:45 PM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MrxNine View Post
Yeah when I ran it, it came out like this:
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?
 
1 members found this post helpful.
Old 04-20-2011, 09:52 PM   #14
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Original Poster
Rep: Reputation: 0
Here is what it looks like when it is ran (huge block of text incoming):

Student | Exam 0 | Std Avg
Student 2790 | 200 | -8471835
Exam Avg | 0 | 200
Exam Median | 1075120320 | 200
EXAM GRADE DISTRIBUTION:
Score 0 : 2790 - 332810952 above, 0 below Score 2 : 1073835168 - 332810952 above, 0 below Score 3 : 15 - 332810952 above, 0 below Score 4 : 1073818945 - 332810952 above, 0 below Score 5 : -1073744300 - 332810952 above, 0 below Score 6 : 1073767846 - 332810952 above, 0 below Score 7 : 134513097 - 332810952 above, 0 below Score 8 : -1 - 332810952 above, 0 below Score 9 : -1073744840 - 332810952 above, 0 below Score 10 : 1073838584 - 332810952 above, 0 below Score 11 : 1073835736 - 332810952 above, 0 below Score 12 : 1 - 332810952 above, 0 below Score 15 : -1073744840 - 332810952 above, 0 below Score 18 : 1073869319 - 332810952 above, 0 below Score 19 : 10 - 332810952 above, 0 below Score 20 : 1073835736 - 332810952 above, 0 below Score 21 : 3 - 332810952 above, 0 below Score 23 : 134513097 - 332810952 above, 0 below Score 24 : 1073835736 - 332810952 above, 0 below Score 25 : 1073838584 - 332810952 above, 0 below Score 26 : 512 - 332810952 above, 0 below Score 27 : 1179403647 - 332810952 above, 0 below Score 28 : 65793 - 332810952 above, 0 below Score 31 : 196611 - 332810952 above, 0 below Score 32 : 1 - 332810952 above, 0 below Score 33 : 89840 - 332810952 above, 0 below Score 34 : 52 - 332810952 above, 0 below Score 35 : 1242552 - 332810952 above, 0 below Score 37 : 2097204 - 332810952 above, 0 below Score 38 : 2621448 - 332810952 above, 0 below Score 39 : 3538999 - 332810952 above, 0 below Score 40 : 6 - 332810952 above, 0 below Score 41 : 52 - 332810952 above, 0 below Score 42 : 52 - 332810952 above, 0 below Score 43 : 52 - 332810952 above, 0 below Score 44 : 256 - 332810952 above, 0 below Score 45 : 256 - 332810952 above, 0 below Score 46 : 5 - 332810952 above, 0 below Score 47 : 4 - 332810952 above, 0 below Score 48 : 3 - 332810952 above, 0 below Score 49 : 1205160 - 332810952 above, 0 below Score 50 : 1205160 - 332810952 above, 0 below Score 51 : 1205160 - 332810952 above, 0 below Score 52 : 19 - 332810952 above, 0 below Score 53 : 19 - 332810952 above, 0 below Score 54 : 4 - 332810952 above, 0 below Score 55 : 1 - 332810952 above, 0 below Score 56 : 1 - 332810952 above, 0 below Score 60 : 1209832 - 332810952 above, 0 below Score 61 : 1209832 - 332810952 above, 0 below Score 62 : 5 - 332810952 above, 0 below Score 63 : 4096 - 332810952 above, 0 below Score 64 : 1 - 332810952 above, 0 below Score 65 : 1209856 - 332810952 above, 0 below Score 66 : 1213952 - 332810952 above, 0 below Score 67 : 1213952 - 332810952 above, 0 below Score 68 : 30120 - 332810952 above, 0 below Score 69 : 40964 - 332810952 above, 0 below Score 70 : 6 - 332810952 above, 0 below Score 71 : 4096 - 332810952 above, 0 below Score 72 : 2 - 332810952 above, 0 below Score 73 : 1238992 - 332810952 above, 0 below Score 74 : 1243088 - 332810952 above, 0 below Score 75 : 1243088 - 332810952 above, 0 below Score 76 : 216 - 332810952 above, 0 below Score 77 : 216 - 332810952 above, 0 below Score 78 : 6 - 332810952 above, 0 below Score 79 : 4 - 332810952 above, 0 below Score 80 : 4 - 332810952 above, 0 below Score 81 : 308 - 332810952 above, 0 below Score 82 : 308 - 332810952 above, 0 below Score 83 : 308 - 332810952 above, 0 below Score 84 : 32 - 332810952 above, 0 below Score 85 : 32 - 332810952 above, 0 below Score 86 : 4 - 332810952 above, 0 below Score 87 : 4 - 332810952 above, 0 below Score 88 : 1685382480 - 332810952 above, 0 below Score 89 : 1205180 - 332810952 above, 0 below Score 90 : 1205180 - 332810952 above, 0 below Score 91 : 1205180 - 332810952 above, 0 below Score 92 : 4652 - 332810952 above, 0 below Score 93 : 4652 - 332810952 above, 0 below Score 94 : 4 - 332810952 above, 0 below Score 95 : 4 - 332810952 above, 0 below Score 96 : 1685382481 - 332810952 above, 0 below


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.
 
Old 04-21-2011, 08:12 AM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Use [code] blocks for the big block of text.

Is the problem fixed? I don't understand.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault zkhuhr Programming 2 08-21-2008 06:34 AM
segmentation fault simasimon Slackware 4 05-09-2006 08:09 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault alnreddy Linux - Software 1 11-05-2005 08:54 PM
Segmentation fault? JiggaJerry Fedora 10 01-16-2005 04:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration