Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The objectives for this assignment involve writing functions, declaring a functi

ID: 3759191 • Letter: T

Question

The objectives for this assignment involve writing functions, declaring a function’s parameters, and function call statements. You will demonstrate using at least 2 functions for modules outlined at the end of these instructions

Objectives to be met (Basis for assignment "points"):

1

Demonstrate how to declare function prototypes with parameters for value-returning and void functions.

2

Demonstrate how to call value-returning and void functions and pass arguments to their parameters.

3

Demonstrate how to define functions to solve small, well-defined tasks.

Use the main algorithm and functional decomposition diagram at the end of these instructions to write a program that will complete the objectives and requirements for this assignment. (See Minimum Requirement #2 below)

The program asks the user to enter the final grade and credit hours for each of 4 classes taken during one semester. The program then calculates the semester grade point average, and displays the total grade points, total credit hours, and semester GPA. A student's semester GPA is calculated using total grade points divided by total credit hours. Each letter grade is assigned a grade point value per credit hour as shown here:

Letter Grade

Grade Point Value per Credit Hour

A

4

B

3

C

2

D

1

F

0

And the semester GPA is the total grade points divided by total credit hours (28 / 13), or 2.154.

Minimum requirements for a "letter grade of C" (also see Recommended Improvements):

1

Adhere to the programming style requirements (below) regarding functions outlined in these instructions.

2

Declare prototypes and write definitions for 2 functions that implement the following 2 modules outlined in the Functional Decomposition diagram at the end of these instructions: Calculate GPA, and Print Results.

3

Program statements and comments inside all functions including main( ) should be neatly indented. Nested statement blocks (decisions, loops, etc…) inside functions should be indented again.

4

Demonstrate how to clearly document function definitions, including pre-conditions, post-conditions, and parameters: in, out, in-out. (See Function PPT and classroom demos for examples)

5

Absolutely NO GLOBAL VARIABLES! (no variables defined outside of any function)

6

The two required functions, and any others you decide to implement, must adhere to the original Functional Decomposition with the same number of parameters and return values that are outlined between the modules in that design.

Programming style requirements regarding functions (on this and every assignment):

Always declare prototypes for every function at the top of the program listing before main( )

Always include names (identifiers) in function prototypes for every parameter

Always document and define functions at the bottom of the program listing after main( )

All functions must return only at the end of their statement block; do not use a return statement or exit( ) inside decision or loop statements (or anywhere else) that might cause the function to end early.

Sample Program Interaction with Minimum Requirements:

(See Bb Discussion Board for output requirements with Recommendation #3)

Enter letter grade for class 1: B

Enter credit hours for class 1: 3

Enter letter grade for class 2: D

Enter credit hours for class 2: 3

Enter letter grade for class 3: A

Enter credit hours for class 3: 4

Enter letter grade for class 4: F

Enter credit hours for class 4: 3

Grade Points: 28

Credit Hours: 13

Semester GPA: 2.154

End Program.

Considerations:

Notice the Calculate GPA module as a required function will take 2 (incoming) value parameters and is a value-returning function that will return 1 value using a return statement, and the Print Results module as a required function will take 3 (incoming) value parameters and is a void function that does not return a value.

Make sure the number of function parameters and their data types match in the declaration (prototype) and definition. Make sure the number of arguments passed to each function in the call statements, and the order in which they are passed, matches the corresponding function declaration.

Regarding Required Functions & Recommendation #1: A parameter used only for incoming data should be a value parameter (no & with parameter types). Both functions required to meet these assignment objectives and one of the recommended improvements must be declared using value parameters because those functions are not intended to change the parameter values – they are not used with cin or assigned new values.

Regarding the Function with Recommendation #2: A parameter used only for outgoing data should be a reference parameter (include the & with that parameter). If a parameter is used for both incoming and outgoing data then it still must be a reference parameter. The function required to meet this recommendation must be declared using reference parameters because the function must change the actual arguments through these parameters – they both must be assigned new values.

Recommended "Letter Grade" Improvements:

(1) Include a third value-returning function for the Determine Grade Point Value module and called to get the grade point value for a single letter grade. (HINT: This function is called repeatedly from the part of the program implementing the Get Semester Totals module that continues to input letter grade and credit hours for each class.)

(2) Include a fourth void function to implement the Get Semester Totals module that will return the total semester grade points and the total semester credit hours. This function will contain the loop structure that inputs and accumulates both totals. (HINT: The only variables declared and used in the main module will be gradePoints, creditHours, and gpa!) NOTE: This draws into question whether the two accumulator parameters are out only or in-out; they would be out only if the Get Semester Totals module initializes them to 0; they would be in-out if the main module still initializes them to 0. In either case, they must be reference parameters.

(3) Write a loop inside main() that will allow the user to continue entering 4 grades for more semesters, however it also must accumulate semester grade points and credit hours and display not only results for the new semester but also the cumulative results for all semesters. (See Bb Discussion Board for output requirements.) The cumulative gpa for all semesters is calculated using the cumulative grade points for all semesters divided by the cumulative credit hours for all semesters. (HINT: This does not require additional functions; Call the same Calculate GPA module and same Print Results module when calculating and printing semester grades and then again with cumulative grades – they implement the same algorithm regardless of whether or not the arguments are for semester results or cumulative results!)

.

Additional Requirements:

Include your name, course, section (CSC140-xxx) at the top of your program file as a comment. Compress and submit your project folder as 1 .zip file. (Your .cpp file must be inside the project folder.)

Algorithm, Level 0 (main-level) Solution:

Define and initialize 1 program constant for CLASSES = 4

Define 2 accumulators for total semester grade points and total semester credit hours, both initialized to 0

Define 1 variable for the semester grade point average

Define 2 variables for inputting a letter grade and the credit hours for a class

Define 1 variable for the numeric point value of a letter grade

Define and initialize a class counter

Loop to process input and accumulate totals for number of CLASSES

o Prompt and read input for letter grade

o Prompt and read input for credit hours

o Determine Grade Point Value as a number for the letter grade

A or a: 4 grade points

B or b: 3 grade points

C or c: 2 grade points

D or d: 1 grade point

Otherwise: 0 grade points

o Multiply credit hours from input by grade point value, and add to total for grade points

o Add credit hours from input to total for credit hours

Get Semester Totals

Calculate GPA as total grade point values divided by total credit hours (use "real" division)

Print Results including total grade points, total credit hours, and the grade point average with 3 decimal places

2 Shaded Modules Required: Calculate GPA, Print Results

Recommendation #1: Determine Grade Point Value

Recommendation #2: Get Semester Totals

The objectives for this assignment involve writing functions, declaring a function’s parameters, and function call statements. You will demonstrate using at least 2 functions for modules outlined at the end of these instructions

Explanation / Answer

1)

A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code.

Functions serve two purposes.

They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else',

Second they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.

A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value.

You already have seen couple of built-in functions like printf(); Similar way you can define your own functions in C language.

Consider the following chunk of code

To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets:

curved brackets after the function's name are required. You can pass one or more paramenters to a function as follows:

By default function does not return anything. But you can make a function to return any value as follows:

A return keyword is used to return a value and datatype of the returned value is specified before the name of function. In this case function returns total which is int type. If a function does not return a value then void keyword can be used as return value.

Once you have defined your function you can use it within a program:

Functions and Variables:

Each function behaves the same way as C language standard function main(). So a function will have its own local variables defined. In the above example total variable is local to the function Demo.

A global variable can be accessed in any function in similar way it is accessed in main() function.

Declaration and Definition

When a function is defined at any place in the program then it is called function definition. At the time of definition of a function actual logic is implemented with-in the function.

A function declaration does not have any body and they just have their interfaces.

A function declaration is usually declared at the top of a C source file, or in a separate header file.

A function declaration is sometime called function prototype or function signature. For the aboveDemo() function which returns an integer, and takes two parameters a function declaration will be as follows:

Passing Parameters to a Function

There are two ways to pass parameters to a function:

Pass by Value: mechanism is used when you don't want to change the value of passed paramters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables.

Pass by Reference mechanism is used when you want a function to do the changes in passed parameters and reflect those changes back to the calling function. In this case only addresses of the variables are passed to a function so that function can work directly over the addresses.

Here are two programs to understand the difference: First example is for Pass by value:

Here is the result produced by the above example. Here the values of a and b remain unchanged before calling swap function and after calling swap function.

Following is the example which demonstrate the concept of pass by reference

Here is the result produced by the above example. Here the values of a and b are changes after calling swap function.

2)

   All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below.

Note:

1. Example program for with arguments & with return value:

In this program, integer, array and string are passed as arguments to the function. The return type of this function is “int” and value of the variable “a” is returned from the function. The values for array and string are modified inside the function itself.

Output:

A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code.

Functions serve two purposes.

  • They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else',

  • Second they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.

A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value.

You already have seen couple of built-in functions like printf(); Similar way you can define your own functions in C language.

Consider the following chunk of code

     int total = 10;     printf("Hello World");     total = total + l;  

To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets:

  Demo()  {     int total = 10;     printf("Hello World");     total = total + l;  }  

curved brackets after the function's name are required. You can pass one or more paramenters to a function as follows:

  Demo( int par1, int par2)  {     int total = 10;     printf("Hello World");     total = total + l;  }  

By default function does not return anything. But you can make a function to return any value as follows:

  int Demo( int par1, int par2)  {     int total = 10;     printf("Hello World");     total = total + l;       return total;  }  

A return keyword is used to return a value and datatype of the returned value is specified before the name of function. In this case function returns total which is int type. If a function does not return a value then void keyword can be used as return value.

Once you have defined your function you can use it within a program:

  main()  {    Demo();  }  

Functions and Variables:

Each function behaves the same way as C language standard function main(). So a function will have its own local variables defined. In the above example total variable is local to the function Demo.

A global variable can be accessed in any function in similar way it is accessed in main() function.

Declaration and Definition

When a function is defined at any place in the program then it is called function definition. At the time of definition of a function actual logic is implemented with-in the function.

A function declaration does not have any body and they just have their interfaces.

A function declaration is usually declared at the top of a C source file, or in a separate header file.

A function declaration is sometime called function prototype or function signature. For the aboveDemo() function which returns an integer, and takes two parameters a function declaration will be as follows:

  int Demo( int par1, int par2);  

Passing Parameters to a Function

There are two ways to pass parameters to a function:

  • Pass by Value: mechanism is used when you don't want to change the value of passed paramters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables.

  • Pass by Reference mechanism is used when you want a function to do the changes in passed parameters and reflect those changes back to the calling function. In this case only addresses of the variables are passed to a function so that function can work directly over the addresses.

Here are two programs to understand the difference: First example is for Pass by value:

  #include <stdio.h>    /* function declaration goes here.*/  void swap( int p1, int p2 );    int main()  {     int a = 10;     int b = 20;       printf("Before: Value of a = %d and value of b = %d ", a, b );     swap( a, b );     printf("After: Value of a = %d and value of b = %d ", a, b );  }    void swap( int p1, int p2 )  {      int t;        t = p2;      p2 = p1;      p1 = t;     printf("Value of a (p1) = %d and value of b(p2) = %d ", p1, p2 );  }  

Here is the result produced by the above example. Here the values of a and b remain unchanged before calling swap function and after calling swap function.

  Before: Value of a = 10 and value of b = 20  Value of a (p1) = 20 and value of b(p2) = 10  After: Value of a = 10 and value of b = 20  

Following is the example which demonstrate the concept of pass by reference

  #include <stdio.h>    /* function declaration goes here.*/  void swap( int *p1, int *p2 );    int main()  {     int a = 10;     int b = 20;       printf("Before: Value of a = %d and value of b = %d ", a, b );     swap( &a, &b );     printf("After: Value of a = %d and value of b = %d ", a, b );  }    void swap( int *p1, int *p2 )  {      int t;        t = *p2;      *p2 = *p1;      *p1 = t;     printf("Value of a (p1) = %d and value of b(p2) = %d ", *p1, *p2 );  }  

Here is the result produced by the above example. Here the values of a and b are changes after calling swap function.

  Before: Value of a = 10 and value of b = 20  Value of a (p1) = 20 and value of b(p2) = 10  After: Value of a = 20 and value of b = 10  

2)

   All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below.

  1. C function with arguments (parameters) and with return value
  2. C function with arguments (parameters) and without return value
  3. C function without arguments (parameters) and without return value
  4. C function without arguments (parameters) and with return value

Note:

  • If the return data type of a function is “void”, then, it can’t return any values to the calling function.
  • If the return data type of the function is other than void such as “int, float, double etc”, then, it can return values to the calling function.

1. Example program for with arguments & with return value:

In this program, integer, array and string are passed as arguments to the function. The return type of this function is “int” and value of the variable “a” is returned from the function. The values for array and string are modified inside the function itself.

  #include<stdio.h>  #include<string.h>    int function(int, int[], char[]);    int main()  {        int i, a = 20;        int arr[5] = {10,20,30,40,50};          char str[30] = ""fresh2refresh"";          printf("    ***values before modification*** ");          printf("value of a is %d ",a);            for (i=0;i<5;i++)        {           // Accessing each variable           printf("value of arr[%d] is %d ",i,arr[i]);          }        printf("value of str is %s ",str);           printf("     ***values after modification*** ");           a= function(a, &arr[0], &str[0]);          printf("value of a is %d ",a);            for (i=0;i<5;i++)        {           // Accessing each variable           printf("value of arr[%d] is %d ",i,arr[i]);          }        printf("value of str is %s ",str);           return 0;  }    int function(int a, int *arr, char *str)  {      int i;        a = a+20;      arr[0] = arr[0]+50;      arr[1] = arr[1]+50;      arr[2] = arr[2]+50;      arr[3] = arr[3]+50;      arr[4] = arr[4]+50;      strcpy(str,""modified string"");        return a;    }  

Output:

***values before modification***
value of a is 20
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
value of str is “fresh2refresh”***values after modification***
value of a is 40
value of arr[0] is 60
value of arr[1] is 70
value of arr[2] is 80
value of arr[3] is 90
value of arr[4] is 100
value of str is “modified string”
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote