Simple C Program: Seneca GPA Calculator


This program was written by me in September 2010.

The idea of this program is very simple:
– You choose menu option
– Enter percentage either letter grades
– It transfers your percentage into letter grade or calculates your GPA
– When you want to quit, you use “q”

Very simple, but also very useful program. All validation is made. (The source code is in the link below)
If you are having problems with compiling and running please see the post below, that instructs how to compile and run programs in different Operating Systems.

Please feel free to copy my source code, I have no problems with that.

Click here to see a screenshot of program in Terminal (Linux Ubuntu OS)

Here is a source code:


/*	 			GPA Calculator Script.

     			>>>>>>>>>>>How it works ?<<<<<<<<<<<<<<

  	GPA CALCULATIN is based on table from : http://www.senecac.on.ca/registrar/records/gpa.html 
	LETTER GRADE CONVERTER are based on table from : http://people.senecac.on.ca/lucie.dutfield/GPA.htm

		GPA Calculator:
		1.You enter number of subjects you need to calculate gpa for
		2.You enter marks for your subjects. (Mark can include "+")
		3.GPA claculator uses formula:
		 Grade Point Average = Total Grade Points Earned / Total Units of Credits. 
		4. If GPA is bigger than 3.55 you receive Honour or if more than 3.95 High Honour
		
		Letter grade converter:
		 You enter the percentage, script shows you letter grade

							Created by Anatoly Spektor
	 
													Have fun! */

 
#include <stdio.h>
#include <ctype.h>
#define SIZE 3


/* Function prototypes */
void percent_to_letter(void);
void gpa_calc(void);
void clear_buffer(void);
int get_an_int(void);
float get_float(void);
int yesorno (void);
int validate_mark(char mark[]);

 /* Main Menu */
  char display_menu () {

	char choice = 'o';
		printf("\n\t-------------------------------------\n");	
	    	printf("\t   SENECA COLLEGE GPA CALCULATOR");	
		printf("\n\t--------------------------------------\n");	
		printf("\n\t Please choose one of the following options:\n");
		printf("\n\t\t[a] Convert percentage into letter grade");
		printf("\n\t\t[b] Calculate GPA");
		printf("\n\t\t[q] Quit Program"); 
		
		/* Choose between GPA calculator or percentage to grade converter */
				       
		 do {
			printf("\n\n\n\t * Enter letter [a][b] or [q]: ");			
			choice=getchar();
			clear_buffer();				
			choice=tolower(choice);
				if ((choice != 'a') && (choice != 'b')&& (choice != 'q'))
				   printf("\n\t >>> Only three options available: [a][b] and [q].\n");				
		}while((choice != 'a') && (choice != 'b') && (choice != 'q'));

		printf("\n\t >>> Your option is [%c] \n\n", choice);		
		
		if (choice == 'a') 
				return 2; 
	
			if (choice == 'b')
					return 1;
				else if (choice == 'q'){    /*[q] - Quit option */
					printf("\t*** Thank you for using GPA Calculator! Good bye! ***\n\n");
						return 0;
						EOF;
						
					}
}

/* MAIN function - Switch between GPA and Percent to letter */
 int main (void) {

	   int yn=0, numsub=0, option =-3;;	


		while (option != 0){

		 	 option=display_menu(); /* Calling a menu */
			 switch (option) {
		
				case 1:	   
					do{
						gpa_calc(); /*Calling GPA */
						printf("\n\t* Do you want to try again [yes] [no]: ");
						yn=yesorno();
						clear_buffer();
					}while(yn != 0);
	     				break;
				case 2:			
					do{
						percent_to_letter(); /* Calling Percent to letter converter */
						printf("\n\t* Do you want to check another letter [yes] [no]:  ");
						yn = yesorno(); 
						clear_buffer();	 
					}while (yn != 0);
						 
							    /* Quit option is inside display_menu */
          			}
	}
  return 0;
	
 }

 /* Percent to letter function */
 void percent_to_letter () {
 
	float num;
 		
	do { 
		printf("\n\t* Please enter percentage: ");
 		num=get_float();

			if (num < 0)
				printf("\n\t>>> Input can not contain negative numbers\n");
				else if (num > 100)
					printf("\n\t>>> Max number is 100\n");
	}while((num < 0)|| (num > 100));

		
		if (num <= 54.9) /* Acting on input, based on table from URL listed above */
			printf("\n\t>>> %.1f percent is  letter : F\n\n", num);
		 if ((num >=55)&&(num<60))
				printf("\n\t>>> %.1f percent is  letter : D\n\n", num);
		  if ((num >=60)&&(num<65))
				   printf("\n\t>>> %.1f percent is  letter : C\n\n",num);			
		   if ((num >=65)&&(num<70))
				     printf("\n\t>>> %.1f percent is  letter : C+\n\n",num);	
		    if ((num >=70)&&(num<75))
				      printf("\n\t>>> %.1f percent is  letter : B\n\n",num);
		     if ((num >=75)&&(num<80))
				         printf("\n\t>>> %.1f percent is  letter : B+\n\n",num);	
		      if ((num >=80)&&(num<90))
				            printf("\n\t>>> %.1f percent is  letter : A\n\n",num);
		        if ((num >=90)&&(num<=100))
				               printf("\n\t>>> %.1f percent is  letter : A+\n\n",num);
	
 } 

 void  gpa_calc (void) {

	int  i=0, grade_points =0, val=0;
	int num =0;
	char mark[SIZE]="OO";
	float num_mark=0, u_of_credit=0, gpa_calc =0;

	do{
	    printf("\n\t* Please enter number of subjects [max 10]: ");	/* I assumed 10 subjects would be enough*/		
	    num= get_an_int(); /* To avoid crashes calling function get_an_int instead of basic scanf */
		 if (num > 10) 
		   printf("\n\t>>> Number of subjects should be 10 or less!\n");
		      else if (num <0)
			printf("\n\t>>> Number of subjects can not be negative!\n");				
	}while ((num < 0) || (num >10));
	

	for (i=0; i < num; i++){

	do {
	
		printf("\n\t* Please enter mark for subject number %d: ", i+1);
		scanf("%s",mark);
		clear_buffer();
		mark[0]=toupper(mark[0]);
		val=validate_mark(mark); /* Function needed to read "+" signs, and avoid errors */ 
	}while(val != 1);

	/* Assign number to each letter grade, for claculation that are based on table from URL listed above */

		if(((mark[0] == 'A')&&(mark[1] == '+')) || (mark[0] == 'A')){
			num_mark = 20.0;
			}
			
		  else if ((mark[0] == 'B')&&(mark[1] == '+')){
		  	 num_mark = 17.5;
			  }
		      else if (mark[0] == 'B'){
			   num_mark = 15.0;
			   }
			
		         else if ((mark[0] == 'C')&&(mark[1] == '+')){
			    num_mark = 12.5;
			    }
			
		           else if (mark[0] == 'C'){
			       num_mark = 10.0;		
			        }
			
	  	               else if (mark[0] == 'D'){
			          num_mark = 5.00;
				     }
			
		                   else {
			            num_mark = 0;
				         }
			

			u_of_credit+=num_mark;
			grade_points+=5;	
}
		/* GPA output  and calculations */
		printf("\n\t>>>>>>>>>>>>>>>>>>>>>\n");		
		gpa_calc = u_of_credit/grade_points;
		printf("\n\n\tYour GPA is: %.1f", gpa_calc);
	

		if (gpa_calc > 3.95)
			printf("\n\n\tCongratulations, you will recieve HIGH HONOUR!\n\n");				
			else if(gpa_calc > 3.55)
				printf("\n\n\tCongratulations, you will recieve HONOUR!\n\n");
				printf("\n\n\t This Program  was created by Anatoly Spektor\n");
		printf("\n\t>>>>>>>>>>>>>>>>>>>>>>>>>\n")	;
}

void clear_buffer(void){ /* function needed to read characters carefully */
	while(getchar() != '\n')
	       ;      
}

int get_an_int(void){
	int n,error = 0;
	char junk;

	
	do{
		if( 0 == scanf("%d", &n) || getchar() != '\n'){
			clear_buffer();
			printf("\n\t>>> Error! Input should contain only numbers\n");
			printf("\n\t * Please try again. Enter number of subjects [max 10]: " );
			error = 1;
		} else error = 0;
	}while( error );
	return n;
}

float get_float(void){ /* needed to read floats carefully */
	float n;	
	int error = 0;
	char junk;

	
	do{
		if( 0 == scanf("%f", &n) || getchar() != '\n'){
			clear_buffer();
			printf("\n\t>>> Error! Input should contain only numbers\n");
			printf("\n\t * Please try again. Enter number: " );
			error = 1;
		} else error = 0;
	}while( error );
	return n;
}


int yesorno() {
      int c, rc = -1;
      do {
         c = getchar();
         if ((c == 'y') || (c == 'Y')) {
		c = getchar();
              if ((c == 'e') || (c == 'E')) {
			c = getchar();
              		if ((c == 's') || (c == 'S')) {
                  rc = 1;
         }
     }
   }
         else if ((c == 'n') || (c == 'N')) {
		c = getchar();
              	if ((c == 'o') || (c == 'O')) {
               rc = 0;
         }
     }
         if (rc == -1 && c != -1) {
            printf("\n\t>>> There are only two options [yes] or [no]: ");
            while (c != -1 && c != '\n')
               c = getchar();
         }
      } while (rc == -1 && c != -1);
      return rc;
   }



int validate_mark ( char mark[]){

 int i, val =0;
		
      for (i=0; i<3;i++){

	 if ((mark[0] == 'A') || (mark[0] == 'B') || (mark[0] == 'C') || (mark[0] == 'D') || (mark[0] == 'E') || (mark[0] == 'F')) 
			val=1;
		if ((mark [1] == '+') || (mark [1] == 0))
			val+=val;	
		   if (val == 2)
		  	  return 1;
	   		else {
				printf("\n\t >>> Invalid Letter!\n");
				printf("\n\t >>> Available letters: A-F [including letters with '+' sign])\n"); 
				return 0;
			}
			i++;
	}
}


Advertisements

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module, Red Hat and some other places :) I am an author of the book 'Eclipse Debugging How To', Muay Thai fighter and amateur photographer ;)
This entry was posted in C, Programming Languages. Bookmark the permalink.

13 Responses to Simple C Program: Seneca GPA Calculator

  1. ed says:

    if my gpa formula is GPA = [ ∑( GPi X CHi ) ] ÷ [ ∑CHi ]
    what should I put to change ∑ and calculate the gpa?

    sorry. but I am very new to C.

  2. ed says:

    GPi is grade point obtained for a certain subject and CHi is Credit hour for a certain subject.

  3. nice………….
    I like it.

  4. amimawe says:

    why did you use #include ??
    which syntax use that library?
    does that library available in all c language software?

  5. amimawe says:

    i mean #include .

  6. amimawe says:

    oh shoot, it wont show..im asking about ctype.h part.

  7. Its really nice code. thank u so much

  8. I do believe all the ideas you’ve presented for your post.
    They’re really convincing and will certainly work. Still, the posts are too quick
    for starters. May just you please prolong them a bit from subsequent time?
    Thank you for the post.

  9. Wow, that’s what I was exploring for, what a stuff! existing here at this weblog, thanks admin of this
    web site.

  10. MASAJISTA A DOMICILIO Masaje Ayurvédico (50) ciento diez€.

  11. Hi would you mind letting me know which webhost you’re working with?
    I’ve loaded your blog in 3 completely different browsers and
    I must say this blog loads a lot quicker then most. Can you suggest a good hosting
    provider at a reasonable price? Thank you, I appreciate it!

  12. Kanken says:

    wonderful points altogether, you just won a brand new reader.
    What might you recommend in regards to your post that you
    simply made some days ago? Any positive?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s