C++ Lab: Kill to Death Ratios

Sun Oct 12 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

In the heat of battle, performance is more than instinct — it’s measurable. Every victory and defeat contributes to your overall record, and understanding those numbers can reveal a lot about how you play. In this lab, you’ll develop a program that calculates and tracks a player’s kill-to-death ratio (K/D) over time. The program will load existing match data from files, compute averages, and allow the player to add new results, dynamically updating their statistics as the game progresses.

This lab expands on your understanding of file input/output, arrays, and loop control, tying these elements together to manage evolving data. You’ll practice reading from and writing to files, storing results efficiently, and updating data sets as new information is introduced — all while maintaining accuracy and structure.

Why does this matter? Because the ability to process and persist data lies at the heart of nearly every modern application. Whether tracking gameplay performance, analyzing sensor data, or monitoring user activity, programs that can remember and respond to new information are the ones that feel truly interactive. By completing this lab, you’ll gain experience writing software that not only performs calculations, but also adapts and grows as its data changes — a skill essential to real-world programming.


Task

Create a small program that allows the user to select between 3 base files of kills and deaths from a video game, then allows the user to keep adding to the kills and deaths to keep calculating their updated kill to death ratio (k/d ratio).

There are 3 base file options (downloadable from here):

OptionKills FileDeaths File
1kills1.txtdeaths1.txt
2kills2.txtdeaths2.txt
3kills3.txtdeaths3.txt

When each option is selected both respective files should be opened which contain integer numbers (guaranteed to be valid integers -- thus no error checking is needed when reading them in). Each integer represents a kill or death count in a pvp match in a game. Each set of files has the same amount of lines respectively guaranteed.


Steps to Complete the Lab

All input files needed for this lab can be downloaded here.

You will need to:

  1. Get a valid selection from the options listed above from the user. If an invalid option is entered (input failure or invalid selection), output the error "Error: invalid selection\n" until a valid option is chosen.

  2. Once a valid option is chosen open the respective kill and death count files to input file variables.

  3. Read from both files into separate arrays. Since both files contain the same amount of data a count should be kept that is equivalent to the size of items read in at any given time in both arrays. The maximum size of the arrays is 10. If the maximum size of the array is hit when reading from the files output "\nArray full\n" and stop reading any further numbers from both files. Make sure to account for newlines at the end of the input files when reading.

  4. Close the input files when done reading.

  5. Sum the kills and deaths separately then output the average k/d ratio from the initial file read in the form:

"\nFile Average k/d: killAverage : deathAverage\n”
Where killAverage is the average kills in the kills file, and deathAverage is the average deaths in the deaths file. The averages can be calculated by dividing the 2 sums by the count of items read from the file individually.
  1. Prompt the user for a "\nNew kill: " and a "New death: " and read each into an integer variable. Neither inputs need to be error checked at all for this lab. These new inputs should be saved to the end of the respective arrays then the count of items in the arrays incremented.

  2. The new k/d ratios calculated and output in the form:

"\nAverage k/d: killAverage : deathAverage\n”
Where killAverage is the new average kills, and deathAverage is new average deaths. The averages can be calculated in a similar manner as before.
  1. Once the average has been output the user should be asked "\n(c)ontinue (any other key to quit)? " and their selection read into a character. If they enter ’c’ then the program should continue from Step 6. If the user enters anything else the program should stop looping. The user should only be asked if the max array size has not been hit, otherwise "\nArray Full\n" should be output and the program should stop looping.

  2. Once the program has finished the interaction loop the kills and deaths files should be opened as output files (which will erase their contents). The contents of the kills and deaths arrays should then be output to their respective files with each kill and death on a new line in their respective files. The files should be closed once all kills and deaths have been output.


Libraries Permitted

You may only use the following libraries in your program:

  1. iostream

  2. fstream


Criteria for Success

See CodeGrade

Example Program Interaction

An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.

Kills Input File

10 5 7 9

Deaths Input File

9 3 6 5

Terminal

lab 6 % ./a.out K/D File 1/2/3? 2 File Average k/d: 7 : 5 New kill: 8 New death: 5 Average k/d: 7 : 5 (c)ontinue (any other key to quit)? c New kill: 5 New death: 0 Average k/d: 7 : 4 (c)ontinue (any other key to quit)? s lab 6 %

Kills Output File

10 5 7 9 8 5

Deaths Output File

9 3 6 5 5 0

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.