LinuxQuestions.org
Help answer threads with 0 replies.
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-23-2011, 11:14 PM   #31
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Original Poster
Rep: Reputation: 0

Ok I need help. My program compiles and reads in from the datafile. The problem is that the input turns out as this:

Code:
Student     |Exam  0 |Exam  1 |Exam  2 |Exam  3 | Std Avg
Student 5    |     0     1     2     3%6
Student 1   |   101  |   100  |   100  |   100  |   100
Student 2   |    91  |    90  |   100  |    90  |   192
Student 3   |    81  |    50  |    60  |    80  |   260
Student 4   |   101  |    50  |    30  |   100  |   330
Exam Avg    |
   422   |   422   |   422   |   422   |   495   |   495   |   495   |   495   |                                                                                                                        567   |   567   |   567   |   567   |   660   |   660   |   660   |   660   |                                                                                                                     Exam Median |   100  |   100  |   100  |   100  |    90  |    90  |    90  |                                                                                                                         90  |   100  |   100  |   100  |   100  |   100  |   100  |   100  |   100  |
Here's the code I am using
Code:
#include <stdio.h>
#include "constants.h"

extern void bubblesort(int[], int);

int main()
{

	int i,j;
	int above, below;
	int scores[NUMBERofSTUDENTS][EXAMSCORES];
	int grade[NUMBERofSTUDENTS],counts[101];
	int readValues[NUMBERofSTUDENTS][EXAMSCORES];
	int median[EXAMSCORES],meanExam[EXAMSCORES],meanStudent[NUMBERofSTUDENTS];
	int sum=0;
	
	for ( i = 0; i < NUMBERofSTUDENTS; i++)    
	{
		for(j=0; j<EXAMSCORES; j++)
		{
			scanf("%d",&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("Student     |");
		for(i=0; i<EXAMSCORES; i++)
		{
			printf("Exam  %-2d|",i);
		}
		printf(" Std Avg\n");
		printf("Student %d    |",j+1);

		for(i=0;i<EXAMSCORES; i++)
		{
		printf("%6d",i);
		}
		printf("%6\n",meanExam[i]);

	for(i=0;i<NUMBERofSTUDENTS;i++)
	{
		printf("Student %-4d|",i+1);
		for(j=0;j<EXAMSCORES;j++)
		{
			printf("%6d  |",scores[i][j]);
		}
		printf("%6d\n",meanStudent[i]);
	}

	printf("Exam Avg    |\n");
	for(i=0;i<EXAMSCORES;i++)
	{
		for(j=0;j<NUMBERofSTUDENTS;j++)
		printf("%6d   |", meanExam[i]);
	}

	printf("Exam Median |");
	for(i=0;i<EXAMSCORES;i++)
	{
		for(j=0;j<NUMBERofSTUDENTS;j++)
		printf("%6d  |", median[i]);
	}
}
Help?
 
Old 04-24-2011, 06:44 AM   #32
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Look here:
Code:
printf("%6\n",meanExam[i]);
What's missing from the format statement?
 
Old 04-24-2011, 07:30 AM   #33
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
Help?
You seem to have ignored most of the help you already received in this thread.

In the first post you had
Quote:
counts[scores[j][i]]++;
Initially that was the line that was crashing. In post 9, that crash was explained, and the explanation clearly indicated a problem elsewhere, so no reason to change the crashing line. But now you have
Quote:
counts[scores[j][i]++];
What did you expect to accomplish with that change?

Earlier I wrote

Quote:
Originally Posted by johnsfine View Post
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.
You made no change for that problem.

I also wrote.
Quote:
Originally Posted by johnsfine View Post
take a closer look at all your uses of the variable sum. You are not doing what you intend.
You don't seem to have tried to fix that problem.

Since the code you posted doesn't have balancing braces (and so won't compile) I don't know how to relate it to the output you posted from some version that obviously did compile. But both the code and the output show the consequences of what you did to the loop starting at line 51.

I assume some editing accident removed the end of that loop and made unrelated following loops part of it. Then you adjusted the indenting to be consistent with the braces to make it seem like those other loops were intended to be nested. But look at the loops you nested there. Obviously nesting them was not correct.

Last edited by johnsfine; 04-24-2011 at 07:50 AM.
 
Old 04-24-2011, 08:07 AM   #34
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Maybe you should read some online tutorials about pointers and arrays in C and play around with that, and then get back to this project?
 
Old 04-24-2011, 02:13 PM   #35
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MrxNine View Post
... Help?
Copy-paste here the command line you've used for compilation and screen output produced by it.
 
Old 04-24-2011, 05:06 PM   #36
MrxNine
LQ Newbie
 
Registered: Apr 2011
Posts: 17

Original Poster
Rep: Reputation: 0
I'm glad I can finally say I finished it.

Code:
#include <stdio.h>
#include "constants.h"

extern void bubblesort(int[], int);

int main()
{

	int i,j;
	int above, below, exam;
	int scores[NUMBERofSTUDENTS][EXAMSCORES];
	int grade[NUMBERofSTUDENTS],counts[101];
	int median[EXAMSCORES],meanExam[EXAMSCORES],meanStudent[NUMBERofSTUDENTS];
	int sum=0;
	
	for (i =0;i<NUMBERofSTUDENTS;i++)    
	{
		for(j=0; j<EXAMSCORES; j++)
		{
			scanf("%d",&scores[i][j]);	
		}
        }
	
   	for(i=0;i<EXAMSCORES;i++)
	{
		for(j=0;j<NUMBERofSTUDENTS;j++)
		{
			grade[j] = scores[j][i];
		}
		bubblesort(grade,NUMBERofSTUDENTS);
		if(NUMBERofSTUDENTS%2==0)
			median[i] = (grade[NUMBERofSTUDENTS/2] + grade[NUMBERofSTUDENTS/2-1])/2;
		else
			median[i] = grade[NUMBERofSTUDENTS/2];
	}

	for(i=0; i<NUMBERofSTUDENTS; i++)
	{
		sum = 0;
		for(j=0; j<EXAMSCORES; j++)
		{
			sum+=scores[i][j];
		}
		meanStudent[i]= sum/EXAMSCORES;
	}
	for(i=0; i<EXAMSCORES; i++)
	{
		sum = 0;
		for(j=0; j<NUMBERofSTUDENTS; j++)
		{
			sum+=scores[j][i];
		}
		meanExam[i]= sum/NUMBERofSTUDENTS;
	} 
	
	
		printf("Student     |");
		for(i=0; i<EXAMSCORES; i++)
		{
			printf(" Exam  %-2d|",i+1);
		}
		printf(" Std Avg\n");

	for(i=0;i<NUMBERofSTUDENTS;i++)
	{
		printf("Student %-4d|",i+1);
		for(j=0;j<EXAMSCORES;j++)
		{
			printf("%6d   |",scores[i][j]);
		}
		printf("%6d\n",meanStudent[i]);
	}
	
	printf("\n");
	printf("Exam Avg    |");
	for(i=0;i<EXAMSCORES;i++)
	{
		printf("%6d   |", meanExam[i]);
	}
	printf("\n");

	printf("Exam Median |");
	for(i=0;i<EXAMSCORES;i++)
	{
		printf("%6d   |", median[i]);
	}
	printf("\n");
	printf("\n");
	
	printf("EXAM GRADE DISTRIBUTION:\n");
	for(exam=0;exam<EXAMSCORES;exam++)
	{
	for(i=0;i<101;i++)
        {
                counts[i]=0;
	 }	
		above=0;
		for(j=0; j<NUMBERofSTUDENTS; j++)
		{
			counts[scores[j][exam]]++;
		}

                printf("Exam %d:\n",exam+1);

		//above=0;		
		for(i=100; i>=0; i--)
		{
			if(counts[i]!=0)
			{	
				below = NUMBERofSTUDENTS-counts[i]-above;
				printf("Score %3d : %d - %d above, %d below\n",i,counts[i],above,below);
			}
			above+=counts[i];
		}
		printf("\n");
	}
}
Output:
Code:
Student     | Exam  1 | Exam  2 | Exam  3 | Std Avg
Student 1   |   100   |   100   |     5   |    68
Student 2   |   100   |    90   |    60   |    83
Student 3   |    90   |    90   |   100   |    93
Student 4   |   100   |   100   |   100   |   100
Student 5   |   100   |    47   |    22   |    56
Student 6   |   100   |    50   |    14   |    54

Exam Avg    |    98   |    79   |    50   |
Exam Median |   100   |    90   |    41   |

EXAM GRADE DISTRIBUTION:
Exam 1:
Score 100 : 5 - 0 above, 1 below
Score  90 : 1 - 5 above, 0 below

Exam 2:
Score 100 : 2 - 0 above, 4 below
Score  90 : 2 - 2 above, 2 below
Score  50 : 1 - 4 above, 1 below
Score  47 : 1 - 5 above, 0 below

Exam 3:
Score 100 : 2 - 0 above, 4 below
Score  60 : 1 - 2 above, 3 below
Score  22 : 1 - 3 above, 2 below
Score  14 : 1 - 4 above, 1 below
Score   5 : 1 - 5 above, 0 below
Thanks for those who helped me!
 
Old 04-24-2011, 11:32 PM   #37
GVrooman
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 45

Rep: Reputation: 11
A nice job of perservering through adversity. A+
 
  


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 02:41 PM.

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