C newb really could use your help!

This is a discussion on C newb really could use your help! within the C Programming forums, part of the General Programming Boards category; Code: Hello all: I am new to C programming and would really appreciate some good advice on the code I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    C newb really could use your help!

    Code:
    Hello all:
    
    I am new to C programming and would really appreciate some good advice on the code I have written on how to get it to work right and would be very great full. I'm getting numerous compiler errors when I remove the remark. If you can help, please do! And point me in the right direction. Thanks!
    
    /* 
    
    #include <stdio.h>
    #include "constants.h"
    
    extern void bubbleSort(int[][],int);
    extern void generateHistogram(int[][], int);
    
    int median (int grades[][], int number);
    double averageStudent(int setOfGrades[][]);
    double averageExam(int setOfGrades[][]);
    double averageofAverage(int averagesofstudents[]);
    
    int main()
    {
    	int classAverage;
    	int median[EXAMSCORES];
    	int score[NUMBERofSTUDENTS][EXAMSCORES];
    	int i;
    	int j;
    	int studentaverage;
    	int studentTotal = 0;
    	int examtotal = 0;
    	//double studentaverage[NUMBERofSTUDENTS];
    	double examaverage[EXAMSCORES];
    	double averagesAverage;
    	
    	
    	for (i=0; i<NUMBERofSTUDENTS; i++) 
    	{ 					
    		for (j = 0; j < EXAMSCORES; j++) 
    		{ 				
    		scanf("%d", score[i][j]);
    		}				
    	} 
    	
     
    	
    	for (i=0; i<EXAMSCORES; i++) 
    	{ 					
    		for (j = 0; j < NUMBERofSTUDENTS; j++) 
    		{ 				
    		printf("%d", score[i][j]);
    		}				
    	}					
    	
    	
    	
    	for (i=0;i<NUMBERofSTUDENTS;i++)
    	{
    		for(j=0; i<EXAMSCORES; j++)
    		{
    		studentTotal = studentTotal+(score[i][j]);
    		
    		}
    		studentaverage= studentTotal/4;
    		printf("Average for student %d", i);
    		printf("is %.2f", studentaverage);
    		studentTotal = 0;
    
    	}
                  
    		
               
     	
    	for (j=0;j<EXAMSCORES;j++)
    	{
    		for (i=0;i<NUMBERofSTUDENTS;i++)
    		{
    		examtotal = examtotal + score[i][j];
    		}
    		printf("Average for exam %d", j+1);
    		printf("is %.2f", examtotal/NUMBERofSTUDENTS);	
    		examtotal = 0;
    	}
    
    	 
    	// averagesAverage =  averageofAverage (studentaverage[i]); 
    	printf("The average of the students averages is %.2f\n", averagesAverage);
    
    	return 0; /* end main */
    }
    
    /*
    double averageStudent(int setOfGrades[num][exam])
    {
    	int i; 
    	int j; 
    	int total=0;
    	double avg;
    	int num;
    	int exam;
    
    	for (j=0; j<exam; j++)
    	{
    	total = total + setOfGrades[i][j]; 
    	}
    	
    	avg = total/exam;
    	
    	return avg;
    }
    */
    
    /*
    double averageExam (int setOfGrades[num][exam])
    {
    	int i; 
    	int j; 
    	int total=0;
    	double avgExam;
    	int num;
    	int exam;
    
    	for (i=0; i<num; i++) 
    	{
    	total=total + setOfGrades[i][j]; 
    	}
    	
    	avgExam = total/num;
    
    	return avgExam;
    }
    */
    
    /*
    double averageofAverage (int averagesofstudents[num])
    {
    	int i;
    	int total=0;
    	double avgAvg;
    	int num;
    
    	for (i=0; i<num; i++)
    	{
    	total=total + averageofstudents[i];
    	}
    	
    	avgAvg = total/num;
    
    	return avgAvg;
    */
    Last edited by davidjg; 11-17-2010 at 06:58 PM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    First of all use codetags in your code...

    second, you have many undeclared variables...why? try to fix this first...
    Last edited by brack; 11-17-2010 at 06:55 PM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Your code looks like a mess if you don't use code tags:
    USE CODE TAGS
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    yes but still try to fix some errors by declaring some variables...is it difficult? :/

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Code:
    abcd.c:4: error: ‘num’ undeclared here (not in a function)
    abcd.c:4: error: ‘exam’ undeclared here (not in a function)
    abcd.c:25: error: ‘num’ undeclared here (not in a function)
    abcd.c:25: error: ‘exam’ undeclared here (not in a function)
    abcd.c:46: error: ‘num’ undeclared here (not in a function)
    abcd.c: In function ‘averageofAverage’:
    abcd.c:55: error: ‘averageofstudents’ undeclared (first use in this function)
    abcd.c:55: error: (Each undeclared identifier is reported only once
    abcd.c:55: error: for each function it appears in.)
    abcd.c: In function ‘main’:
    abcd.c:64: error: ‘EXAMSCORES’ undeclared (first use in this function)
    abcd.c:65: error: ‘NUMBERofSTUDENTS’ undeclared (first use in this function)
    got it?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    mmm...i don' t see the reason you have all thease functions since you don' t call no one.... :/

    i mean that in main:

    you read the score.
    you print the score
    you calculate the total score.
    you calculate the average
    you print it...
    and that is all... you don' t use any function...
    Last edited by brack; 11-17-2010 at 07:39 PM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    also you can have many variables in one type...try this...
    eg if you have
    Code:
    int a;
    int b;
    int c;
    int d;
    you can write simply:
    Code:
    int a,b,c,d;
    better eh?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Code:
    Here are the program requirements and what it should do.
    
    Your program will read in a number of student grade records from a file (stdin), with each grade record including (potentially) multiple exam scores. So, for example, you might read data for 200 students that include scores for each of 4 different exams. The exact number of students and exams will be specified in a constants.h file that I will provide. For example, the constants.h file might look like
    
    #define NUMBERofSTUDENTS 200
    #define EXAMSCORES 4
    
    Your program would then read the 800 exam scores and store them in a two-dimensional array, declared as
    
    int scores [NUMBERofSTUDENTS][EXAMSCORES];
    
    I don't care what variable name you use for your array. I used scores here, but that is up to you. However, you WILL need to use the names NUMBERofSTUDENTS and EXAMSCORES as those will be names I'll use in constants.h.
    
    Here is what your program will do:
    
    
    Calculate and print the "class" average for each set of exams (So, for our "example" of four exam scores for each student, you'd print 4 different class averages, one for each exam.)
    Calculate and print the median exam score for each set of exams.
    Print a histogram of the scores found for each set of exams.
    Calculate and print the average of all exam scores for each student record (So, again for our example, the average of the 4 exams for each student.)
    Calculate and print the average of the averages of the students (so the average for all 800 exam scores in our example.)
    To do some of these things, you'll need to sort the individual (4?) exam scores, for example to find the median and to print the histogram. You can use the bubble sort function that I've written for class. You can even use the histogram function that I've written for class. Bear two things in mind, however, namely
    
    Both the bubble and histogram function(s) are written to work on a single-dimension array. You'll have to determine how to pass a one-dimensional array for each group of exam scores.
    You cannot change the order of the exam scores in any column of your two-dimensional array since that would have the effect of changing the scores (and averages) for each student. 
    Luckily, each of those concerns can be addressed easily using techniques we'll discuss in class.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    it is the same code you posted but i just putted the functions above. now,
    for example there is an error (num undeclared) in the red mark. why? because it is a parameter...it must be declared inside the function that calls this function, main()! But, even if you declare this there will be again error cause you don' t call "averageStudent" from main()!!!!!!! think about this...you MUST call the functions...they do not work by themselves...

    Code:
    #include <stdio.h>
    #include "constants.h"
    
    double averageStudent(int setOfGrades[num][exam])
    {
    	int i; 
    	int j; 
    	int total=0;
    	double avg;
    	int num;
    	int exam;
    
    	for (j=0; j<exam; j++)
    	{
    	total = total + setOfGrades[i][j]; 
    	}
    	
    	avg = total/exam;
    	
    	return avg;
    }
    
    
    
    double averageExam (int setOfGrades[num][exam])
    {
    	int i; 
    	int j; 
    	int total=0;
    	double avgExam;
    	int num;
    	int exam;
    
    	for (i=0; i<num; i++) 
    	{
    	total=total + setOfGrades[i][j]; 
    	}
    	
    	avgExam = total/num;
    
    	return avgExam;
    }
    
    
    
    double averageofAverage (int averagesofstudents[num])
    {
    	int i;
    	int total=0;
    	double avgAvg;
    	int num;
    
    	for (i=0; i<num; i++)
    	{
    	total=total + averageofstudents[i];
    	}
    	
    	avgAvg = total/num;
    
    	return avgAvg;
    }
    
    int main()
    {
    	int classAverage;
    	int median[EXAMSCORES];
    	int score[NUMBERofSTUDENTS][EXAMSCORES];
    	int i;
    	int j;
    	int studentaverage;
    	int studentTotal = 0;
    	int examtotal = 0;
    	//double studentaverage[NUMBERofSTUDENTS];
    	double examaverage[EXAMSCORES];
    	double averagesAverage;
    	
    	
    	for (i=0; i<NUMBERofSTUDENTS; i++) 
    	{ 					
    		for (j = 0; j < EXAMSCORES; j++) 
    		{ 				
    		scanf("%d", score[i][j]);
    		}				
    	} 
    	
     
    	
    	for (i=0; i<EXAMSCORES; i++) 
    	{ 					
    		for (j = 0; j < NUMBERofSTUDENTS; j++) 
    		{ 				
    		printf("%d", score[i][j]);
    		}				
    	}					
    	
    	
    	
    	for (i=0;i<NUMBERofSTUDENTS;i++)
    	{
    		for(j=0; i<EXAMSCORES; j++)
    		{
    		studentTotal = studentTotal+(score[i][j]);
    		
    		}
    		studentaverage= studentTotal/4;
    		printf("Average for student %d", i);
    		printf("is %.2f", studentaverage);
    		studentTotal = 0;
    
    	}
                  
    		
               
     	
    	for (j=0;j<EXAMSCORES;j++)
    	{
    		for (i=0;i<NUMBERofSTUDENTS;i++)
    		{
    		examtotal = examtotal + score[i][j];
    		}
    		printf("Average for exam %d", j+1);
    		printf("is %.2f", examtotal/NUMBERofSTUDENTS);	
    		examtotal = 0;
    	}
    
    	 
    	// averagesAverage =  averageofAverage (studentaverage[i]); 
    	printf("The average of the students averages is %.2f\n", averagesAverage);
    
    	return 0; /* end main */
    }

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    have you tried to write a simply program to understand how to use functions ?

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Yes but I'm very new to C programming and I do appreciate all of your help!

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    This is really giving me a hard time. What should I do with it?


    averagesAverage = averageofAverage (studentaverage[i]);
    printf("The average of the students averages is %.2f\n", averagesAverage);



    gradesum.c: In function `main':
    gradesum.c:87: subscripted value is neither array nor pointer
    gradesum.c:87: called object is not a function

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    you should pass two things in the function...
    first the array...eg int A[]
    and second the array size.

    and NOT pass the arguments like this: int A[SIZE] cause this is something like passing only the last element!

    Code:
    double averageofAverage (int averagesofstudents[num]) // (int averagesofstudents[], int size)
    {
    	int i;
    	int total=0;
    	double avgAvg;
    	int num;
    
    	for (i=0; i<num; i++)
    	{
    	total=total + averageofstudents[i];
    	}
    	
    	avgAvg = total/num;
    
    	return avgAvg;
    }

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    take a look at an example...i have not compile this..but i think it is correct...let me know..

    Code:
    #include <stdio.h>
    #define size 5
    
    double avg(int score[][], int n){
    	int i, j;
    	float total = 0; // this MUST be float! think about this...
    		for (i=0; i<n; i++){
    			for(j=0; j<n; j++){
    				total = total + score[i][j];
    			}
    		}
    return total; // return total after the 2 for loops have ended so this is the average of all!
    }
    
    int main(){
    	int i, j;
    	int score[size][size];
    	float avgr;
    		
    		// reads the 2-d array score!
    		for (i=0; i<size; i++){
    			for(j=0; j<size; j++){
    				scanf("%d", &score[i][j]);				
    			}
    		}
    		
    		// prints the 2-d array score!
    		for (i=0; i<size; i++){
    			for (j=0; j <size; j++){
    				printf("%d", score[i][j]);
    			}				
    		}					
    		
    		// then i call a function that returns avg!
    		// what shall i have as a parameter? the array and its size...
    		avgr = average(score, size); // stores the avg in the "avgr" variable
    		printf("average is: %f\n", avgr);
    return 0;
    }
    Last edited by brack; 11-17-2010 at 09:19 PM.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    and line 9 is?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 09:27 PM
  2. newb question
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2008, 09:44 AM
  3. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. Detective 1: Who do you think killed him? Detective 2: google it newb.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 10-09-2004, 03:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21