Introduction to C Programming – COP 3223 Objectives 1. To learn how to use array
ID: 3803647 • 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
1. Use of arrays to store and retreive data
Arrays are one of the simplest data structures and are very easy to traverse ,search,sort etc. An array stores a list of a finite number(n)of similar(homogeneous) data elements. The number n is called lenght or size or range of an array. When upper bound and lower bound of an array are givenits size is calculated as:
Array size(length)=UB-LB+1
For example if an arrya has elements numbered as -6,-5.....0,1,2...10, then its UB is 10 and LB is -6 then array length is
=10-(-6)+1
=17
Arrays are of diffrent types :1) one-dimensional arrays 2)multi-dimesional arrays.
Here we will discuss how to use arrays to store and retrieve data for that i am considering one dimesional array as example(we can use both types).
So for that we will take a example for insertion in array in C language. Through this example we will be able to understand how data is stored and retreived by using array.
#include<stdio.h>
void main()
{
int arr[30]; item, num, i, index;
printf(" Enter no of elements you want to create array with :");
scanf("%d", &num);
printf("/n Enter array elements :");
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
printf(" Enter the element to be inserted :");
scanf("%d", &item);
printf(" Enter the location :");
scanf("%d", &index);
for (i = num; i >= index; i--)
{
arr[i] = arr[i - 1];
}
num++;
arr[index - 1] = element;
//Print the result of insertion
printf(" After insertion elements of array are :");
for (i = 0; i < num; i++)
{
printf("n %d", arr[i]);
}
getch();
}
Output of the program
Enter number of elements you want to create array with :7
Enter array elements :
1 2 4 5 6 7
...................................................................................................................................................
Enter the element to be inserted : 9
Enter the location :2
After insertion elements of array are :
1 9 2 4 5 6 7
In the above example their are 3 for loops for diffrentiation i have converted first one as italic and third one as bold its because first loops shows how data is stored i.e the elements are inserted in array and the third one show the retrival of data from array. We will discuss this in detail here:
In memory, 1D arrays are implemented by allocating a sequence of addressed locations so as to accomodate all its elements. The starting address of the very first element of the array is called its base address. The elemets of address are given contiguous memory locations.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.