Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 68 / 68

[Project] Build a simple grade-tallying program

This lesson covers building a simple grade-tallying program with arrays in Objective-C, with the aim of combining what you have learned so far into a larger program.

Combine the arrays and loops covered so far into a program that totals several people's scores. Totalling and averaging data held in an array is a basic pattern that turns up constantly in practice.

The sample code stores several people's scores in an NSArray called scores and totals them by adding every element into total with for (NSNumber *score in scores). Note (float)total / scores.count, which calculates the average.

A common early stumble is integer division when calculating the average. Dividing two integers discards everything after the decimal point, so an accurate average needs one side cast to float or double.

Classes, categories, error handling — everything covered in these lessons — carries straight over into real iOS development as core Objective-C skills. Well done.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)