0% found this document useful (0 votes)
4 views7 pages

Problem Set 02

The document outlines a problem set for a Data Structure Laboratory course at Khulna University, focusing on programming tasks involving arrays and random numbers. It includes 20 distinct programming challenges such as generating lottery numbers, calculating rainfall statistics, validating account numbers, and implementing games like tic-tac-toe. Each problem emphasizes the use of arrays and loops to manipulate and display data effectively.

Uploaded by

Rakib Sheikh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Problem Set 02

The document outlines a problem set for a Data Structure Laboratory course at Khulna University, focusing on programming tasks involving arrays and random numbers. It includes 20 distinct programming challenges such as generating lottery numbers, calculating rainfall statistics, validating account numbers, and implementing games like tic-tac-toe. Each problem emphasizes the use of arrays and loops to manipulate and display data effectively.

Uploaded by

Rakib Sheikh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Khulna University, Khulna

Computer Science and Engineering Discipline


Course No: CSE 2102
Course Title: Data Strucute Laboratory
Problem Set: Arrays and Random Numbers

1. Design a program that generates a 7-digit lottery number. The program should
have an Integer array with 7 elements. Write a loop that steps through the array,
randomly generating a number in the range of 0 through 9 for each element. Then
write another loop that displays the contents of the array.

2. Design a program that lets the user enter the total rainfall for each of 12 months
into an array. The program should calculate and display the total rainfall for the
year, the average monthly rainfall, and the months with the highest and lowest
amounts.

3. Design a program that randomly generates a series of 20 numbers. The program


should store the numbers in an array and then display the following data:

• The lowest number in the array


• The highest number in the array
• The total of the numbers in the array
• The average of the numbers in the array

4. Design a program that asks the user to enter a charge account number. The
program should determine whether the number is valid by comparing it to the
following list of valid charge account numbers:
5658845 4520125 7895122 8777541 8451277 1302850
8080152 4562555 5552012 5050552 7825877 1250255
1005231 6545231 3852085 7576651 7881200 4581002
These numbers should be stored in an array. Use the sequential search algorithm
to locate the number entered by the user. If the number is in the array, the
program should display a message indicating the number is valid. If the number
is not in the array, the program should display a message indicating the number
is invalid.

5. Design a program that displays the number of days in each month. The program’s
output should be similar to this:
January has 31 days.
February has 28 days.
March has 31 days.
April has 30 days.
May has 31 days.
June has 30 days.
July has 31 days.
August has 31 days.
September has 30 days.
October has 31 days.
November has 30 days.
December has 31 days.
The program should have two parallel arrays: a 12-element String array that is
initialized with the names of the months, and a 12-element Integer array that
is initialized with the number of days in each month. To produce the output

1
specified, use a loop to step through the arrays getting the name of a month and
the number of days in that month.

6. Design a program that has two parallel arrays: a String array named people that
is initialized with the names of seven of your friends, and a String array named
phoneNumbers that is initialized with your friends’ phone numbers. The program
should allow the user to enter a person’s name (or part of a person’s name). It
should then search for that person in the people array. If the person is found, it
should get that person’s phone number from the phoneNumbers array and display
it. If the person is not found in the people array, the program should display a
message indicating so.

7. Design a program that uses the following parallel arrays:

• empId: An array of seven Integers to hold employee identification numbers.


The array should be initialized with the following numbers:
56588 45201 78951 87775 84512 13028 75804
• hours: An array of seven Integers to hold the number of hours worked by
each employee.
• payRate: An array of seven Reals to hold each employee’s hourly pay rate.
• wages: An array of seven Reals to hold each employee’s gross wages.

The program should relate the data in each array through the subscripts. For
example, the number in element 0 of the hours array should be the number of
hours worked by the employee whose identification number is stored in element
0 of the empId array. That same employees pay rate should be stored in element
0 of the payRate array.
The program should display each employee number and ask the user to enter
that employee’s hours and pay rate. It should then calculate the gross wages for
that employee (hours times pay rate), which should be stored in the wages array.
After the data has been entered for all the employees, the program should display
each employee’s identification number and gross wages.

8. The local driver’s license office has asked you to design a program that grades
the written portion of the driver’s license exam. The exam has 20 multiple choice
questions. Here are the correct answers:
1.B 6.A 11.B 16.C
2.D 7.B 12.C 17.C
3.A 8.A 13.D 18.B
4.A 9.C 14.A 19.D
5.C 10.D 15.D 20.A
Your program should store these correct answers in an array. (Store each ques-
tion’s correct answer in an element of a Stringarray.) The program should ask
the user to enter the student’s answers for each of the 20 questions, which should
be stored in another array. After the student’s answers have been entered, the
program should display a message indicating whether the student passed or failed
the exam. (A student must correctly answer 15 of the 20 questions to pass the
exam.) It should then display the total number of correctly answered questions,
the total number of incorrectly answered questions, and a list showing the ques-
tion numbers of the incorrectly answered questions.

9. Design a program that allows two players to play a game of tic-tac-toe. Use
a two-dimensional String array with three rows and three columns as the game
board. Each element of the array should be initialized with an asterisk (*). The
program should run a loop that does the following:

a. Displays the contents of the board array.


b. Allows player 1 to select a location on the board for an X. The program
should ask the user to enter the row and column number.

Page 2
c. Allows player 2 to select a location on the board for an O. The program
should ask the user to enter the row and column number.
d. Determines whether a player has won or if a tie has occurred. If a player
has won, the program should declare that player the winner and end. If a
tie has occurred, the program should say so and end.
e. Player 1 wins when there are three Xs in a row on the game board. Player
2 wins when there are three Os in a row on the game board. The winning
Xs or Os can appear in a row, in a column, or diagonally across the board.
A tie occurs when all of the locations on the board are full, but there is no
winner.

10. The Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, ....., where each number is
the sum of the two preceding numbers. Write a program fragment that declares
an array named fib numbers of length 40 and fills the array with the first 40
Fibonacci numbers.
Hint: Fill in the first two numbers individually, then use a loop to compute the
remaining numbers.

11. Calculators, watches, and other electronic devices often rely all seven-segment
displays for numerical output. To form a digit such devices “turn on” some of
the seven segments while leaving others “off”:

Suppose that we want to set up an array that remembers which segments should
be “on” for each digit. Let’s number the segments as follows:

Here’s what the array might look like, with each row representing one digit:
canst int segments [10] [7] = {{1, 1, 1, 1, 1, 1, 0}, · · · };
Above is the first row of the initializer; fill in the rest. Using this initialization,
write a program that will ask the user to enter a number and then dispays the
number, one digit at each line, using seven-segment dispay. If the user enters 123,
your program should display:

Page 3
12. Write a progrma that counts the repeated digits in a given number. It prints a
table showing how many times each digit appears in the number:
Enter a number: 41271092
Digit: 0 1 2 3 4 5 6 7 8 9
Occurrences: 1 2 2 0 1 0 0 1 0 1

13. The prototypical Internet newbie is a fellow named B1FF who has a unique way
of writing messages. Here’s a typical B1FF communique:
H3Y DUD3, C 15 R1LLY C00L!!!!!!!!!!
Write a “B1FF filter” that reads a message entered by the user and translates it
into B1FF-speak:
Enter message: Hey dude, C is rilly cool
In B1FF-speak: H3Y DUD 3, C 15 R1LLY C00L!!!!!!!!!!
Your program should convert the message to upper-case letters substitute digits
for certain letters (A→ 4, B→ 8, E → 3, I → 1, O → 0, S → 5), and then append
10 or so exclamation marks.
Hint: Store the original message in an array of characters, then go back through
the array, translating and printing characters one by one.

14. Write a program that generates a “random walk” across a 10 × 10 array. The
array will contain characters (all ’.’ initially). The program must randomly
“walk” from element to elcment, always going up, down, left, or right by one
element. The elements visited by the program will be labeled with the letters A
through Z, in the order visited. Here’s an example of the desired output:

Hint: Use the srand and rand functions to generate random numbers. After
generating a number, look at its remainder when divided by 4. There are four
possible values for the renlainder-0, 1, 2 and 3-indicating the direction of the next
move. Before performing a move, check that (a) it won’t go outside the array,
and (b) it doesn’t take us to an element that already has a letter assigned. If
either condition is violated, try moving in another direction. If all four direc-
tions are blocked, the program must terminate. Here’s an example of premature
termination:

Y is blocked on all four sides, so there’s no place to put Z.

15. Write a program that translates an alphabetic phone number into numeric form:
Enter phone number: CALLATT
2255288
(In case you don’t have a telephone nearby, here are the letters on the keys:
2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=WXY.) If the
original phone number contains nonalphabetic characters (digits or punctuation,
for example), leave them unchanged:

Page 4
Enter phone number: 1-800-COL-LECT
1-800-265-5328
You may assume that any letters entered by the user are upper case. The program
will need to store the phone number (either in its original forrn or in its numeric
form) in an array of characters until it can be printed. You may assume that the
phone number is no more than 15 characters long.

16. In the SCRABBLE Crossword Game, players form words using small tiles, each
containing a letter and a face value. The face value varies from one letter to
another, based on the letter’s rarity. (Here are the face values: 1:AEILNORSTU,
2:DG, 3:BCMP, 4:FHVWY, 5: K, 8:JX, 10: QZ.) Write a program that computes
the value of a word by summing the values of its letters:
Enter a word: pitfall
Scrabble value: 12
Your program should allow any mixture of lower-case and upper-case letters in
the word. Hint: Use the toupper library function.

Store the SCRABBLE values of the letters in an array. The array will have 26
elements, corresponding to the 26 letters of the alphabet. For example, element
0 of the array will store 1 (because the SCRABBLE value of the letter A is 1),
element B of the array will store 3 (because the SCRABBLE value of the letter
B is 3), and so forth. As each character of the input word is read, the program
will use the array to determine the SCRABBLE value of that character. Use an
array initializer to set up the array.

17. Write a program that tests whether two words are anagrams (permutations of
the same letters ):
Enter first word: smartest
Enter second word: mattress
The words are anagrams.

Enter first word: dumbest


Enter second word: stumble
The words are not anagrams.
Write a loop that reads the first word, character by character, using an array
of 26 integers to keep track of how many times each letter has been seen. (For
example, after the word smartest has been read, the array should contain the
values 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 2 2 0 0 0 0 0 0,
reflecting the fact that smartest contains one a, one e, one m, one r, two s’s and
two t’s.) Use another loop to read the second word, except this time decrementing
the corresponding array element as each letter is read. Both loops should ignore
any characters that aren’t letters, and both should treat upper-case letters in the
same way as lower-case letters. After the second word has been read, use a third

Page 5
loop to check whether all the clements in the array are zero. If so, the words are
anagrams.
Hint: You may wish to use functions from <ctype.h>, such as isalpha and
tolower.
18. Write a program that declares a 3 × 5 array of int and initializes it to some values
of your choice. Have the program print the values, double all the values, and then
display the new values.
19. Write a program that prompts the user to enter three sets of five double numbers
each. (You may assume the user responds correctly and doesnt enter non-numeric
data.) The program should accomplish all of the following:
a. Store the information in a 3 × 5 array.
b. Compute the average of each set of five values.
c. Compute the average of all the values.
d. Determine the largest value of the 15 values.
e. Report the results.
20. Children often play a memory game in which a deck of cards containing matching
pairs is used. The cards are shuffled and placed face down on a table. The players
then take turns and select two cards at a time. If both cards match, they are left
face up; otherwise, the cards are placed face down at the same positions. Once
the players see the selected pair of cards and if the cards do not match, then they
can memorize the cards and use their memory to select the next pair of cards.
The game continues until all the cards are face up. Write a program to play the
memory game. Use a two-dimensional array of 4 rows and 4 columns to use a
deck of 16 cards with 8 matching pairs and you can use numbers 1 to 8 to mark
the cards. (If you use a 6 by 6 array, then you will need 18 matching pairs, and so
on.) Use random number generators to randomly store the pairs in the array. Use
appropriate functions in your program, and the main program should be merely
a call to functions.
21. (Airplane Seating Assignment) Write a program that can be used to assign
seats for a commercial airplane. The airplane has 13 rows, with six seats in each
row. Rows 1 and 2 are first class, rows 3 through 7 are business class, and rows 8
through 13 are economy class. Your program must prompt the user to enter the
following information:
a. Ticket type (first class, business class, or economy class)
b. Desired seat
Output the seating plan in the following form:
A B C D E F
Row 1 * * X * X X
Row 2 * X * X * X
Row 3 * * X X * X
Row 4 X * X * X X
Row 5 * X * X * *
Row 6 * X * * * X
Row 7 X * * * X X
Row 8 * X * X X *
Row 9 X * X X * X
Row 10 * X * X X X
Row 11 * * X * X *
Row 12 * * X X * X
Row 13 * * * * X *
Here, * indicates that the seat is available; X indicates that the seat is occupied.
Make this a menu-driven program; show the user’s choices and allow the user to
make the appropriate choices.

Page 6
22. Write a program to compute the summations of even and odd numbers in an
array. Initialize the array with 50 random numbers.

23. Write a program to compute the summations of numbers in even and odd indices
in an array. Initialize the array with 50 random numbers.

24. Given two linear arrays A and B of different sizes, filled with random numbers,
write a program that expand either A or B to hold elements of both A and B.
Then copy all elements of A and B to your expanded array.

25. Given a two dimensional array, find out the summation of the boundary elements
of the array. No element should be added twice.

26. Given a two dimensional array find out the summation of the diagonal elements
of the array.

Page 7

You might also like