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

Objectives 1. To learn how to use arrays to store and retrieve data to help solv

ID: 3802591 • Letter: O

Question

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.

Grading Details

Your programs will be graded upon the following criteria: 1) Your correctness

2) Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 10% or 15% deducted from your grade.

3) Compatibility – You must submit C source files that can be compiled and executed in a standard C Development Environment. If your program does not compile, you will get a sizable deduction from your grade.

Text Input

Output

Explanation / Answer

//program

#include<stdio.h>
#include<string.h>
//max number of mentors
#define MAX 1000
//there are 5 Combat Skills , so declare it as constant
const int COMBAT_SIZE = 5;
//declare function to calculate Ranking for different category
float ranking(int c, int arr[]);
//sort arrays
void sort(int arr1[], float scores[], int id[],int size);

void display(int category[], float scores[], int id[], int c, int n);

int main()
{
   //declare char array to hold combat,agility and stealth category
   int category[MAX];
   //declare array to store mentor scores
   float scores[MAX];
   //declare array of id's
   int id[MAX];
   //declare array to store combat_scores
   int combat_score[COMBAT_SIZE];
   char filename[20];
   //number of metors to read
   int n;
   int i, j;
   int no_of_times = 0;
   //file pointer for file operation
   FILE *fp;
   char c;
   int key = 0, C_executed =0;

   printf("Enter the name of the file: ");
   scanf("%s", filename);
  
   //open file for reading
   fp = fopen(filename, "r");
   if (!fp)
   {
       printf("%s file cannot be opened for reading ", filename);
       return -1;
   }
   //first read number of mentors
   fscanf(fp, "%d", &n);
   for (i = 0; i < n; i++)
   {
       fscanf(fp, "%d %c", &id[i], &c);
       category[i] = c;
          
       //read 5 combat_scores
       for (j = 0; j < COMBAT_SIZE; j++)
       {
           fscanf(fp, "%d", &combat_score[j]);
           //call function to calculate Ranking
          
       }
       scores[i] = ranking(category[i], combat_score);
   }
   //call soring in descending order
   sort(category, scores, id,n);
   //display the results for A,C,C, S, S category
  
   //diisplay results
   for (i = 0; i < n; i++)
   {
       if (no_of_times == 3)
           break;
       if (category[i] == 'A' && key == 0)
       {
           display(category, scores, id, category[i], n);
           no_of_times++;
           key = 'A';
       }
       if (category[i] == 'C' && key == 'A')
       {
          
           display(category, scores, id,'C', n);
           key = 'C';
           no_of_times++;
           //C_executed++;
       }
       if (category[i] == 'S' && key == 'C')
       {
           display(category, scores, id, 'S', n);
           no_of_times++;
       }
       //key++;
   }
  

}

void display(int category[], float scores[], int id[], int c, int n)
{

   int found = 0,i;
   for (i = 0; i < n; i++)
   {
       if (c == category[i])
       switch (c)
       {

           case 'A':
                   if (found == 1)
                       break;
                   printf("%c: %d %.2f ", category[i], id[i], scores[i]);
                   found++;
                   break;
           case 'C':
                   if (found == 2)
                       break;
                   printf("%c: %d %.2f ", category[i], id[i], scores[i]);
                   found++;
                   break;
           case 'S':
                   if (found == 2)
                       break;
                   printf("%c: %d %.2f ", category[i], id[i], scores[i]);
                   found++;
                   break;

           default:
               break;
       }
      

   }

}


void sort(int arr1[], float scores[], int id[],int size)
{
   int i, j, tmp_id;
   char c;
   float tmp;

   for (i = 0; i < size; i++)
   {
       for (j = 0; j < size-i-1; j++)
       {
           if (scores[j] < scores[j + 1])
           {
               tmp = scores[j + 1];
               scores[j + 1] = scores[j];
               scores[j] = tmp;
               c = arr1[j];
               arr1[j] = arr1[j + 1];
               arr1[j + 1] = c;
               tmp_id = id[j];
               id[j] = id[j + 1];
               id[j + 1] = tmp_id;
           }
       }
   }
}

float ranking(int c, int arr[])
{
   float sum= 0;
   switch (c)
   {
   case 'A': //calculate ranking based on given formula
               //(HandCombatPts + DistanceCombatPts*2 + ObservationPts*2 + ConcealPts + AgilityPts*5)/10
       sum += (float)(arr[0] + arr[1] * 2 + arr[2] * 2 + arr[3] + arr[4] * 5) / 10;
       break;
   case 'C':
       //calculate ranking based on given formula
       //(HandCombatPts*5 + DistanceCombatPts*5 + ObservationPts + ConcealPts + AgilityPts*2)/10
       sum += (float)(arr[0] *5+ arr[1] * 5 + arr[2] + arr[3] + arr[4] * 2) / 10;
       break;
   case 'S':
       //calculate ranking based on given formula
       // (HandCombatPts + DistanceCombatPts + ObservationPts*5 + ConcealPts*5 + AgilityPts*2)/10
       sum += (float)(arr[0] + arr[1] + arr[2] * 5 + arr[3] * 5+ arr[4] * 2) / 10;
       break;
   default:
       printf("Invalid category ");
       break;

   }

   return sum;
}

---------------------------------------------------------------------------------------------------------------------------

//output

Enter the name of the file: combat.txt
A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50