Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > Intro Programming
|
Intro Programming What is a loop? Which language is best for beginners? What is "object oreinted?" All those types of questions and more are welcome here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Intro Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 23rd, 2008, 07:19 AM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default About Functions and refinement in program

I need to have critical comments about the following piece of code. I am learning to write my own functions in C++. Could someone guide me on how to make this program more refined and how can i make practice on making my own functions:

//Function declaration to calculate percentage
float CalcPerc(float,float);

//Function declaration to calculate GPA
 float CalcGPA(float);

//Function declaration to calculate CGPA
float CalcCGPA(float,float,float);

//Function declaration to decide Grade
char CalcGrade(float);

main()
{
      float TotMrks,MrksObt;
      float perc;
      float gpa;
      float gpa1,gpa2,gpa3;
      float cgpa;

      char grade;





      for (int subj=1;subj<=3;subj++)
      {
        cout<<"\nPlease enter the total marks and marks obtained in Subject "<<subj;;

        cout<<"\nTotal Marks: ";
        cin>>TotMrks;

        cout<<"Marks Obtained: ";
        cin>>MrksObt;

        perc = CalcPerc(MrksObt,TotMrks) ;
        cout<<"Percentage: "<<perc<<" %"<<"\n";

      gpa = CalcGPA(perc);
      cout<<"GPA: "<<gpa<<"\n";

           switch (subj)
           {
           case 1:
               gpa1=gpa;
           break;

          case 2:
               gpa2=gpa;
          break;

          default:
               gpa3=gpa;
          break;
          }

      }
      cgpa = CalcCGPA(gpa1,gpa2,gpa3);
      cout<<"\n"<<"CGPA: "<<cgpa<<"\n";

      grade = CalcGrade(cgpa);
      cout<<"Grade: "<<grade<<"\n";
      system("pause");





}


//Function for calculating percentage
float CalcPerc(float MO, float TM)
      {
            return (MO/TM * 100);
      }


//Function for calculating GPA
float CalcGPA(float percentage)
{
    return (percentage/100) * 5;
}


//Function for calculating CGPA
float CalcCGPA(float GPA1,float GPA2,float GPA3)
{
      return (GPA1 + GPA2 + GPA3)/3;
}


//Function for assigning grade
char CalcGrade(float CGPA)
{
     if (CGPA >= 4)
     {
        return 'A';
     }
     else if (CGPA >= 3 && CGPA < 4)
     {
        return 'B';
     }
     else if (CGPA >= 2 && CGPA < 3)
     {
        return 'C';
     }
     else if (CGPA >= 1 && CGPA < 2)
     {
        return 'D';
     }
     else if (CGPA < 1)
     {
        return 'F';
     }
}

MAXOOD!

Life is an endless journey towards perfection
__________________
MAXOOD!

Life is an endless journey towards perfection
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
weird program flow with nested functions zayasv General .NET 1 January 6th, 2006 11:06 AM
weird program flow with nested functions zayasv VB.NET 0 October 26th, 2005 11:17 AM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
Start a program inside another program Silje Classic ASP Professional 1 November 16th, 2004 02:08 AM
Help with Functions megabytes C# 1 August 7th, 2003 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.