Fundamentals of Computer Programming 2073

Tribhuwan University
Institute of Science and Technology
2073
Bachelor Level / First Semester / Science
Computer Science and Information Technology ( CSC-102 )
( Fundamentals of Computer Programming )
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to give their answers in their own words as far as practicable.
The figures in the margin indicate full marks.

Attempt all the questions.

1. Draw a flow chart and write an algorithm to find out whether a given number is zero or +ve or -ve.

6 marks view

Algorithm:

1.      Start

2.      Print”Enter a number”.

3.      Read n.

4.      If n>0 then print “The number is positive”

Else if n<0 print “The number is negative”

Else “The number is zero”.

5.      Stop

 Flowchart:


2. What are the different types of operators available in C?Explain.

6 marks view

An operator is a symbol that operates on single or multiple data items. It is used in program to perform certain mathematical or logical manipulations. E.g. In a simple expression 2+3, the symbol “+” is called an operator which operates on two data items 2 and 3.

C operators can be classified into following types:

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations. There are five arithmetic operators:


2. Relational Operators

- Relational operators are used to compare two operands and taking decisions based on their relation.

-  Result of relational expression is either True(1) or False(0).

-  Relational operators are used in decision making and loops.

-  Relational operators are:


3. Logical Operators

- Logical operators are used to compare logical and relational expression.

- The operands of logical operators must be either Boolean value (1 or 0) or expression that produces Boolean value.

-  The output of these operators is always 0 (flase) or 1 (true).

The logical operators are:  


4. Assignment Operator

-  Assignment operators are used to assign the result of an expression to a variable.

-  The mostly used assignment operator is ‘=’. 

-   C also supports shorthand assignment operators which simplify operation with assignment.


5. Increment and Decrement Operators

- Increment operator is used to increase the value of an operand by 1.

- Decrement operator is used to decrease the value of an operand by 1.  


6. Conditional Operator (Ternary Operator)

-  It takes three arguments.

- Conditional operators return one value if condition is true and returns another value if condition is false.

Syntax: (condition) ? value_if_true : value_if_false


etc.

3. Describe the four basic data types with example.

6 marks view

 Data type in C refers to an extensive system used for declaring variable for function of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

The Four basic data types are described below:

1.      Integer type (int):

  •      Integers are whole numbers.
  •      It requires 16 bit of storage i.e. 2 bytes.
  •      Three classes of integer: Integer(int)Short integer (short int) and Long integer (long int)


E.g. int a;    int x=5;

 

2. Floating point type (float):

  • Floating point type are fractional numbers.
  • A variable of float type requires 4 bytes and the range of values that can be stored in it, is 3.4e-38 to 3.4e+38.\\
  • Variable is defined as:

float a;

float x=23.7;

 

3. Character type (char):

  •      All single character used in programs belong to character type.
  •      The character data type holds exactly 8 bits (1 byte).
  •      The unsigned char has values between 0 and 255.
  •      The signed char has values from -128 to 127.
  •      Variable is declared as:

char var1= ‘h’;

            Here, var1 is a variable of type char which is storing a character ‘h’.

 

4. Double precision floating point type (double):

     i. Double precision:

  •          It reserves 8 bytes in memory.
  •          It represents fractional number of the range 1.7e-308 to 3.4e+308

   ii. Long double precision:

  •          It reserves 10 bytes in memory.
  •          It represents fractional numbers of the range 3.4e-4932 to 1.1e+4932

4. Differentiate between while and do while loop with example.

6 marks view

Difference between while and do while loop

1. The while loop is pre-test loop, where firstly the condition is checked and if the condition is true then only the statements of the while loop execute. The do-while loop is a post-test loop. In the do-while loop, the statements of the do-while loop are executed after that, the condition is evaluated, and if the condition is true then again the statements of the do-while loop are executed.

2.

Syntax of while loop:

while (condition)

 {

   // statements

 }

Syntax of the do-while loop:

do

 {

   // statements 

 } while(condition);

3. The condition of the while loop is at the top of the loop but the condition of the do-while loop is at the bottom of the loop.

4. While loop can’t be terminated with a semicolon but the do-while loop should be terminated with a semicolon.

5. The statements of the do-while loop execute at least 1 time in every condition. In the while loop, the test expression evaluates false in first checking then the statements of the while loop is not executed. But the condition of the do-while loop is checked at the end of the loop, so it is guaranteed that the statements of the do-while loop execute at least once.

Example of while loop:

#include<stdio.h>

 int main()

 {

   int m,n;

   printf("Enter range: ");

   scanf("%d %d",&m, &n);

   while (m<=n)

   {

     if(m%2==0) 

        printf("%d ",m);

     ++m;

   }

   return 0;

 }

Example of do while loop:

 #include<stdio.h>

 int main()

 {

   int m,n;

   printf("Enter range: ");

   scanf("%d %d",&m, &n);

   do

   {

     if(m%2==0)

       printf("%d ",m);

     ++m;

   } while(m<=n);

   return 0;

 }

5. Write a program to check whether the diagonal elements of a (4*4) matrix are all Zero.

6 marks view

#include<stdio.h>

void main()

{

    int mat[4][4];

    int i,j,flag;

    printf("Enter the elements of the matrix\\n");

    for(i=0;i<4;i++)

    {

        for(j=0;j<4;j++)

        {

            scanf("%d",&mat[i][j]);

        }

    }

    printf("The matrix\\n");

    for(i=0;i<4;i++)

    {

        for(j=0;j<4;j++)

        {

            printf("%d\\t",mat[i][j]);

        }

        printf("\\n");

    }

    for(i=0;i<4;i++)

    {

        for(j=0;j<4;j++)

        {

            if(i==j)

            {

                if(mat[i][j]==0)

                    flag=0;

                else

                    flag=1;

            }

        }

    }

    if(flag==1)

        printf("The diagonal elements of matrix are not zero");

    else

        printf("The diagonal elements of matrix are all zero");

}

6. What is the function of a pointer variable? Explain the declaring and initializing pointers with example.

6 marks view

7. Write a program using pointers to read in an array of integers and print its elements in reverse order.

6 marks view

#include<stdio.h>

#include<conio.h>

#define MAX 30

void main()

{

  int size, i, arr[MAX];

  int *ptr;

  clrscr();

  ptr=&arr[0];

  printf("Enter the size of array : ");

  scanf("%d",&size);

  printf("Enter %d integers into array:n",size);

   for(i=0;i<size;i++)

   {

   scanf("%d",ptr);

   ptr++;

   }

  ptr=&arr[size-1];

  printf("Elements of array in reverse order are:n");

  for(i=size-1;i>=0;i--)

   {

   printf("nElement%d is %d :",i,*ptr);

   ptr--;

   }

  getch();

}

8. Differentiate between call by value and call by reference with example.

6 marks view

In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. 

In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.

In call by value, actual arguments will remain safe, they cannot be modified accidentally whereas In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.

Example using Call by Value:

#include <stdio.h>

void swapByValue(int, int); /* Prototype */

int main() /* Main function */

{

  int n1 = 10, n2 = 20;

  /* actual arguments will be as it is */

  swapByValue(n1, n2);

  printf("n1: %d, n2: %d\\n", n1, n2);

} 

void swapByValue(int a, int b)

{

  int t;

  t = a; 

  a = b; 

  b = t;

}

Example using Call by Reference:

#include <stdio.h>

void swapByReference(int*, int*); /* Prototype */

int main() /* Main function */

{

  int n1 = 10, n2 = 20;

  /* actual arguments will be altered */

  swapByReference(&n1, &n2);

  printf("n1: %d, n2: %d\\n", n1, n2);

}

void swapByReference(int *a, int *b)

{

  int t;

  t = *a; 

  *a = *b; 

  *b = t;

}

9. What is structure? How is it different from array and Union? Discuss

6 marks view

10. Define the graphics function. Write a program to draw a circle using graphics function.

6 marks view

C graphics using graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h in Turbo C compiler we can make graphics programs, animations, projects, and games. We can draw circles, lines, rectangles, bars and many other geometrical figures. We can change their colors using the available functions and fill them. 

Program to draw a circle using graphics function

#include <stdio.h>

#include <graphics.h>

main()

{

    int gd = DETECT, gm;

    initgraph(&gd, &gm, "C:\\TC\\BGI");

    circle(100, 100, 50);

    getch();

    closegraph();

    return 0;

}