CS 103 Computer Programming: Assignment Number 1a February 3, 2017
CS 103 Computer Programming: Assignment Number 1a February 3, 2017
Assignment Number 1a
February 3, 2017
For all the problems you are required to user “char *”, usage of string is strictly prohibited.
Please start early otherwise you will struggle with it.
Note: You have to follow the submission instructions to the letter. Failing to do so can get a zero
in assignment.
Q1: Morse Code Morse code has been one of the most basic communication protocol and still used to convey SOS
messages and other urgent communication. In Morse code each English alphabet is encoded by a sequence of ’.’ and
’-’, see Figure 1 for complete mapping between English alphabets and Morse codes. Your task is to design a program
that can (i) convert any given string into a Morse code sequence and (ii) a morse code sequence to string. You are
not authorized to used string date type, however you can use char *
Q2: Hangman In this question your goal is to write a program for playing Hangman game. You are given a
dictionary of 4000 frequently occurring '(1-4000.txt)' English words and a function for file reading and extracting a
list of strings. During game execution, your program will randomly choose any words from the list of read words and
use it as a target guessing word. Your program must display 31 randomly selected letters from the chosen word and
ask the user to guess the remaining 32 letters of the given word. The user can at most have twice the number of tries to
the displayed blanks to guess the word correctly. On each wrong guess you will display some portion of the Hangman
ASCII graphic given below. If the user is unable to guess the word in the given number of tries you can either finish
the game or deduct his life count.
1
Figure 1: A mappping table between English Alphabets and Morse codes. Source:http://en.wikipedia.org/
wiki/Morse_code
16 int count=0;
17 while(ifile && count < nwords)
18 ifile >> words[count++];
19 ifile.close();
20 }
2
1 // ASCII art for Hangman as an array of strings
2 char *hangman[19]={" ___________.._______",
3 "| .__________))______|",
4 "| | / / ||",
5 "| |/ / ||",
6 "| | / ||.-’’.",
7 "| |/ |/ _ \\",
8 "| | || `/,|",
9 "| | (\\\\`_.’",
10 "| | .-`--’.",
11 "| | /Y . . Y\\",
12 "| | // | | \\\\",
13 "| | // | . | \\\\",
14 "| | ’) | | (`",
15 "| | ||’||",
16 "| | || ||",
17 "| | || ||",
18 "| | || ||",
19 "| | / | | \\",
20 " |_`-’ `-’ | |"};
Q3: Magic Squares In this question your goal is to write code for solving magic square (for details read the
attached file magic-square.pdf) using 2-D pointers. Your program should ask the user for the dimension of magic
square and then solve the magic square for given dimension. You must do the proper allocation and deallocation of
the memory.
Q4: String manipulation functions Write the implementation of the following functions
13 }
3
1 char *StrCat( char *s1, const char *s2 )
2 /*Appends string s2 to array s1.
3 The first character of s2 overwrites
4 the terminating null character of s1.
5 The value of s1 is returned.*/
6 {
7
8 }
9
18
4
5
Q5: String Text Editor [30 Marks]
Here your goal is to build a string text editor, which will allow its user to create simple text files and store them
in primary memory (Remember you will be storing data in primary memory RAM not on hard disk, so you will not
need file handling). Your text editor will have following main menu:
Press 1. To create a new file.
Press 2. To view an existing file via giving a file name.
Press 3. To edit an exisiting file via giving its name.
Press 4. To copy an exisiting file to a new file.
Press 5. To delete an exisiting via its name.
Press 6. To view list of all files with the names.
Press 7. To Exit.
File Creation Whenever user will press 1. your program will ask for the new file name and number of text lines in
the file. You will also tell the user that in any single line of file there can be maximum of 60 characters, if there are
more than 60 characters in a single line the remaining characters should be moved to next line. While taking input
from user your program should automatically display each line number on the terminal, you will be using this number
during file editing for updating a particular line. For example, this is how your program output will editing a file.
1. I am writing a text file.
2. This is second line of my file and i am storing data
3. on line by line basis.
File Editing User should be able to edit a file by giving its name. When editing a file user can upgrade any single
line by giving its number and your program will update the content of that line with the newly provided text by user.
Furthermore, use can ask to replace a specific word, e.g. user can ask to replace all the instances of word “work” with
“enjoy”. Your program should find all the instances of work work and should replace it with “enjoy”.
File Copying Your program will allow its user to create a copy of an existing file and he should be asked to provide
a valid new file name.
File Deleting User should be allowed to delete a file via its name.
For completing this task you will need to make use of dynamic memory and multi-dimensional pointers. So please
start early otherwise you will struggle with it.
Q5:Text Analysis The availability of computers with string-manipulation capabilities has resulted in some rather
interesting approaches to analyzing the writings of great authors. This exercise examines three methods for analyzing
texts with a computer. You have to use char * for following exercises.
1. Write a function that receives a string consisting of several lines of text and returns an array indicating the
number of occurrences of each letter of the alphabet in the text. For example, the phrase “To be, or not to be:
that is the question”: contains one “a,” two “b’s,” no “c’s,” and so on.
2. Write a function that receives a string consisting of several lines of text and returns an array indicating the
number of one-letter words, two-letter words, three-letter words, and so on, appearing in the text. For example,
the phrase “Whether this nobler in the mind to suffer” contains 2, 3, 4, etc. length words.
6
1 void countWordsBasedOnLength(char *string, int *&array/*to be allocated */,
2 int & size/*updated array size*/)
3 /*Parameters:
4 Input:
5 char * : a multiline string
6 Output:
7 int *: an array containing counts of each different length words,
8 to be allocated in function
9 int : array size */
10 ;
3. Write a function that receives a string consisting of several lines of text and returns arrays indicating unique
words and the number of occurrences of each unique word in the text along with their size.