need sum help with a function please!!

This is a discussion on need sum help with a function please!! within the C Programming forums, part of the General Programming Boards category; OK, i am very new to programming as a whole infact i have only been doing it for a couple ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    need sum help with a function please!!

    OK, i am very new to programming as a whole infact i have only been doing it for a couple of months i am generally getting on ok with it,altho frustrating at time but i am stuck on something that i have to do for college. I have written a program that calculates two exam results and gives out a total mark and converts the total marks to a grade from A - F, however i have not put it into fuctions. i have tried and tried to separate my programme into functions but i cant seem to get it to work can anybody give me some assistance.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    	int examMark;
    	int assignMark;
    	int totalMark;
    	char grade;
    	
    	{
    	printf("\nEnter exam mark out of 50: ");
    	scanf("%d", &examMark);
    	printf("\nEnter assignment mark out of 50: ");
    	scanf("%d", &assignMark);
    
    	totalMark = examMark + assignMark;
    	if (totalMark >= 39) 
    	{
    		if ((examMark <15) || (assignMark < 15))
    		{
    			totalMark = 39;
    		}
    		else
    		{
    			if (totalMark == 39)
    			{
    				totalMark = 40;
    			}
    		}
    	}
    	if (totalMark >=70)
    	{
    		grade = 'A';
    	}
    	else
    	{
    		if (totalMark >=60)
    		{
    			grade = 'B';
    		}
    		else
    		{
    			if (totalMark >= 50)
    			{
    				grade = 'C';
    			}
    			else
    			{
    				if (totalMark >= 40)
    				{
    					grade = 'D';
    				}
    				else
    				{
    					grade = 'F';
    				}
    			}
    		}
    	}
    	printf("\nExam : %d/50  Assignment : %d/50  Grade : %c\n\n", examMark, assignMark, grade);
    	
    	} 
    
    	return 0;
    }
    any help at all would be much appriciated, cheers james

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,655
    Well you basically look for blocks of code which seem to do a specific task.

    So taking the bit of code which calculates the total...
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    	int examMark;
    	int assignMark;
    	int totalMark;
    	char grade;
    	
    	{
    	printf("\nEnter exam mark out of 50: ");
    	scanf("%d", &examMark);
    	printf("\nEnter assignment mark out of 50: ");
    	scanf("%d", &assignMark);
    
    
    	totalMark = examMark + assignMark;
    	if (totalMark >= 39) 
    	{
    		if ((examMark <15) || (assignMark < 15))
    		{
    			totalMark = 39;
    		}
    		else
    		{
    			if (totalMark == 39)
    			{
    				totalMark = 40;
    			}
    		}
    	}
    
    	if (totalMark >=70)
    	{
    		grade = 'A';
    	}
    	else
    	{
    		if (totalMark >=60)
    		{
    			grade = 'B';
    		}
    		else
    		{
    			if (totalMark >= 50)
    			{
    				grade = 'C';
    			}
    			else
    			{
    				if (totalMark >= 40)
    				{
    					grade = 'D';
    				}
    				else
    				{
    					grade = 'F';
    				}
    			}
    		}
    	}
    	printf("\nExam : %d/50  Assignment : %d/50  Grade : %c\n\n", examMark, assignMark, grade);
    	
    	} 
    
    	return 0;
    }
    Would be something like this
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int calculateTotal ( int examMark, int assignMark );
    
    int main(void)
    {
    	int examMark;
    	int assignMark;
    	int totalMark;
    	char grade;
    	
    	{
    	printf("\nEnter exam mark out of 50: ");
    	scanf("%d", &examMark);
    	printf("\nEnter assignment mark out of 50: ");
    	scanf("%d", &assignMark);
    
    
    	totalMark = calculateTotal( examMark, assignMark );
    
    	if (totalMark >=70)
    	{
    		grade = 'A';
    	}
    	else
    	{
    		if (totalMark >=60)
    		{
    			grade = 'B';
    		}
    		else
    		{
    			if (totalMark >= 50)
    			{
    				grade = 'C';
    			}
    			else
    			{
    				if (totalMark >= 40)
    				{
    					grade = 'D';
    				}
    				else
    				{
    					grade = 'F';
    				}
    			}
    		}
    	}
    	printf("\nExam : %d/50  Assignment : %d/50  Grade : %c\n\n", examMark, assignMark, grade);
    	
    	} 
    
    	return 0;
    }
    
    int calculateTotal ( int examMark, int assignMark ) {
    	int totalMark = examMark + assignMark;
    	if (totalMark >= 39) 
    	{
    		if ((examMark <15) || (assignMark < 15))
    		{
    			totalMark = 39;
    		}
    		else
    		{
    			if (totalMark == 39)
    			{
    				totalMark = 40;
    			}
    		}
    	}
    	return totalMark;
    }
    
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    For example this part
    Code:
    if (totalMark >=70)
    	{
    		grade = 'A';
    	}
    	else
    	{
    		if (totalMark >=60)
    		{
    			grade = 'B';
    		}
    		else
    		{
    			if (totalMark >= 50)
    			{
    				grade = 'C';
    			}
    			else
    			{
    				if (totalMark >= 40)
    				{
    					grade = 'D';
    				}
    				else
    				{
    					grade = 'F';
    				}
    			}
    		}
    	}
    can be easely converted to function
    Code:
    char getGradeFromMark(int totalMark)
    {
       if (totalMark >=70)
       {
    	return 'A';
       }
       if (totalMark >=60)
       {
    	return 'B';
       }
       if (totalMark >= 50)
       {
    	return 'C';
       }
       if (totalMark >= 40)
       {
    	return 'D';
       }
       return 'F';
    }
    Also you can create functions for entering scores, for calculating totalMark and for outputting resulting Grade
    [edit]
    It took me too long
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,655
    Heh - I guess he's got all he needs now that we've done one each
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    Thanx a lot i will go and give it a go and see if i can get it to work, much apprieciated, cheers again

    james

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    Yeah it works like a charm....and wats more i actually understand what uve done which is always helpful for future programs cheers guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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