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

Assignment #8 Introduction to C Programming – COP 3223 Objectives To learn how t

ID: 3692993 • Letter: A

Question

Assignment #8

Introduction to C Programming – COP 3223

Objectives

To learn how to design and implement functions for program development

To reinforce how to use pass by value and pass by reference variables

To learn how to use structures

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: Forming an Alliance (ninjateam.c)

Congratulations! You have graduated from the Ninja Academy! Now it’s time to form your own alliance of ninjas with yourself and fellow graduates. You will be able to tackle the toughest ninja jobs and bring home the best rewards.

You want to make sure your team is balanced, so as you add members you will track their name and their strongest skillset. You will need to be able to do the following:

Add a ninja to your team

Remove a ninja from your team

Search for a particular ninja by name

List all of the ninjas with a particular skillset

To make the testing of the program easier, your program will read in all of its input from a file called “ninjas.txt”. Your alliance of ninjas cannot exceed 1000 members.

Header Specification

To facilitate grading, include in your header comment which portions of the assignment you have completed. You must complete all options in order to earn full credit, but partial credit is available for completing some of the steps. The primary steps are as follows:

(1) Adding ninjas only

(2) Adding and removing ninjas only

(3) Adding, removing, and searching ninjas only

(4) All options implemented: adding, removing, searching, and listing ninjas

If your comment is accurate, meaning that you pass the appropriate tests cases corresponding to your choice, you'll earn 10 points. If your comment is roughly accurate, meaning that you sincerely attempted the items you listed, and most of them work minus a tiny bug, then you'll get 5 of these points. If your comment isn't accurate to a reasonable degree, you'll get 0 of these points.

The reason this is here is because it's very important to communicate to others accurately what you've accomplished and what is left to accomplish. This sort of honesty and ability to appraise your own work is a critical skill in a job.

Input Specification (ninjas.txt)

The first line of the file will contain a single positive integer representing the number of updates to the ninja alliance. (Note: We assume that at the beginning of the program, the team has no ninjas on it.) Each following line will have directions for a single update to the team. The formats for these lines are as follows:

The very first value on each of these lines will be a string from the following subset: {ADD, REMOVE, SEARCH, LIST}. This string indicates which operation (corresponding to the list in the problem statement) is being designated.

If the operation is to add a ninja (ADD), then the line will contain the following information after ADD, separated by spaces:

NAME SKILL

The name and skill of the ninja will be strings that contain only alphabetic letters and underscores. None of these strings will exceed 39 characters. No ninja will share a name with another ninja, though there may be multiple ninjas with the same special skillset.

If the operation is to remove a ninja (REMOVE), then the line will only contain the exact name of the ninja after REMOVE.

If the operation is to search for a ninja (SEARCH), then the line will only contain the exact name of the ninja that is being searched for.

If the operation is to list all of the ninjas by a particular skill (LIST), then the line will only contain the desired skill after LIST.

Output Specification

All output should go to the screen. Separate the output for each command with one blank line. Here is the format for output for each command:

For adding a ninja, simply write out a statement of the form:

X has joined the alliance! Their special skill is Y.

For removing a ninja, simply write out a statement of the form:

X has left the alliance.

For both of these, X represents the name of the ninja and Y represents his or her special skillset. You may assume that remove operations in the input file will always be valid. Thus, if a remove is ever requested, the ninja in question is guaranteed to be on the team.

For searching for a ninja, output one of two statements, depending on whether or not the designated ninja was found on the team:

X is currently in the alliance.

X is NOT currently in the alliance.

For searching for all the ninjas with a particular skill, output a single line of the form:

Members with Y skill:

Y represents the skill requested. Each following line should list the name of one ninja with that skill in the alliance. List each ninja exactly once.

Implementation Restrictions

You must use the following constants:

#define MAX_LENGTH 40

#define MAX_TEAM 1000

You must use the following structures to store information:

struct ninja {

    char name[MAX_LENGTH];

    char skill[MAX_LENGTH];

};

   

struct alliance {

    struct ninja team[MAX_TEAM];

    int num_ninjas;

};

   

It is not sufficient to simply have the structures in your program, you must use them store information and process operations for the alliance.

Though function prototypes will not be provided, it is expected that you follow good programming design and create several functions with well-specified tasks related to the solution of this problem. Make sure to pay very careful attention to parameter passing.

Hints

Use pass by reference parameters for your functions. This will ensure that any change made to the ninja alliance in a function is reflected in main. Inside the function, remember to access the components using the -> syntax for structures.

Use the prewritten string functions when dealing with strings:

use strcpy whenever you want to copy the contents of one string into another, instead of the equals sign.

use strcmp to determine which operation is being requested.

Whenever you add a ninja to the team, make sure you add it to the first open slot on the team.

Whenever you remove a ninja from the team, make sure you copy the ninja in the last spot on the team into the vacated spot. For example, if the ninja to be removed is in index 3 and the number of ninjas on the team before removal is 7, then the ninja in index 6 (the last filled index) should be copied into the ninja spot in index 3. Subsequently, the number of ninjas on the team should be updated to 6.

Sample Input File (ninjas.txt)

12

ADD NAGATO SWORDS

ADD SANDAYU STAVES

SEARCH HANZO

ADD HANZO STEALTH

ADD CHIYOME DISGUISE

ADD KOTARO STEALTH

LIST STEALTH

REMOVE KOTARO

ADD HATTORI SWORDS

LIST SWORDS

LIST DISGUISE

LIST STAVES

Sample Output (corresponding to input file)

NAGATO has joined the alliance! Their special skill is SWORDS.

SANDAYU has joined the alliance! Their special skill is STAVES.

HANZO is NOT currently in the alliance.

HANZO has joined the alliance! Their special skill is STEALTH.

CHIYOME has joined the alliance! Their special skill is DISGUISE.

KOTARO has joined the alliance! Their special skill is STEALTH.

Members with STEALTH skill:

HANZO

KOTARO

KOTARO has left the alliance.

HATTORI has joined the alliance! Their special skill is SWORDS.

Members with SWORDS skill:

NAGATO

HATTORI

Members with DISGUISE skill:

CHIYOME

Members with STAVES skill:

SANDAYU

Deliverables

One source file: ninjateam.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.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 40
#define MAX_TEAM 1000 // all are given data structs and required headers
void readfile(FILE *fp);
struct ninja {
char name[MAX_LENGTH];
char skill[MAX_LENGTH];
};
enum { MAXLINES = 30 };

struct alliance {
struct ninja team[MAX_TEAM];
int num_ninjas;
};

int main()
{
FILE *fp; // File object
fp=fopen("H:/ninjas.txt","r"); // opening file Observe the location
if(fp==0) // checking weather the file opened or not
{
printf("unable to open the file"); // file opening failed
exit(-1);
}
else
readfile(fp); // calling function by passing reference of file object
return 0;
}

void readfile(FILE *fp) // actual code starts here
{
struct ninja names; // Struct type variables
struct ninja skills;
char temp[100]; // temporary char array for string operations
string s;   
int num;
int k;
int namesize;
fscanf(fp,"%d",num); // reading the first word 12
while (fscanf(fp, " %s",temp ) == 1) { // continuing loop til the last word
puts(temp); // putting string into temp
switch(temp); // switch case for different kind of operations
{
case "ADD": // if the word is ADD
fscanf(fp,"%s",names.name); // reading next word name
fscanf(fp,"%s",skills.skill); // reading skill
printf("%s has joined in alliance with skills %s",names.name[namesize-1],skills.skill[namesize-1]);
nimesize++; // incrementing array size for next store
break;
case "LIST": fscanf(fp,"%s",s); // Switch for LIST word
for(k=0;k<namesize;k++) // checking the existence of word in the array
{
if(strcmp(s,names.name[i])) // comparing equality of string
{
printf("%s is in the alliance ",s);
printf("%s has skills %s",names.name[i],skills.skill[i]); //printing the result according to //conditions
}
else if(k==namesize)
printf("%s is Not in alliance ",s);
}
break;
case "SEARCH": fscanf(fp,"%s",s);
for(k=0;k<namesize;k++) // for loop to search the word in array
{
if(strcmp(s,names.name[i])) // search techniq goes here with the help of strcmp()
{
printf("%s is in the alliance ",s);
  
}
else if(k==namesize)
printf("%s is Not in alliance ",s);
}
break;
case "REMOVE":fscanf(fp,"%s",s);
for(k=0;k<namesize;k++) // searching word to remove
{
if(strcmp(s,names.name[i]))
{
names.name[i]=""; // making null afer finding the word
  
}
else if(k==namesize)
printf("%s is Not in alliance ",s);
}
break;

  
}
  
}
}

// Due to the time I could not able to execute the code so understand the logic, and look into the systax before //executing the program