联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2019-02-10 11:02

EngGen 131 2018

Lab 10

Project, Strings and Random numbers

HAVE YOU COMPLETED THE PREPARATION EXERCISES?

Pages 157-164 of the coursebook contain the “Preparation Exercises” for this lab. You should

complete these exercises before you attempt the exercises in this lab. In particular, these preparation

exercises illustrate a Monte-Carlo technique and basic string manipulation and comparison.

GETTING STARTED

When you have completed the “Preparation Exercises”, download the .zip file called

“Lab10Resources.zip” from Canvas. This file contains all of the resources that you will need to

complete this lab.

DEVELOPMENT ENVIRONMENT

You may use either the Developer Command Prompt or the Visual Studio integrated development

environment - just choose whatever environment you prefer.

EngGen 131 S2 2018

Lab 10 – 2 – Course Manual (C Programming)

EXERCISE ONE

Crossword Helper

When solving a crossword puzzle, it would sometimes be helpful to have a way of viewing all of the

possible words that could fit in a particular place given the partial clues that have already been solved.

For example, if you already had the first two and the last two

characters of a 6-letter word:

ab--rd

it might be useful to be able to produce a list of all possible

words that could fit. In this case, there are just two:

aboard

absurd

We can solve this problem using a word list, and a function to

check if a word matches a given pattern.

For this exercise, you need to complete just one function:

int WordMatchesPattern(char *word, char *pattern)

The WordMatchesPattern() function takes two strings as input, and returns true only if the

string “word” is a possible completion of the string “pattern”. The “pattern” string is a partial

word where some of the characters have been replaced with hyphens (-). The “word” will be a valid

completion of the “pattern” if all of the characters not corresponding to hyphens match.

For example:

word: “apple” and pattern “a-p-e” is a match

word: “apple” and pattern “a---e” is a match

word: “apple” and pattern “--ple” is a match

whereas

word: “apple” and pattern “app--e” is not a match

word: “apple” and pattern “---p-” is not a match

word: “apple” and pattern “--l-e” is not a match

The main() function, which reads all of the words from a provided text file of English words, is

already provided for you. The program prompts the user for a pattern, and should display all of the

words that match the pattern along with a count of the total number of matching words.

EngGen 131 S2 2018

Lab 10 – 3 – Course Manual (C Programming)

The main() function that is provided to you is given below:

int main(void)

{

FILE *input;

char word[100];

char pattern[100];

int count;

input = fopen("wordlist.txt", "r");

count = 0;

if (input == NULL) {

printf("Could not open file.");

} else {

printf("Enter pattern: ");

scanf("%s", pattern);


while (fscanf(input, "%s", word) != EOF) {

if (WordMatchesPattern(word, pattern)) {

printf("%s\n", word);

count++;

}

}

printf("\n%d matches", count);

}


return 0;

}

Below are some examples of how the program should behave once you have correctly defined the

WordMatchesPattern() function (user input is in bold).

Enter pattern: p-uplug


plum

plus

pouf

pour

pout

6 matches

Enter pattern: apple

apple

1 matches

Enter pattern: a-b-c-d

0 matches

Enter pattern: --------e--------

compartmentalised

compartmentalises

compartmentalized

compartmentalizes

counterrevolution

environmentalists

intergovernmental

plenipotentiaries

8 matches

Enter pattern: he---us

heinous

1 matches

Having trouble running the program?

Are you seeing “Could not open file”?

This program reads data from a file (called "wordlist.txt")

stored on disk. If you are using the Developer Command

Prompt to compile and run your program, then the file

should be read correctly as long as it is in the same folder as

your source file.

If you are using the Visual Studio environment, you will

need to ensure that your project is configured so that it can

find the data files on disk. If you create your project as

described here:

https://www.cs.auckland.ac.nz/~paul/C/Windows/

and place the data file ("wordlist.txt") in the same folder as

the location of the project file (which has a .vcxproj

extension), the project should be able to locate it.

If you continue to have any difficulty with this, simply use

the Developer Command Prompt as that will work perfectly!

EngGen 131 S2 2018

Lab 10 – 4 – Course Manual (C Programming)

EXERCISE TWO

Triangles

Read the description of the following algorithm very carefully. This algorithm describes a process for

plotting a collection of points.

1) Initialise point A, point B and point C to represent the vertices of a triangle. Plot these points.

This is illustrated below.

2) Define a fourth point (call it D) which should be randomly positioned anywhere

– it does not need to be inside the triangle formed by A, B and C. Plot point D as well.

3) Show the following prompt to the user, which asks them how many points they would like to

plot in total:

Enter number of points:

4) Select either A or B or C at random, and plot the midpoint between this random

vertex and the last point plotted.

5) Repeat step 4 over and over again, until the requested number of points have been plotted

________________________________

For this exercise, you need to write a program which implements this algorithm and which plots all of

the points to an image file (using the LibBMP library). The first part of the algorithm has been

implemented for you - the first four points (A, B, C and D as described above) have already been

generated and plotted on the image. Points A, B and C are drawn in red and point D is drawn in blue.

You may need to zoom into the image to see these points, as they are each only a single pixel!

Your job is to complete the implementation of Steps 4 and 5.

Try plotting lots of points - what pattern emerges? Feel free to experiment with adding colours to this

picture, or to adjust the positions of the initial points A, B and C.

COMPULSORY LAB TASKS END HERE

A

B C

EngGen 131 S2 2018

Lab 10 – 5 – Course Manual (C Programming)

EXERCISE THREE (optional)

Word Builder

The NZ Herald often publishes a puzzle called “Word

builder”. The idea of the puzzle is that you are given five

letters, and using those letters you must form as many

words as you possibly can. Each solution word:

must be a real word from the dictionary

must consist of three or more letters

must only use the five letters provided

must not use any of the five letters more than once

(i.e. there can be no repeated letters in a solution

word)

For example, given the letters ‘a’, ‘n’, ‘c’, ‘h’, ‘t’ as shown

in the picture, there are 13 solutions to the puzzle: act, ant,

can, cant, cat, chant, chat, hat, nah, natch, nth, tan, than.

Of course, you might want to argue that some of these are

not real English words, but that just depends on whose

dictionary you are using!

For this exercise you must define one function.

The one function you define will be called IsSolution(), and this function will be given two

inputs which are both strings. The first input will be a potential solution word to the “Word builder”

puzzle and the second input will be the five allowed letters. The prototype declaration for this

function is as follows:

int IsSolution(char *word, char *letters)

Your IsSolution() function must return true (i.e. 1) or false (i.e. 0). The function should return

true if the potential solution word is actually a valid solution word. That is, if the word satisfies the

following conditions:

1. it is at least 3 characters in length

2. it does not use any letters that are not part of the five allowed letters

3. it does not contain any repeated letters

and of course your function should return false if the potential solution word is not actually a solution!

How do you test your function to see if it is correct?

Lucky for you, you have been provided with a program that attempts to generate all of the solutions

for a given puzzle! The program begins by asking the user to enter the five allowed letters for the

puzzle. The program then uses a dictionary file (consisting of 60,340 words), and it calls your

function 60,340 times - once for each word in the dictionary. As long as your function returns true

only when a word is actually a solution (given the five allowed letters), then the program will print out

just the valid solutions to the puzzle.

EngGen 131 S2 2018

Lab 10 – 6 – Course Manual (C Programming)

In the Lab10Resources folder, you have been provided with this incorrect implementation of the

IsSolution() function:

int IsSolution(char *word, char *letters)

{

// You need to fix this function. At the moment it always returns

// false - clearly this isn't right, as it means there will always be

// no solutions to the puzzle!

return 0;

}

You need to fix this broken implementation by replacing the “return 0;” with the correct code for this

function.

Can I see an example of how the program should behave?

Sure thing - once you have completed the IsSolution() function correctly, if the user inputs the 5

letters ‘a’, ‘d’, ‘n’, ‘s’ and ‘t’ then the output of your program should be as follows (user input is in

bold):

Welcome to Lab 10 - "Word builder"

Please enter 5 lower case letters without any spaces (e.g. abcde): adnst

All solutions:

0) ads

1) and

2) ant

3) ants

4) sad

5) sand

6) sat

7) stand

8) tan

9) tans

There are 10 solutions to the puzzle.

When you have finished, you should test your function by running the program with a few different

input values. Once you have it working, you can now solve all the “Word builder” puzzles in the

newspaper!

How cool!


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp