Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming 4. What is the NOT principle? Why is it important? 6. List the adv

ID: 3752698 • Letter: C

Question

C programming

4. What is the NOT principle? Why is it important?

6. List the advantages and disadvantages of cohesive functions.

7. If given the choice between writing your own function or using a library function, can you think of situations where it would be preferable to write your own?

12. A light year is a unit of astronomical distance that measures how far light can travel in one year, defined as 365.25 days, which is about 9.4507 x 1012, or about 5.878 trillion miles. A parsec is a unit of astronomical distance, equal to about 3.26 light years. One parsec corresponds to the distance at which the mean radius of the earth's orbit subtends an angle of one second of arc. Since the numbers grow so large so quickly, it's often easier for astronomers to work with parsecs, and convert them back to light years, or even miles, when needed. Write a function to convert parsecs to light years.

Explanation / Answer

Q4) What is the NOT principle? Why is it important?

The NOT(operator = !) principle is basically used to flip/reverse the value of an expression so computed. For eg:- if an expression return bool = true, NOT will return the value as false, and vice versa. So it reverses the value of any computed expression. It is important because sometimes in our code we want to enforce negative condition check based on which we decide furthur course of actions to be taken, for eg:-

int isPresent = 0; // initially when we declare

...........; isPresent = 1; // in some code fragment we make it 1 meaning true

if (!isPresent) { // based on the NOT of the computed value, we decide furthur course of actions

}

Q6) List the advantages and disadvantages of cohesive functions

Advantages:-

Disadvantages:-

Q7) If given the choice between writing your own function or using a library function, can you think of situations where it would be preferable to write your own?

We should write our own function when we want to customize the output/modify the output so returned from the built in C Library functions:- eg:- strlen(char *) - will return the length of a string in C

Now we want to return if the string is odd/even length one, then we can write our own version of function based on built in Library function as:-

int isOddLengthString(char* input){

int length = strlen(input);

if (length % 2 != 0)

return 1;

return 0;

}

Please let me know in case of any clarifications required. Thanks!