Learn how to write a program that calculates grade point averages (GPA) using arithmetic operators. Understand how to expand your application's functionality by incorporating conditional statements and crafting your own function.
Key Insights
- The tutorial covers the application of arithmetic operators in creating a program that calculates the GPA for a student in a school using academic quarters and a 4.0 grading scale.
- Four variables need to be defined, each representing an academic quarter, to get the GPA average. These values are then averaged using arithmetic operators.
- The tutorial further enhances the GPA app's functionality by testing various conditions through conditional statements.
- Conditional statements allow the program to print specific feedback based on the GPA values of the academic quarters.
- Additional instructions include writing a function to calculate GPA. This function should accept four input parameters, returning a float.
- The returned value from the function should be assigned to a constant and printed on the screen.
Dive into iOS development with this tutorial that covers calculation of averages, use of conditional statements, and writing your own function for GPA calculation.
This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.
Topics Covered in This IOS Development Tutorial:
Calculating Averages
Exercise Overview
Using arithmetic operators, we can write an app that does simple math calculations. In the Playground, write a program that calculates the grade point average (GPA) for a student in a school that uses academic quarters (four quarters per school year) and a 4.0 grading scale.
Getting GPA Average
You will need to define four variables, one for each academic quarter. Use any values you’d like for the quarters.
Use arithmetic operators to calculate the average of the four academic quarters and set that to a new variable.
Print out the final GPA for the academic year to the results sidebar.
Conditional Statements
Expand the functionality of the GPA app you created by adding conditional statements to test the following conditions:
If the first and second academic quarter GPA values were above or equal to a B (3.0), print the following statement to your results sidebar: “Good work on your first 2 quarters.”
If the first and second academic quarter GPA values weren’t both above a B, then test to see which academic quarter was the problem.
If the first academic quarter was less than a B (3.0), print: “Sorry, your first quarter needs some review.”
If the second quarter was less than a B, print: “Sorry, your second quarter needs some review.”
If the 3rd or 4th academic quarter are equal to a 4.0, print: “Great work on your last half of the year!”
If neither the 3rd or 4th academic quarters are equal to a 4.0, then print: “Work harder next year and get your grades up.”
Make sure to change the values for the four academic quarter variables as needed to fully test all the functionality above.
Writing Your Own Function
Write a function to calculate your grade point average (GPA).
The function should accept four input parameters, a grade point for each quarter, and return a float.
You should assign your returned value to a constant and print it to your screen.