Introduction to C Programming – COP 3223 Objectives 1. To learn how to use array
ID: 3803502 • Letter: I
Question
Introduction to C Programming – COP 3223 Objectives 1. To learn how to use arrays to store and retrieve data to help solving problems. 2. Reinforce use of input files. Introduction: Ninja Academy Ninjas are awesome! Your friend has not stopped talking about how cool ninjas and how they would like to become a ninja. To amuse your friend, you have decided to create a series of programs about ninjas. Problem: Mentorship (ninjamentors.c) It is time for your friend to select their ninja mentors! Ninja students are able to select several mentors from the class of higher level students to learn special skills from. Skills are categorized as Stealth (S), Combat (C), and Agility (A). Your friend will be provided with a file of older students that has their name and rankings for the different skills. They can then choose 5 mentors to learn from. To assist, your program should read in all of the student’s information and print out the two best combat mentors, the two best stealth mentors, and the best agility mentor. If your friend has been a diligent student, they will be able to select these best options! If not, they will need to go down the list and select other mentors. Combat Skills are split into Hand to Hand and Distance. Stealth skills are split into Observation and Concealment. Agility is a singular category. Input File Format The first line of the input file will contain a single integer n (5 n 100), denoting the number of potential mentors, for which information is listed in the file. The following n lines will have all the information for all the mentors with one mentor's information on a single line. Each line will have the following format: ID Category HandCombatPts DistanceCombatPts ObservationPts ConcealPts AgilityPts ID will be a positive integer representing the potential mentor. Category will be a single character, either 'C', 'S' or 'A', for combat, stealth or agility, respectively. HandCombatPts will be an integer representing the number of points that student was given last year by their hand to hand combat instructor. DistanceCombatPts will be an integer representing the number of points that student was given last year by their distance combat instructor. ObservationPts will be an integer representing the number of points that student was given last year by their observation and spying skills instructor. ConcealPts will be an integer representing the number of points that student was given last year by their concealment and disguise instructor. AgilityPts will be an integer representing the number of points that student was given last year by their agility and acrobatics instructor. How to Compute a Ranking For each potential mentor, their ranking will be a summation weighted by their category. If they are a potential combat mentor their ranking should be: (HandCombatPts*5 + DistanceCombatPts*5 + ObservationPts + ConcealPts + AgilityPts*2)/10 If they are a potential stealth mentor their ranking should be: (HandCombatPts + DistanceCombatPts + ObservationPts*5 + ConcealPts*5 + AgilityPts*2)/10 If they are a potential agility mentor their ranking should be: (HandCombatPts + DistanceCombatPts*2 + ObservationPts*2 + ConcealPts + AgilityPts*5)/10 Program Specification You must use arrays to solve the problem. Your program should first prompt the user for the name of the input file. Then, your program should process the input file and write the five best mentors for your friend. Each line should list the category, the ID, and the ranking of the mentor, respectively, separated by spaces. Round the ranking to two decimal places. The mentors must be listed according to category as follows: agility, followed by the two combat, followed by the two stealth. Both the combat and the stealth mentors must be listed in descending order of ranking. Output Sample Sample outputs will be provided on the webcourse. Deliverables One source file: ninjamentors.c for your solution to the given problem submitted over WebCourses. Restrictions Although you may use other compilers, your program must compile and run using Code::Blocks. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem
Input Files
10
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165 6
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48
Output Sample
A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50
Explanation / Answer
// C code
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *inFile;
char filename[100];
// prompt the user for the name of the input file.
printf("Enter file name : ");
scanf("%s", filename);
inFile = fopen(filename, "r");
int size;
// read from the file the total number of is that will be in the file
fscanf(inFile, "%d ", &size);
int id[size];
char category[size];
int handCombatPt[size];
int distCombatPt[size];
int observationPt[size];
int concealPt[size];
int agilityPt[size];
float ranking[size];
// read all the lines in file and fill it in arrays
int i=0;
for (i=0; i<size; i++)
{
fscanf(inFile, "%d %c %d %d %d %d %d", &id[i], &category[i], &handCombatPt[i], &distCombatPt[i], &observationPt[i], &concealPt[i], &agilityPt[i]);
printf("%d %c %d %d %d %d %d ", id[i], category[i], handCombatPt[i], distCombatPt[i], observationPt[i], concealPt[i], agilityPt[i]);
}
// iterate over the arays and determine the ranking
for (i=0; i<size; i++)
{
if (category[i] == 'A')
{
ranking[i] = (handCombatPt[i] + distCombatPt[i]*2 + observationPt[i]*2 + concealPt[i] + agilityPt[i]*5)/10.0;
}
if (category[i] == 'C')
{
ranking[i] = (handCombatPt[i]*5 + distCombatPt[i]*5 + observationPt[i] + concealPt[i] + agilityPt[i]*2)/10.0;
}
if (category[i] == 'S')
{
ranking[i] = (handCombatPt[i] + distCombatPt[i] + observationPt[i]*5 + concealPt[i]*5 + agilityPt[i]*2)/10.0;
}
}
// variables to show 1st 2,nd other randings
int firstA=0, firstS=0, secondS=0, firstC=1, secondC=1;
for (i=0; i<size; i++)
{
// determine the first and second for each category
if (category[i]=='A' && ranking[firstA] < ranking[i])
{
firstA = i;
}
else if (category[i]=='S' && ranking[firstS] < ranking[i])
{
secondS = firstS;
firstS = i;
}
else if (category[i]=='S' && ranking[secondS] < ranking[i])
{
secondS = i;
}
else if (category[i]=='C' && ranking[firstC] < ranking[i])
{
secondC = firstC;
firstC = i;
}
else if (category[i]=='C' && ranking[secondC] < ranking[i])
{
secondC = i;
}
}
printf(" A : %d %0.2f ", id[firstA], ranking[firstA]);
printf("C : %d %0.2f ", id[firstC], ranking[firstC]);
printf("C : %d %0.2f ", id[secondC], ranking[secondC]);
printf("S : %d %0.2f ", id[firstS], ranking[firstS]);
printf("S : %d %0.2f ", id[secondS], ranking[secondS]);
}
/*
output:
Enter file name : input.txt
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165 6
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48
A : 21 1171.00
C : 6 696.70
C : 32 578.00
S : 11 1094.20
S : 19 1046.50
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.