Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSci 1113: Introduction to C/C++

Programming for Scientists and Engineers

Homework 3

Fall 2022

Instructions: This is an individual homework assignment. There are two problems worth 20    points. Solve the problem below by yourself (unlike the labs, where you work collaboratively), and submit the solution as a C++ source code file. Here are a few more important details:

1.   Unlike the computer lab exercises, this is not a collaborative assignment.

a.   You must design, implement, and test the solution to each problem on your    own without the assistance of anyone other than the course instructor or TAs.

b.  You may use examples from the textbook, lectures, and code you and your partner wrote to solve lab problems.

c.   You may not include solutions or portions of solutions obtained from any other  source. This includes Chegg, stackoverflow, etc. We check these sites against all

submitted solutions.

d.  Gradescope also has a similarity checker which automatically compares your submitted solution to all other submitted solutions.

e.   Otherwise obtaining or providing solutions to any homework problems for  this class is considered academic misconduct. See the Scholastic Conduct section in the syllabus in Canvas for more details, and ask the instructor if  you have questions.

f.   Follow the naming conventions strictly.

g.  No late assignments will be accepted.

h.   If a program doesn’t compile, the score will be a zero.

2.   Because all homework assignments are submitted and tested electronically, the following are important:

You follow the naming conventions mentioned for the problems.

You submit the correct file(s) on Gradescope ( https://www.gradescope.com/ ) by the due deadline.

You follow the example input and output formats exactly given in each problem description.

Regardless of how or where you develop your solutions, your programs compile and execute on gradescope computers (which run Linux/Ubuntu operating        system like the cselabs machines).

3.   The problem descriptions will usually show at least one test case and the resulting correct output. However, you should test your program on other test cases (that you make up) as well. Making up good test cases is a valuable programming skill and is part of ensuring   your code solution is correct.

Problem A: Generate sequential prime numbers. (20 points)

(Part of the solution to this problem will be needed in the next problem.)

In this problem, you are supposed to generate all the prime numbers up to a certain integer         (inclusive). The definition of this certain integer can be found in a randomly selected word from a text file (with a .txt extension) and is as follows:

integer value ofa word = sum ofASCII values ofthefirst and the last letters ofthe word - 194

According to this definition, the integer value of the word andy is ( (97+121) - 194 ) = 24. Therefore, the prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23.

To randomly select a word from a text file, you must have a user-defined function in your program, named random_word, which has the following function prototype.

string random_word(string filename);

The random_word function takes a string type filename as its input and returns a string type randomly selected word as its output. The function shall do the following:

●   Successfully terminate the program (without raising an ERROR) with the          message “Unable to open <filename>.txt file.” if the filename is incorrect or if the file does not exist.

●   Read the content of the text file line by line, randomly select a word, and return   the selected word to the function call. The randomly selected word should vary at different executions ofyour file. Also, your random number generation must        consider all the words in the text file.

* You may assume that the text file will contain only a single word in each line. All the words will be in lowercase.

Putting things together: your main program shall call the  random_word function to select a   random word from the text file, compute the integer value of the word, and finally, print all the   prime numbers up to that integer value (inclusive). For testing the random word selection part of your homework, create a text file and put some words in the file at each line. You may begin       testing with a small file which has a small number of lines.

Example 1 (user input is underlined for clarity, underlined text in your program is not needed): Enter the filename of the word database? Names.txt

Selected word = andy

Integer value of the word andy = 24

Expected prime numbers = 2, 3, 5, 7, 11, 13, 17, 19, 23,

Example 2 (user input is underlined for clarity, underlined text in your program is not needed): Enter the filename of the word database? FileDoesNotExist.txt

Unable to open FileDoesNotExist .txt file .

Terminating the program . . .

Test your program using not only the example data above, but other cases as well. And revise your program until you are sure it is correct. Once completed do the following:

1.  Name your file: hw3A.cpp

2.   If a file is submitted with a different, then we won’t be able to compile and execute your program and you will receive a score of zero for the assignment.

3.   Log onto gradescope and upload the file, called hw3A.cpp, for the Homework 3A submission.

4.   If you name your file incorrectly it will be unable to compile and run your code, so   you will fail all test cases. You may submit cpp files as many times as you want until the deadline to try and fix the code if you fail a test case. Following rigorous naming             conventions and using test cases are something computer programmers often must do in  “real life” programming, and so submitting your program with the correct name and        functionality is part of doing this assignment correctly.

5.   This is called test driven software development.

Problem B: Guess the word. (20 points)

In this problem, you are supposed to design and implement a modified version of the game Wordle called modWordle. Game rules and the steps are as follows:

●   A player will be given a word to guess and will have 5 attempts.

●   At each guessing turn, the program must prompt the user for a word. You user must only input 4-letter words. Then, the program shall display the letters which are in their correct positions, otherwise, display * in the corresponding positions.

   The program must also display the number of attempts the user took to guess the word.

   If the player is not able to guess the word in 5 attempts, they lose.

* You may assume that the modWordle database text file will contain words of length 4 only. Also, each line will contain only a single word. And, all the words will be in lowercase.

You will need to use the same random_word function to randomly pick a word from the      database text file to initiate the game. For testing the random word selection part of your          homework, create a text file and put some words in the file at each line. You may begin testing with a small file which has a small number of lines.

Example 1 (user input is underlined for clarity, underlined text in your program is not needed): Enter the filename of the word database? FileDoesNotExist.txt

Unable to open FileDoesNotExist .txt file .

Terminating the program . . .

Example 2 (user input is underlined for clarity, underlined text in your program is not needed): Enter the filename of the word database? Words.txt

Initiating modWordle

Word: four

Guess 1: fine

Progress: f***

Guess 2: fear

Progress: f**r

Guess 3: four

Progress: four

You win!! You guessed the word in 3 attempts .

Example 3 (user input is underlined for clarity, underlined text in your program is not needed): Enter the filename of the word database? Words.txt

Initiating modWordle

Word: dear

Guess 1: swim

Progress: ****

Guess 2: true

Progress: ****

Guess 3: lint

Progress: ****

Guess 4: fear

Progress: *ear

Guess 5: pear

Progress: *ear

You lose!! You were not able to guess the word .

Test your program using not only the example data above, but other cases as well. And revise your program until you are sure it is correct. Once completed do the following:

1.  Name your file: hw3B.cpp

2.   If a file is submitted with a different, then we won’t be able to compile and execute your program and you will receive a score of zero for the assignment.

3.   Log onto gradescope and upload the file, called hw3B.cpp, for the Homework 3B submission.

4.  If you name your file incorrectly it will be unable to compile and run your code, so   you will fail all test cases. You may submit cpp files as many times as you want until the deadline to try and fix the code if you fail a test case. Following rigorous naming             conventions and using test cases are something computer programmers often must do in  “real life” programming, and so submitting your program with the correct name and        functionality is part of doing this assignment correctly.

5.   This is called test driven software development.