C Language Functions: Library Functions vs User-Defined Functions, Lecture notes of Food Science and Technology

The concept of functions in C language, focusing on the differences between library functions and user-defined functions. It covers the syntax and examples of various types of functions, including those with no input and no return value, input and no return value, input and return value, and recursive functions.

Typology: Lecture notes

2021/2022

Uploaded on 08/01/2022

hal_s95
hal_s95 🇵🇭

4.4

(655)

10K documents

1 / 50

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Computer Science Questions and Answers
UNIT-A
Chapter - 1 Configuring a Computer
I One Mark Question and Answer
1. Name the components of CPU
Ans. a) ALU b) PC c) Accumulator d) MAR e) IR f) ID g) MDR
2. What is the use of registers in CPU?
Ans. It is used to store intermediate data and instructions.
3. What is the function of MAR?
Ans. It specifies the address of memory location from which data or instruction is to be accessed
or to which the data is to be stored.
4. What is the function of MDR / MBR?
Ans. It contains the data to be written into or read from memory location addressed by MAR.
5. What is program counter?
Ans. It keeps track of the memory address of the instruction that is to be executed next.
6. What is the function of IR?
Ans. It contains the instruction that is being executed.
7. What is the purpose of Instruction Decoder?
Ans. It decodes the instructions.
8. What is a data bus?
Ans. It carries a word to or from memory.
9. What is an address bus?
Ans. It carries memory address. The width of address bus equals the number of bits in the
MAR.
10. What is the function of control bus?
Ans. It carries signals between the units of the computer.
11. Name different I/O ports.
Ans. Port is a socket on the back of computer. Different types of ports are serial ports, parallel
ports and USB ports.
12. What is meant by plug and play cards?
Ans. It enables the user to add devices to computer.
13. Expand SD RAM.
Ans. Synchronous Dynamic Random Access Memory.
14. Expand DDR RAM.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32

Partial preview of the text

Download C Language Functions: Library Functions vs User-Defined Functions and more Lecture notes Food Science and Technology in PDF only on Docsity!

Computer Science Questions and Answers UNIT-A Chapter - 1 Configuring a Computer

I One Mark Question and Answer

1. Name the components of CPU Ans. a) ALU b) PC c) Accumulator d) MAR e) IR f) ID g) MDR 2. What is the use of registers in CPU? Ans. It is used to store intermediate data and instructions. 3. What is the function of MAR? Ans. It specifies the address of memory location from which data or instruction is to be accessed or to which the data is to be stored. 4. What is the function of MDR / MBR?

Ans. It contains the data to be written into or read from memory location addressed by MAR.

5. What is program counter?

Ans. It keeps track of the memory address of the instruction that is to be executed next.

6. What is the function of IR?

Ans. It contains the instruction that is being executed.

7. What is the purpose of Instruction Decoder?

Ans. It decodes the instructions.

8. What is a data bus?

Ans. It carries a word to or from memory.

9. What is an address bus?

Ans. It carries memory address. The width of address bus equals the number of bits in the MAR.

10. What is the function of control bus?

Ans. It carries signals between the units of the computer.

11. Name different I/O ports.

Ans. Port is a socket on the back of computer. Different types of ports are serial ports, parallel ports and USB ports.

12. What is meant by plug and play cards?

Ans. It enables the user to add devices to computer.

13. Expand SD RAM.

Ans. Synchronous Dynamic Random Access Memory.

14. Expand DDR RAM.

Ans. Double Data Rate Random Access Memory. II Two Marks Questions.

1. Explain USB Port (Universal Serial Bus).

Ans. a. It is a high speed serial bus b. It is a plug and play interface between computers and add on devices.

2. Give the specifications of Laser printer.

Ans. a. Resolution varies from 600 to 1200 dots per inch. b. Speed varies from 4 PPM to 24 PPM. c. Buffer size varies from 4MB to 32 MB d. Can print color images.

3. Give the specifications of dot matrix printer.

Ans. a. Print head normally has 9 pins to 24 pins. b. Speed varies from 30 to 300 characters per second. c. Buffer size varies from 1K to 64 K.

4. Give the specifications of inkjet printer.

Ans. a. Resolution is around 300 dots per inch. b. Speed varies from 1PPM to 12 PPM c. Buffer size varies from 1 MB to 4 MB.

III Five Marks Questions:

1) Write a note on computer maintenance.

Ans : a. Avoid heat dust and noise b. Periodic updates and enhancements c. Installing fire walls and anti virus. d. In house software maintenance e. Computer periodic checkup. 2) What are the possible threats to a computer system and how to provide security?

Ans : Threats a. Privacy b. Integrity c. Environmental damage d. Human threats e. Software threats f. Unauthorized access g. Computer viruses Security a. Physical protection of machine and media. b. Giving passwords and users ID c. Using Licensed software d. Use of Cryptography e. Use of Spike busters and UPS 3) Explain the typical causes of computer failures.

Ans: a. Break down of components. b. Excessive dust or humidity c. Virus d. Voltage fluctuation

  • Ans : CPU Speed/Clock speed: Speed of CPU also known as clock speed. The clock speed is the number of instructions executed by the CPU in one second It is measured in megahertz(million instructions per second). The average speed of a new CPU is about 1000MHz to 4000 MHz.(1 to 4 giga hertz).
  • Instruction set: The number of instructions decide the efficiency of a CPU. More the instructions , less efficient is the CPU and less the instructions, More efficient is the CPU.
  • Word size/Register size: The size of registers determines the amount of data the computer can work with at a time. Normally it is 32 bits. Smaller the size of register, slower will be the computer.. It is also known as Word size. It varies from 16 bits to 128 bits.
  • Data bus capacity: Width of a data bus determines the largest number of bits that can be transported at one time.
  • Cache Memory size: Cache memory is a high speed memory. Greater the cache, faster a processor runs. Most modern processors can execute multiple instructions per clock cycle which speeds up a program. Some CPU’s have storage for instructions and data built inside the processor chip. This is called internal cache or L1 cache memory.
  • Memory Size: The amount of PRIMARY STORAGE (RAM) determines the size of program that can be kept in primary storage, which is faster than secondary storage. There by the speed of computer increases. The size of RAM varies from 64 MB to 4 GB. 8. Explain the General Structure of CPU.

Ans : Program counter: This register stores address of next instruction to be executed. Memory address register (MAR): This register specifies address in memory where information can be found. This register is also used to point to a memory locations where information can be stored.

Memory buffer register: This register acts as an interface between CPU and memory. When CPU issues a Read Memory command, instruction is fetched and placed in MB register.

Instruction register -This stores a copy of current instruction.

Instruction decoder (ID): ID is an electronic hardware, which decodes instructions. The instructions are further broken down into a set of micro operations, so that they can be executed directly.

General purpose registers: They are used to store data. There are 6 GP registers. These are used for storage of data as needed by the program. Arithmetic logic unit: The basic arithmetic operations such as addition,subtraction,multiplication and division are carried out here. These operations need atleast two operands, one which is stored in accumulator and the other in the MBR. After the manipulation of data in ALU, the result is transferred to accumulator. Accumulator : During processing the intermediate data needed for future processing is stored in accumulator. The contents of ACC are used by ALU for operations and later by MBR holds the final result for further action.

Chapter - 2 Problem Solving Techniques UNIT-B

I One Mark Question and Answer

1) Define Sorting.

Ans. Sorting is a method of arranging data items in any order.

2) What is searching?

Ans: It is a process of locating an element stored in a file.

3) Mention the different structured programming constructs.

Ans: a. Sequence b. Selection c. Iteration 4) What is stepwise Refinement?

Ans: Is a process of breaking down the problem at each stage to obtain a solutions.

5) Name the different searching methods.

Ans: a. Linear Search b. Binary Search 6) Define module.

Ans: It is an independent set of statements, which can be called in another program.

7) What is the main advantage of linear search method?

Ans. It is simple and useful when the elements to be searched are not in any definite order.

8) What is the other name of bubble sort?

Ans: The bubble sort method is also called as sorting by exchange.

9) What is structured programming?

Ans: It is a method of using the concept of sequence , selection , iteration and modularity.

10) Name the searching technique which requires sorted elements.

Ans: Binary Search.

11) What is top-down analysis?

Ans: Solving a problem by breaking it up into smaller parts is called as top-down analysis.

12) What is top-down approach called as?

Ans: Top-down analysis is also called as stepwise refinement

13) What is a module?

Ans: Each program segment is called a module.

b. Structured programs are easy to test and debug.

III Five Marks Questions and Answers:

1) Mention the objectives of structured programming.

Ans: a. To produce error free program. b. To incorporate basic structured constructs. c. To eliminate use of GOTO Statements. d. To obtain a disciplined approach towards programming. e. To improve the flexibility of a program. 2) Explain top-down analysis with an example.

Ans: The approach of dividing a problem into sub problems and dividing the sub problems until a solution is obtained is called top-down analysis. Ex:

3) Write an algorithm to sort an elements using Insertion Sort. Ans: Algorithm for Insertion sort Step 1 : START Step 2 : Input N Step 3 : for I← 0 to N - Read A [ I ] ( end of I for loop ) Step 4 : For I ← 1 to N -1 Do Step 5 : J = I Step 6 : While ( J > = 1 ) and ( A[ J ] < A [ J -1 ] ) Step 7 : TEMP = A [ J ] Step 8 : A [ J ] = A [ J - 1 ] Step 9 : A[ J - 1 ] = TEMP Step 10 : J = J - 1 [ end of While loop ] [ end of step I For loop ] Step 11 : for I ← 0 to N - Step 12: Print A [ I ] Step 13 : Stop

Consider the elements A[0 ] A[1] A[2] A [3]

20 16 4 3

Process Output

SI ← ( P * T * R ) 100

Input

SIMPLE INTEREST

P T R (^) SI P^ T (^) R

Pass 1: A[1],A[0]>16,20,4, Pass 2: A[2],A[1]> 16,4,20, A[1],A[0]>4,16,20, Pass 3: A[3],A[2]> 4,16,3, A[2],A[1]>4,3,16, A[1],A[0]> 3,4 16,

Sorted order is 3,4,16,

4) Explain bubble sort algorithm with example.

Ans: Algorithm for bubble sort Step 1 : START Step 2 : Input N Step 3 : for I← 0 to N - Step 4 : Read ( A [ I ] ) ( end of for loop ) Step 5 : for I← 1 to N - Step 6 : for J ← 0 to N –I - 1 Step 7 : If (A[ J ] > A [ J +1] ) Temp ← A [ J ] A [ J ] ← A [ J + 1 ] A [ J +1 ] ← Temp [ end of if ] [ end of J loop ] [ end of I loop ] Step 8 : for J ← 0 to N - Print A [ J ] Step 9 : Stop Consider the following elements: 50 , 20, 10 , 05 Pass 1 Pass 2 Pass 3 50 20 20 20 10 10 20 50 10 10 20 05 10 10 50 05 05 20 05 05 05 50 50 50

Sorted order is 5, 10, 20 , 50 5) Explain selection sort with an example.

Ans: Slection sort is based on finding the smallest element in the list and placing it at the first position. Then the next element is found and placed at the second position and so on. Consider the elements 20,16,4, Pass 1: 20 3 16 16 4 4 3 20

Step 9: High ← M – 1 Step 10 : Else Step 11 : Low ← M + 1 [ end of if ] [ end of While loop ] Step 12 : if ( Loc > = 0 ) Then Step 13: Print “ ele , found , search successful ” , Loc Step 14: else Step 15 : Print “ Item not found , search Unsuccessful ” [ end if ] Step 16 : Stop

7) Write the steps involved in performing binary search operation to search an element 56 in the following numbers.

Ans: a.

0 1 2 3 4 5 6 Assuming ‘ a’ is the name of the array

initial values:

n = 7 low = 0 high = n – 1 = 6 mid = [ low + high ] / 2 = ( 0 + 6 ) / 2 = 3 S = 56 [ search element ] a [ mid ] = 67 Step 1 : Compare the element in a [ mid ] and search element 56 67 is not equal to 56 Step 2 : Check whether search element comes after or before the mid index element Since 56 < 67 [ The search element is lesser than 67 , then the search element lies to the left of mid point ] Step 3 : Change high = mid – 1 = 3 – 1 = 2 mid = ( low + high ) / 2 = ( 0 + 2 ) / 2 = 1 Step 4 : Compare a [ mid ] and S for equality Step 5 : Since 56 is greater than 48 [ If ( S > a [ mid ] ) i.e. 56 > 48 ] Step 6: Change low = mid + 1 = 1 + 1 = 2 mid = ( low + high ) / 2 = ( 2 + 2 ) / 2 = 2 Step 7: Compare a [ mid ] and S for equality. Since S and a [ mid ] are equal search is successful and the element is found at the location mid i.e. 2

32 48 56 79 82 99

32 48 56 67 79 82 99

8) Write an algorithm to find the maximum in an array

Ans: Step 1 : [Assume 1st^ element is the largest ] large = A [ 0 ] Step 2 : POS = 0 Step 3 : [ Find the largest element in the array and its position] For I = 1 to N – 1 Do Step 4 : If ( A [ I ] > large ) Then Step 5 : large = A [ I ] Step 6 : POS = 1 [ End if ] [ End of step 3 for loop ] Step 7: [ Print the largest and its position] Print “ Largest = “ , large , “ position = “ , pos Step 8: Exit 9) Write an algorithm to find the minimum in an array

Ans: Step 1 : [Assume 1st^ element is the smallest] small = A [ 0 ] Step 2 : POS = 0 Step 3 : [ Find the smallest element in the array and its position] For I = 1 to N – 1 Do Step 4 : If ( A [ I ] < small ) Then Step 5 : small = A [ I ] Step 6 : POS = 1 [ End if ] [ End of step 3 for loop ] Step 7: [ Print the smallest and its position] Print “ smallest = “ , small , “ position = “ , pos Step 8: Exit

10) Explain linear search method with an algorithm.

Ans: Step 1 : START Step 2 : input N Step 3 : for I← 0 to N - Read A [ I ] Step 4 : Loc = - Step 5 : For I = 0 to N- 1 do Step 6 : If ( ele = A[I]) Then Step 7: Loc = I Step 8 : Goto step 9 [ end if ] [ end for ] Step 9 : If ( Loc > = 0 ) then Step 10: Print “ ele Found in location “ , Loc Step 11: else Step 12: Print “ ele not found ” Step 13 Stop

To print elements of an array a for loop is used. Ex: int a[10]; For(i=0;i<10;i++) Printf(“%d\n”,a[i]);

  1. How two dimensional arrays are initialized?
  • initialization at design time- the data element are stored in to the array at the time of declaration of the array. The elements of each row and each column are represented within the pair of flower brackets. Ex: int a[3][3]={1,3,5,3,7,82,5,8};
  • initialization of the array at the time of execution- to input data in to two dimensional array, two for loops are used foe every value of the outer loop index. The inner loop is executed a specified number of times. Ex: int a[4][5]; For(i=0;i<4;i++) For(j=0;j<5;j++) Scanf(“%d”,&a[i][j]);
  1. How do you print a matrix? Give ex.

A two dimensional array can be printed as follows- Int a[3][4]; For(i=0;i<3;i++) { For(j=0;j<4;j++) { Printf(“%d\t”,a[i][j]); } Printf(“\n”); }

STRINGS

Question carrying one mark:-

  1. What is a string?

A string is a sequence of one or more characters.

  1. Write the syntax for declaration of a string.

syntax: char arrayname [size]; Ex: char str [100];

  1. State the difference between a null character and a null sting.

A null character indicates the end of the string. A null string is a string with zero characters or no characters.

  1. Which header file is needed to perform string operation?

#include<string.h>

  1. Which header file is needed to perform character operations? #include<ctype.h>

  2. Write an example for string initialization. char str[100] ={“MYSORE CITY”};

  3. What is the difference between gets() and scanf() function? gets() function receives a string input up to the end of the line or a new line character. This means the string may have blank spaces and punctuations. Scanf() function accepts a string only up to the first blank space or punctuation.

Question carrying 5 marks :-

  1. Explain the various string operations with examples.

(note:Write any 5 string operations with syntax and example)

  • strupr() – this function converts a string to upper case. syntax: strupr(str1); Ex: strupr(“hello world”)= HELLO WORLD
  • strlwr() – this function converts a string to lower case. Syntax: strlwr(str1) Ex: strlwr(“BANGALORE”)=Bangalore
  • strlen() – this function returns the length of the string. The function returns the number of characters in the strings. Syntax: strlen(str1) Ex: strlen(“ hello world”)=
  • strcat()- this function concatenates(combines) two or more stings in to a single string. strcat(str1,str2) Ex: x=”beau” y=”tiful” strcat(x, y)=beautiful
  • strrev()-this function reverses the string. Syntax:strrev(str1) Ex: strrev(“hello”)=olleh
  • strcpy()-this function copies a string to a variable. Syntax:strcpy(str1,str2) Ex: strcpy(x,”moonlight”) x=”moonlight”
  • strcmp()- this function compares the two syrings and returns the ascii difference between the two strings. This is case sensitive. Syntax:Strcmp(str1,str2) Ex: strcmp(“their”,”there”)=-
  1. What is a user defined function? A user-defined function is a complete and independent program unit, which can be used(or invoked) by the main program or by other sub-programs.
  2. Mention the need for a function If there are a number of statements that are repeatedly needed in the same program at different locations,then a function may be used.
  3. What do you mean by a calling function? Once the function is created it can be called from the main program or from any other function. This main program or the function which calls another function is called calling function.
  4. What is a called function? The user-defined function which is called by the main program or from any other function is known as called function.
  5. What does the return-type-specifier of a function identify? It identifies the type of value, which will be sent back after the function has performed its task (eg: int , float ).
  6. Why is the return type void used? The return type void is used if the function does not return any value to the calling function.
  7. What is an argument? Information or values are passed to the function through special identifiers are called arguments.
  8. What are actual parameters? The arguments (values) which are passed to the function when a function call is made are called actual parameters.
  9. What are formal parameters? The arguments which are used in the argument list of the function header to receive the values from the calling program are called formal parameters or dummy parameters.
  10. Give the syntax of a function call? A function call has the following syntax: variable = function_name(arg1,arg2…); where arg1, arg2… are the actual parameters.
  1. How is a function invoked? A function is invoked(or called) through an output statement or through an assigement statement by using the function name followed by the list of arguments. Ex: p = prod(x, y);
  2. What is the use of the return statement? The return statement is used in the body of the function which contains the value that has to be sent back to the main program.
  3. Give the syntax of return statement? The return statement can take one of the following forms return; Or return(expression); The first form does not return any value, where the second function returns the value of the expression.
  4. What are local variables? Variables declared inside a block or function are said to belong only to that block and these are called as local variables. Values of these variables are valid only in that block.
  5. What are global variables? Variables declared before the main function block are called global variables. Values of these variables are available in every block of that program.
  6. What is function prototype? A function must be declared before it is actually called. The declaration of a function is known as function prototype.
  7. Give the syntax of a function prototype. return-type function-name (type1, type2); Ex: float power(float, int);
  8. Define the term recursion. A function which calls itself directly or indirectly again and again until a condition is satisfied is called as recursive function. This process is called recursion.
  9. How do we pass an entire array to a function? To pass an entire array to a called function only the array name without a subscript is used as argument(actual parameter). Ex: void main( ) { int a[100], n; ..........
  1. Why is the return statement is required in a function body? When a function is called control is transferred from the calling function (main program) to the called function (sub-program). The statements of the called function are executed and finally the called function is terminated and sends the required result back to the calling function. The return statement is used to send the result from the function to the main program, thus terminating the called function. The syntax is: return; Or return(expression); The first form does not return any value, where the second function returns the value of the expression.
  2. What is a function prototype? When is a function prototype necessary? The declaration of a function before it is used is called as function prototype. A function prototype contains the data type of arguments sent as input to the function and data type of the result sent back from the function Syntax: data_type function_name(data_type1,data_type2,….); Ex: float power(float, int); 5 mark questions
  3. Explain user defined function with an example User defined function is a subprogram that is called by a main() function or any other function. A user defined function may appear before or after the main() function. The general syntax of function definition is as follows:

Return_type_ specifier Function_name (type arg1, type arg2….) { local variable declarations; argument list statement1; statement2; … Body of the function statement n; return (expression); }

Return data type- indicates the data type of variable sent back from called function to calling function Function name- indicates the name of the function .It is an identifier Argument list- list of input variables and their datatypes received from calling function

Local variable declaration-list of data that required only for the function block in which they are declared. Body of the function - It includes declaration part (local variables) and a set of executable statements which will perform the required task. Return statement- indicates termination of function and transfer of control from called

function to calling function

Ex: int sum(int a,int b)

int c; c=a+b; return(c); }

  1. Mention different types of functions. Explain any one(or any specific type may be asked) Note: The explanation should include syntax and example The different types of functions are
  2. functions with no input and no return value
  3. functions with input and no return value
  4. Functions with input and return value
  5. Recursive functions
  6. functions with no input and no return value- In this type of functions ,the user defined function does not receive any input and does not send back any result to the calling function.

Syntax: