0% found this document useful (0 votes)
9 views

Assignment 2 C Programming

The document is an assignment submission that includes 10 multiple choice questions about C programming. It provides the student's name, ID number, and details that the assignment is worth 24 marks and is individual work due on February 8, 2024. It then lists each question, the number of marks for each, and the student's answers. The questions cover topics such as why fflush(stdin) is used, scanf return values, preprocessor directives, converting a math formula to code, bitwise operations in hexadecimal, GPIO meaning, code style issues, and for/do-while loop conversions.

Uploaded by

Saurabh Kamboj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment 2 C Programming

The document is an assignment submission that includes 10 multiple choice questions about C programming. It provides the student's name, ID number, and details that the assignment is worth 24 marks and is individual work due on February 8, 2024. It then lists each question, the number of marks for each, and the student's answers. The questions cover topics such as why fflush(stdin) is used, scanf return values, preprocessor directives, converting a math formula to code, bitwise operations in hexadecimal, GPIO meaning, code style issues, and for/do-while loop conversions.

Uploaded by

Saurabh Kamboj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name : Saurabh Kamboj Student Id : 991718738

Assignment #2: Touch of Everything


DUE: Thursday February 8, 2024

Individual effort

Total marks: 24 marks (1.5% of your final mark)

1. (2 marks) Why do we need to use fflush(stdin) before each scanf statement?

Ans : fflush(stdin) is typically used for output stream only. Its purpose is to clear (or flush) the output
buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).

2. (4 marks) What would the value of check be in the following code snippet if someone entered the
following values for num1, num2, and num3 respectively: 34.5, 66, and 75.

Also: What would num1, num2, and num3 contain after the scanf statement?

int num1 = 1, num2 = 2, num3 = 3;

int check;

printf(“Please enter 3 integers values\n”);

fflush(stdin);

check = scanf(“%d%d%d”,&num1, &num2, &num3);

Ans : The program attempts to read three integer values from the user input.However, scanf encounters a
problem when trying to read the floating-point value 34.5 because %d expects integers. Consequently,
scanf stops processing further and returns. As a result, the variable check holds the return value of scanf,
which indicates the number of successful conversions. Since scanf fails to read all three integers, check will
be 0.After scanf returns, the variables num1, num2, and num3 retain their initial values (1, 2, and 3
respectively) since scanf couldn't assign new values to them.Therefore, the value of check would be 0, and
num1, num2, and num3 would remain unchanged (1, 2, and 3 respectively).

3. (2 marks) What is a preprocessor directive and how do you start a preprocessor directive?
Ans: A preprocessor directive is a command used in C and C++ programming languages to instruct the
preprocessor, a program that runs before compilation, to perform specific tasks such as including header
files, defining macros, and conditional compilation.
To start a preprocessor directive, you use the # symbol followed by the directive name. Preprocessor
directives are not terminated by a semicolon (;). For example :
#include <stdio.h> // This is a preprocessor directive to include the standard input-output library
In this example, #include is a preprocessor directive, and <stdio.h> is an argument that specifies the
header file to be included. This directive tells the preprocessor to insert the contents of the stdio.h file into
the current source file before compilation.

1
4. (3 marks) Write the following math formula into a C language statement (just one line of code):

𝑄𝑄𝑥 = 𝑅1 + 𝐴2 𝑥 4
Ans : QQx = R1 + A2 * pow(x, 4);

5. (3 marks) Determine the value of the variable a at the end of the code snippet in hexadecimal (show
your work by hand, but you can use the C-compiler to double check):
int i;
unsigned char a = 8;

for(i=0;i<2;i++)
{
a = a >> 1;
}
a = a ^ 0xF5;
printf(“The value of a is %i”,a);

Ans : In this program we have Initialize an integer variable i and declare an unsigned char variable a and
initialize it to 8 (In binary number system it would be equivalent to 0000 1000 ).when for loop starts it
initialize i equals to 0 and loop will terminate while i is less than 2.
In each iteration of the loop, a would be right-shifted by 1 bit (a = a >> 1). This effectively divides a by 2.
 After the first iteration: a becomes 0000 0100 (which is 4 in decimal).
 After the second iteration: a becomes 0000 0010 (which is 2 in decimal).
a ^ 0xF5 will Perform a bitwise XOR operation between a = 0000 0010 (which is 2 in decimal) and 0xF5 =
1111 0101
a : 0000 0010
0xF5 : 1111 0101
---------------------
Result : 1111 0111
So, after the operation a = a ^ 0xF5, the value of a will be the result of the bitwise XOR operation.
Which is 1111 0111 in binary Number system, equivalent to 0xF7 in hexadecimal.hence a would be equal
to 0xF7 in hexadecimal.

2
6. (1 mark) What does GPIO stand for?
Ans: GPIO stands for "General Purpose Input/Output."

7. (2 marks) What is wrong with the following code (code works fine but there is a style problem)?
do
{
printf(“Please enter your shopping receipt value:\n”);
Error = scanf(“%f”,&Bill);
if((Error!=0)&&(Bill>0.00)&&(Bill<1000.00)) Tip = Bill * Rate;
} while((Error==0)||(Bill<0.00)||(Bill>1000.00));
Ans : Inconsistent use of curly braces: The if statement lacks curly braces, which can lead to confusion
and potential errors when modifying the code later. Curly braces should be added around the body of the if
statement to ensure it's executed as a single block of code.
Inconsistent indentation: The indentation is not uniform, making it harder to understand the code's
structure. Consistent indentation must be ensured throughout the code to improve readability and
maintainability.

8. (3 marks) Convert the following code to work within a do...while loop.

int i, count = 0;
float Var1 = 25.2;

for(i=0;i<100;i+=2)
{
count++;
printf(“The iteration number of this loop is %d”, count);
printf(“The new value is currently set to: %.3f”, Var1*=(float) i);

Ans: int i = 0, count = 0;


float Var1 = 25.2;

do {
count++;
printf("The iteration number of this loop is %d\n", count);
printf("The new value is currently set to: %.3f\n", Var1 *= (float)i);
i += 2;
} while (i < 100);

9. (2 marks) Both of the following code snippets do the same thing, summing even numbers between 0
and 500, but which one is better and why?

Style 1: Style 2:

for(i=0;i<=500;i++) for(i=0;i<=500;i+=2)
{ {
if(i%2) Sum = Sum + i; Sum = Sum + i;
} } 3
Ans:The Style 2 is better than Style 1 because of the following reasons:
Efficiency: The style 2 code snippet directly iterates through even numbers by incrementing i by 2 (i+=2).
This means it only performs half as many iterations compared to the style 1 code snippet, which iterates
through all numbers and then checks if each number is even (if(i%2)). Avoiding unnecessary checks for odd
numbers makes the style 2 snippet more efficient.
Clarity and Readability: The style 2 code snippet is clearer and more readable because it explicitly states
its intent to sum even numbers by incrementing i by 2 in each iteration. This direct approach makes it
easier to understand the purpose of the loop. On the other hand, the style 1 code snippet requires
additional mental processing to understand that it's summing even numbers due to the conditional check
if(i%2).
Maintenance: The style 2 code snippet is also easier to maintain because its logic is straightforward and
less prone to errors. If someone needs to modify or debug the code in the future, they can quickly grasp its
purpose and operation. In contrast, the Style 1 code snippet's conditional check might lead to confusion or
oversight during maintenance.

10. (2 marks) Which style is better (explain) – think in terms of security, readability, and ease:

Style 1: if(!End)
Style 2: if(End != 0)
Style 3: if(0 != End)

Ans : In terms of security, readability, and ease, Style 1: if(!End) is generally the preferred choice.To
explain:

 Security: All three styles are equally secure, as they check if End is non-zero. Style 1 is preferred
due to its common usage.
 Readability: Style 1 (if(!End)) is clearest, directly indicating a check for false or zero.
 Ease: Style 1 is simplest and widely understood, reducing complexity and potential confusion.

In summary, Style 1 (if(!End)) is clearer, more commonly used, and simpler to understand, making it the
preferred choice. It directly expresses the intent of checking whether End is false or zero, improving
readability. Additionally, it's a straightforward and familiar idiom in C programming, reducing the
likelihood of errors and confusion.

You might also like