Assignment 4
Assignment 4
ASSIGNMENT # 4
Question 1:
Ali is playing a game of cards. This is a special type of card in which a card is a 3 digit number
(i.e. 100 - 999). He has to sort the cards in ascending order. However, sorting will be done on the
sum of the digits. Make a C program to sort his cards, take user input for number of cards. Taking
input of cards number should be a function named get_data( … ). Sorting should be another
function sort( … ) and displaying the final result should be another function display( … ).
Question 2:
Suppose that you are asked to store the data given in the following Table by using three arrays, one
for each column, where the index number is mapped for the record reference. For example the Roll
number in index 0 of the first array corresponds to the name in index0 of the second array and the
student marks are placed in third array on the same index.
Question 3:
You are required to print Pascal’s triangle. Pascal’s triangle is a triangular array of the binomial coefficients.
Take number of rows(n) as input from user and print pattern similar to the one shown for n=6:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
NED University of Engineering & Technology
Department of Computer Science & Information Technology
CS-175: Programming Fundamentals
Fall 2024
Question 4:
The Physics teacher at your university needs help in grading a True/False test. The students’
IDs andtest answers are entered by the user. First input must be the answers to the test in the
form: TFFTFFTTTTFFTFTFTFTT Every other input is in the form: student ID, followed
by a blank, followed by the student’s responses. For example, the entry: ABC54301
TFTFTFTT TFTFTFFTTFT indicates that the student ID is ABC54301 and the answer to
question 1 is True, the answer to question 2 is False, and so on. This student did not answer
question 9. Each correct answer is awarded two points, each wrong answer gets one point
deducted, and no answer gets zero points. Write a program that processes the test data. The
output should display the list containing student’s ID, followed by the answers, followed by
the test score, followed by the test grade.
Question 4:
Someone has asked you to design a special purpose calculator with the following two
options.
a) Solve an equation involving combination of four (+, -, *, /) operators and maximum
100 operands. Assume that all the operators have same precedence and equation is
supposed to be solved from left to right. For example, 3+34.5/2*60-22 is an equation,
and it consists of six operands and five operators. In order to store all the operands and
operators of the equation, define an array of type “struct data” having three members;
two for operands and one for operator. At the first index of the array store the first two
operands (3), (34.5) and the first operator (+). At the next index of the array, store the
result of the last operation (37.5), the next operator (/), and the next operand (2). Call a
user-defined function basic( ) in order to perform a given arithmetic operation on two
operands. Continue this until all the operands and operators of the equation are entered
and you get the final result.
b) Obtain the dot product of two vectors of same size. Note that if A=[1,2,5,2,1] and
B=[4,5,6,3,10], then the dot product can be obtained as A . B = 1*4 + 2*5 + 5*6 + 2*3
+ 1*10 = 60.
In main ( ), ask the user whether she wants to solve an equation or dot product. The user should
press a for choosing equation and b for choosing dot product. If the user enters any character
other than a or b the program should terminate after printing an invalid input message.
If the user chooses a, then ask how many operands are there in the equation. Store the operands
and operators entered by the user in an array of type struct data. In case the user enters an
invalid operator or operands, print the message of invalid input. Otherwise, call a user defined
function basic( ) to get the required result. Print the result by calling display( ) function from
main( ). The program ends only if the user wants to terminate it.
NED University of Engineering & Technology
Department of Computer Science & Information Technology
CS-175: Programming Fundamentals
Fall 2024
If the user chooses dot product by pressing b, then ask her to enter the elements of two arrays.
Call dot( ) function in order to calculate the dot product of two arrays. Print the result in main().
The program ends only if the user wants to terminate it.
For defining basic( ), display( ), and dot( ) functions, please follow the following details.
1) float basic(float, float, char op);
It takes two operands and one operator as input.
It returns the result of applying the given operation on the operands.
2) void display(const struct data [ ], int);
It receives an array of struct data and its size as input.
It prints the information stored in the array.
3) double dot(const double [ ], const double [ ], int);
It receives two arrays of type double and size of the array as input.
It calculates the dot product of two arrays and returns the result.