(For Part 6(a) of this series, pls see here: Functions (Unit 3) – 16 marks – Q & A.)


Anna Univ Exam prep : Previous Years Q & A.
CS 3251 – Programming in C

Nov/Dec 2022, 16 marks, Unit 3 (Functions)

Question:

(a) Classify function prototypes with suitable examples. (8 marks)

(b) Write a program to design scientific calculator using built-in functions. (8 marks)


Part (a) was answered in the previous post.

★ In this post, a discussion related to part (b) is given.

★ Answer for Part (b) will be given in a subsequent post.


Teacher: Before attempting to answer the question, we should first try to understand what the question is trying to say and what we are required to do.
Students: We need to write a program to build a scientific calculator. And we need to use built-in functions.

Teacher: Can anyone suggest some operations our scientific calculator should perform?
Students: Addition,
Subtraction,
Multiplication
and
Division.

Teacher: And how would we do these?
Students: Using arithmetic operators
+ for addition
for subtraction
* for multiplication
/ for division


Teacher: Let’s say we want to perform these operations on two operands (two numbers). What data type would be good to use for these operands?
Students: We can use double. It allows decimal point values with high precision.
We can have something like:
double m, n;
And we can perform:
m + n
m – n
m * n
m / n

Teacher: Anything else we need to keep in mind?
Students: We need to avoid division by zero.


Teacher: Any other operations we can perform?
Students: Exponentiation. Square root.
Teacher: How will we perform exponentiation?
Students: The pow() function is defined in the math.h header file. So, we’ll include the math.h header file and call the pow() function.
Teacher: Good. We are required to use built-in functions.
Students: We can use the built-in pow() function defined in math.h.


Teacher: How do we use the pow() function?
Students: We’ll get two double values — base and exponent — from the user. And we’ll pass them as arguments to pow().

Teacher: Should we include the stdio.h header file too?
Students: Yes, stdio.h is needed for printf() and scanf() functions.


Teacher: How to compute square root?
Students: We’ll use the sqrt() function from math.h. We’ll get a double value from the user, and pass it as the argument to sqrt().

Teacher: Anything else we need to check for?
Students: Square root of a negative number is not allowed. So, we’ll check for that.


Teacher: We’ve been dealing with double values for our program. How do we declare them?
Students: The double keyword is used to declare variables of double data type. E.g. double m, n;

Teacher: How to get user input of double values?
Students: We’ll use scanf() with %lf as the format specifier. E.g. scanf(“%lf”, &m);

Teacher: How to display double values?
Students: We can use printf() with %lf as the format specifier. Usually this displays till 6 decimal places. We can use %.2lf to round it off to 2 decimal places.


Teacher: Any other mathematical operations?
Students: Trigonometric functions like sin, cos.

Teacher: How will we implement them?
Students: We’ll use the built-in functions sin() and cos() from math.h. We’ll take input of radians as a double value from the user.


Teacher: Our scientific calculator will perform the following operations:

  • simple arithmetic operations
  • power
  • square root
  • sin
  • cos

Students: We’ll display all these options to the user, and ask them what operations they wish to perform. Then we can use switch-case to perform the appropriate operation based on the user’s choice.


To summarize, key features of our answer:

★ Include stdio.h and math.h

★ Use variables of double data type (%lf for input, and %.2lf for display).

★ Operations to perform:

  • arithmetic operations
  • square root
  • power
  • sin
  • cos

★ Display menu to user, and ask them which operation to perform.

★ Use switch-case to process user choice and perform appropriate operation.

★ Avoid division by zero, and square root of negative numbers.


(For the next post in this series (Part 6c), pls see here: Program in C for scientific calculator.)

Posted in

One response to “C Programming Question Bank – Part 6(b). CS 3251. Previous Years Q & A. Functions (Unit 3)”

  1. […] C Programming Question Bank – Part 6(b). CS 3251. Previous Years Q & A. Functions (Unit&nb… […]

    Like

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In