Question

Introduction to C Programming – COP 3223 1. To learn how to use arrays to store...

Introduction to C Programming – COP 3223

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 in/

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

/Text out/

A: 21 1171.00

C: 6 696.70

C: 32 578.00

S: 11 1094.20

S: 19 1046.50

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screenshot:

# include<stdio. h> #include-string.h> //max number of mentors #de fine MAX 1000 //there are 5 Combat Skills, so declare it afscanf (fp, %d, &combat score [j ] ) ; //call function to calculate Ranking scores i] -ranking (category[i], combat score);for (i = 0; 1 < size; 1++) for (i 0: i < size-1-1: j++) if (scores [j] <scores[j + 1] ) tmp scores [j + 1] scores [j + 1] = scaseA //calculate ranking based on given formula // (HandCombatPts DistanceCombatPts*2 + ObservationPts*2+ // ConcealPts + Ag

Input file: combat.txt

10 14 A 447 252 68 34 978 2 C 230 299 597 180 9 27 A 318 220 97 28 1317 32 С 563 450 547 112 28 8 С 669 260 200 36 171 11 S17

Sample output:

Console program output Enter the name of the file combat txt A: 21 1171.00 C: 6 696.00 C: 32 578.00 S: 11 1094.00 S: 19 1046.

Code to copy:

#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\n", 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\n", category[i], id[i], scores[i]);
found++;
break;
case 'C':
if (found == 2)
break;
printf("%c: %d %.2f\n", category[i], id[i], scores[i]);
found++;
break;
case 'S':
if (found == 2)
break;
printf("%c: %d %.2f\n", 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 += (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 += (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 += (arr[0] + arr[1] + arr[2] * 5 + arr[3] * 5+ arr[4] * 2) / 10;
break;
default:
printf("Invalid category\n");
break;

}

return sum;
}

Add a comment
Know the answer?
Add Answer to:
Introduction to C Programming – COP 3223 1. To learn how to use arrays to store...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 4: C Programming (15 points) Create a program that asks the user for an integer...

    Question 4: C Programming (15 points) Create a program that asks the user for an integer number representing the number of students in a classroom. Your program will then malloc an array of struct students having the same number of cells as input by the user's integer number. The student struct will have only two fields: student name and gpa. Your program then opens a CSV file

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile...

    Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting rules we discussed in class. Please see the descriptive ile on eLearning for details. Program 1- Linear Search Algorithm In Computer Science, it is often very important to be able to locate a specific data item inside a list or collection of data. Algorithms that perform...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • C++ Programming Programming Project Outcomes:  Understand and use C++ functions for procedural abstraction and Functional...

    C++ Programming Programming Project Outcomes:  Understand and use C++ functions for procedural abstraction and Functional Decomposition  Develop some functions for generating numerical data from numerical inputs  Understand and utilize file input and file output to perform computing tasks Requirements: 1. You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc. 2. The program should prompt the user...

  • C LANGUAGE!!! The program uses both files and two dimensional arrays. The Problem Statement Business is...

    C LANGUAGE!!! The program uses both files and two dimensional arrays. The Problem Statement Business is going well for your friend, who is selling discounts to area clubs and restaurants, which means that business is going well for you! Both of you have decided that you'll advertise on memory mall, which is roughly arranged as a rectangle. There's not enough time before a football game to hand flyers to everyone arranged on the mall. Therefore, you will only be able...

  • programming language is C++. Please also provide an explanation of how to create a file with...

    programming language is C++. Please also provide an explanation of how to create a file with the given information and how to use that file in the program Let's consider a file with the following student information: John Smith 99.0 Sarah Johnson 85.0 Jim Robinson 70.0 Mary Anderson 100.0 Michael Jackson 92.0 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT