# B.Tech. (SEM-II) THEORY EXAMINATION 2017-18 COMPUTER SYSTEM & PROGRAMMING IN C

Time: 3hrs
Total Marks: 70

Note:-

• There are three sections. Section A carries 20 marks, Section B carries 30 marks and Section C carries 50 marks.
• Attempt all questions. Marks are indicated against each question.
• Assume suitable data wherever necessary.

# Section – B

2. Attempt any three of the following: (7*3 = 21)

1. Write a program in C to find the largest number of elements in 4*4 matrix.

 `// C program for finding maximum in 4x4 matrix. ` ` `  `#include ` ` `  `#define MAX 100 ` ` `  `// Finds maximum ` `void` `max(``int` `arr[][MAX], ``int` `n) ` `{ ` `    ``int` `max = arr; ` `    ``int` `i, j; ` ` `  `    ``// Traverses rows one by one ` `    ``for` `(i = 0; i < n; i++) { ` `        ``for` `(j = 0; j <= n; j++) { ` ` `  `            ``// Compare elements ` `            ``if` `(arr[i][j] > max) { ` `                ``max = arr[i][j]; ` `            ``} ` `        ``} ` `    ``} ` `    ``printf``(``"Largest number = %d"``, max); ` `} ` ` `  `/* Driver program to test above function */` `int` `main() ` `{ ` `    ``int` `arr[MAX][MAX] = { { 5, 9, 11, 55 }, ` `                          ``{ 1, 25, 0, 14 }, ` `                          ``{ 74, 21, 6, 4 }, ` `                          ``{ 2, 24, 65, 43 } }; ` `    ``max(arr, 4); ` ` `  `    ``return` `0; ` `} `

Output:

```Largest number = 74
```
2. Explain the syntax and use of the following directives with examples:

1. #ifdef: This directive is the simplest conditional directive. This block is called a conditional group. The controlled text will get included in the preprocessor output iff the macroname is defined. The controlled text inside a conditional will embrace preprocessing directives. They are executed only if the conditional succeeds. You can nest these in multiple layers, but they must be completely nested. In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you can’t begin a conditional group in one file and finish it in another.

Syntax:

```#ifdef MACRO
controlled text
#endif /* macroname */
```
2. #undef: The #undef directive is used to undefine an existing macro. This directive works as:

Syntax:

`#undef LIMIT`

Using this statement will undefine the existing macro LIMIT. After this statement every “#ifdef LIMIT” statement will evaluate to false.

3. #pragma: This directive is a special purpose directive and is used to turn on or off some features. This type of directives are compiler-specific i.e., they vary from compiler to compiler. Some of the #pragma directives are discussed below:
• #pragma startup and #pragma exit: These directives helps us to specify the functions that are needed to run before program startup( before the control passes to main()) and just before program exit (just before the control returns from main()).
• #pragma warn Directive: This directive is used to hide the warning message which are displayed during compilation.
We can hide the warnings as shown below:

• #pragma warn -rvl: This directive hides those warning which are raised when a function which is supposed to return a value does not returns a value.
• #pragma warn -par: This directive hides those warning which are raised when a function does not uses the parameters passed to it.
• #pragma warn -rch: This directive hides those warning which are raised when a code is unreachable. For example: any code written after the return statement in a function is unreachable.
4. #include: When we use include directive, the contents of included header file (after preprocessing) are copied to the current file.
Angular brackets < and > instruct the preprocessor to look in the standard folder where all header files are held. Double quotes “ and “ instruct the preprocessor to look into the current folder (current directory).
3. Write short note on:

1. Top down program development approach: This approach refers to the development of program in which the program is divided into sub-programs according to tasks. These tasks are known as modules. C language supports this approach and programmers also believe that this approach is a good way to create a project in a hierarchical manner. In this the main module is developed first, then the other module and so on.
2. Difference between Structure and Array
ARRAY STRUCTURE
Array refers to a collection consisting of elements of homogenous data type. Structure refers to a collection consisting of elements of heterogenous data type.
Array uses subscripts or “[ ]” (square bracket) for element access Structure uses “.” (Dot operator) for element access
Array is pointer as it points to the first element of the the collection. Structure is not a pointer
Instantiation of Array objects is not possible. Instantiation of Structure objects is possible.
Array size is fixed and is basically the number of elements multiplied by the size of an element. Structure size is not fixed as each element of Structure can be of different type and size.
Bit filed is not possible in an Array. Bit filed is possible in an Structure.
Array declaration is done simply using [] and not any keyword. Structure declaration is done with the help of “struct” keyword.
Arrays is a primitive datatype Structure is a user-defined datatype.
Array traversal and searching is easy and fast. Structure traversal and searching is complex and slow.
data_type array_name[size]; struct sruct_name{
data_type1 ele1;
data_type2 ele2;
};
Array elements are stored in continuos memory locations. Structure elements may or may not be stored in a continuos memory location.
Array elements are accessed by their index number using subscripts. Structure elements are accessed by their names using dot operator.
4. Write a Recursive program in “C” language to print Fibonacci series.

 `// Fibonacci Series using Recursion ` ` `  `#include ` ` `  `int` `fib(``int` `n) ` `{ ` `    ``if` `(n <= 1) ` `        ``return` `n; ` `    ``return` `fib(n - 1) + fib(n - 2); ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `n = 9; ` ` `  `    ``printf``(``"%d"``, fib(n)); ` ` `  `    ``return` `0; ` `} `

Output:

```34
```
5. What is algorithm? What are the main steps followed in the development of an algorithm? Write an algorithm for sum of digits in a given number.

Algorithm: An algorithm is used to provide a solution to a particular problem in form of well-defined steps. Whenever you use a computer to solve a particular problem, the steps which lead to the solution should be properly communicated to the computer. While executing an algorithm on a computer, several operations such as additions and subtractions are combined to perform more complex mathematical operations. Algorithms can be expressed using natural language, flowcharts, etc.

Steps to Design an Algorithm:

• Step 1: Fulfilling the pre-requisites

Inorder to write an algorithm, following things are needed as a pre-requisite:

1. The problem that is to be solved by this algorithm.
2. The contraints of the problem that must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output to be expected when the problem the is solved.
5. The solution to this problem, in the given contraints.
• Step 2: Designing the algorithm
• Step 3: Testing the algorithm by implementing it.

Algorithm for sum of digits in a given number:

1. Get the number
2. Declare a variable to store the sum and set it to 0
3. Repeat the next two steps till the number is not 0
4. Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.
5. Divide the number by 10 with help of ‘/’ operator
6. Print or return the sum

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.