Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives 1. Give students additional practice with functions 2. Give students additional practice with sorting structs 3. Give students additional practice with file 10 4.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Objectives 1. Give students additional practice with functions 2. Give students additional practice with sorting structs 3. Give students additional practice with file 10 4. Give students practice with structs Problem A: Meaning of life (dictionary.c) In this vast world every human being searches for meaning. I too am on this quest, and truthfully, I need a better way to find the meaning of words without the use of the internet. For this program you will need to write several functions for my program that involve structs used by my program. The structs are used to store my words and definitions. See below the structs that will be used, #define WORD_LEN 20 #define TEXT LEN 80 #define MAX_DEFS 10 #define MAX_WORDS 100 struct definition { char part_of_speech [WORD_LEN + 1]; char text [TEXT_LEN + 1]; }; struct word { char theWord [WORD_LEN + 1]; int num_of_defs; struct definition defs [MAX_DEFS]; }; struct dictionary { int num_of_words; struct word words [MAX_WORDS]; }; The function prototypes used will be the following // Function that takes as input a dictionary and a word // If the word does not already exist and dictionary has room, then // * the function adds the word and returns 1 // If the word does exist or the dictionary is full, then // * the dictionary is not modified and return 0 int addWord (struct dictionary * dict, char word[]); // Function that takes as input // * a dictionary, // * a word, // the part of speech of the word, and // * the text of the definition // If the word does not exist and the dictionary has room, then // * the function adds the word with the given definition & returns 1 // If the word does exist and can have the definition added, then // * the function adds the definition // If there is no room, then // * the function does not modify the dictionary and returns 0 int addDef (struct dictionary * dict, char word[], char part_of_speech[], char text[]); // Function that takes as input // * a dictionary, // * a word, // If the word exists in the dictionary, then // * return the address to the word in the dictionary // If the word does not exist in the dictionary, then // * return NULL struct word * lookUp (struct dictionary * dict, char word[]); Input Specification There will be no input. I will be using your functions in my own code. Output Specification There should be no output. I will be using your functions in my own code. Problem B: Pubs and Ponies (pandp.c) Your friend organized a session of Pubs and Ponies. The game involves using miniature equestrian transport to move between locations and build taverns to make as much money as possible. The game is overly simplistic since you always get more money than you spend for construction of a tavern. As long as you have the money to build a tavern you can travel to the location and perform the construction. You always end up just visiting the locations in the cheapest order of their cost, but your friend likes to draw out the time it takes to travel between locations. You don't want to bother yourself with playing the game yourself. You will instead write a program to figure out how many taverns you will build, how far your character will travel, and how much money they will make in the game. Note your character starts the session at location (0,0). You must use the following struct in your code. struct pub { int cost; int reward; int x; int y; }; Input Specification You will be given a string representing the filename that contains a description of the session. The file name will be at most 20 characters, and contain no spaces. Input File Specification The first line of the input file contains 2 integers, N and M, representing the number of pubs and the starting cash of your character. The following N lines each contain 4 space separated integers, C, R, X, and Y, representing the cost of building the tavern (necessary to begin building), the reward for building the tavern, the X coordinate of the tavern, and the Y coordinate of the tavern. You are guaranteed that no two taverns will cost the same amount. Output Specification Output a single line in the following format. You will travel M miles and build T taverns for a profit of $P. Where M is the number of miles you travel. T is the number of taverns that will be built and the P is the net profit. Output Sample On the next page is one sample output of running the program. Note that this sample is NOT a comprehensive test. You should test your program with different data than what is shown here based on the specifications given above. In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: when you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity's sake.) Sample Run What is the name of the game description file? session 1.txt You will travel 32.434919 miles and build 4 taverns for a profit of $9. File name Contents session 1.txt 6 10 20 100 0 0 10 12 1 3 50 90 -1 -3 4 8 3 4 78 -2 - 4 1 3 8 6 Hints . I recommend sorting the taverns by their cost. Loop through these sorted taverns and break out early if a tavern is too expensive. Restrictions Please ensure that your programs compile with an approved C compiler (e.g. gcc from Code::Blocks, Dev C++, MinGW, etc. or clang from XCode). Write each problem in a separate file with the names specified previously, dictionary.c, and pandp.c. Each program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure to include comments throughout your code describing the major steps in solving the problem. Objectives 1. Give students additional practice with functions 2. Give students additional practice with sorting structs 3. Give students additional practice with file 10 4. Give students practice with structs Problem A: Meaning of life (dictionary.c) In this vast world every human being searches for meaning. I too am on this quest, and truthfully, I need a better way to find the meaning of words without the use of the internet. For this program you will need to write several functions for my program that involve structs used by my program. The structs are used to store my words and definitions. See below the structs that will be used, #define WORD_LEN 20 #define TEXT LEN 80 #define MAX_DEFS 10 #define MAX_WORDS 100 struct definition { char part_of_speech [WORD_LEN + 1]; char text [TEXT_LEN + 1]; }; struct word { char theWord [WORD_LEN + 1]; int num_of_defs; struct definition defs [MAX_DEFS]; }; struct dictionary { int num_of_words; struct word words [MAX_WORDS]; }; The function prototypes used will be the following // Function that takes as input a dictionary and a word // If the word does not already exist and dictionary has room, then // * the function adds the word and returns 1 // If the word does exist or the dictionary is full, then // * the dictionary is not modified and return 0 int addWord (struct dictionary * dict, char word[]); // Function that takes as input // * a dictionary, // * a word, // the part of speech of the word, and // * the text of the definition // If the word does not exist and the dictionary has room, then // * the function adds the word with the given definition & returns 1 // If the word does exist and can have the definition added, then // * the function adds the definition // If there is no room, then // * the function does not modify the dictionary and returns 0 int addDef (struct dictionary * dict, char word[], char part_of_speech[], char text[]); // Function that takes as input // * a dictionary, // * a word, // If the word exists in the dictionary, then // * return the address to the word in the dictionary // If the word does not exist in the dictionary, then // * return NULL struct word * lookUp (struct dictionary * dict, char word[]); Input Specification There will be no input. I will be using your functions in my own code. Output Specification There should be no output. I will be using your functions in my own code. Problem B: Pubs and Ponies (pandp.c) Your friend organized a session of Pubs and Ponies. The game involves using miniature equestrian transport to move between locations and build taverns to make as much money as possible. The game is overly simplistic since you always get more money than you spend for construction of a tavern. As long as you have the money to build a tavern you can travel to the location and perform the construction. You always end up just visiting the locations in the cheapest order of their cost, but your friend likes to draw out the time it takes to travel between locations. You don't want to bother yourself with playing the game yourself. You will instead write a program to figure out how many taverns you will build, how far your character will travel, and how much money they will make in the game. Note your character starts the session at location (0,0). You must use the following struct in your code. struct pub { int cost; int reward; int x; int y; }; Input Specification You will be given a string representing the filename that contains a description of the session. The file name will be at most 20 characters, and contain no spaces. Input File Specification The first line of the input file contains 2 integers, N and M, representing the number of pubs and the starting cash of your character. The following N lines each contain 4 space separated integers, C, R, X, and Y, representing the cost of building the tavern (necessary to begin building), the reward for building the tavern, the X coordinate of the tavern, and the Y coordinate of the tavern. You are guaranteed that no two taverns will cost the same amount. Output Specification Output a single line in the following format. You will travel M miles and build T taverns for a profit of $P. Where M is the number of miles you travel. T is the number of taverns that will be built and the P is the net profit. Output Sample On the next page is one sample output of running the program. Note that this sample is NOT a comprehensive test. You should test your program with different data than what is shown here based on the specifications given above. In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: when you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity's sake.) Sample Run What is the name of the game description file? session 1.txt You will travel 32.434919 miles and build 4 taverns for a profit of $9. File name Contents session 1.txt 6 10 20 100 0 0 10 12 1 3 50 90 -1 -3 4 8 3 4 78 -2 - 4 1 3 8 6 Hints . I recommend sorting the taverns by their cost. Loop through these sorted taverns and break out early if a tavern is too expensive. Restrictions Please ensure that your programs compile with an approved C compiler (e.g. gcc from Code::Blocks, Dev C++, MinGW, etc. or clang from XCode). Write each problem in a separate file with the names specified previously, dictionary.c, and pandp.c. Each program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure to include comments throughout your code describing the major steps in solving the

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Valuation For Accountants A Short Course Based On IFRS

Authors: Stephen Lynn

1st Edition

9811503567, 9789811503566

More Books

Students also viewed these Accounting questions