You are on page 1of 25

Date: 11-11-2020 1

PROBLEM SOLVING WITH PROGRAMMING


THEORY IMPORTANT QUESTIONS AND ANSWERS
UNIT-1: POINTERS

1. Explain the pointers to one dimensional array and two dimensional array through a c
program.

Ans:

One Dimensional Array: An array is a block of sequential data of same data type elements.

Eg:

#include <stdio.h>

int main()

int x[4];

int i;

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

printf("&x[%d] = %p\n", i, &x[i]);

printf("Address of array x: %p", x);

return 0;

Output:

&x[0] = 1450734448 Address of array x: 1450734448

&x[1] = 1450734452

&x[2] = 1450734456

&x[3] = 1450734460

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 2

Two Dimensional Array: In a two dimensional array, we can access each element by using two
subscripts, where first subscript represents the row number and second subscript represents the
column number. The elements of 2-D array can be accessed with the help of pointer notation
also.

Eg:

#include<stdio.h>

int main()

int arr[3][4] = {

{ 10, 11, 12, 13 },

{ 20, 21, 22, 23 },

{ 30, 31, 32, 33 }

};

int i, j;

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

printf("Address of %dth array = %p \n", i, *(arr + i));

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

printf("%d ", *(*(arr + i) + j));

printf("\n");

return 0;

Output:

Address of 0th array = 0x7ffe50edd580

10 11 12 13

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 3

Address of 1th array = 0x7ffe50edd590

20 21 22 23

Address of 2th array = 0x7ffe50edd5a0

30 31 32 33

2. Explain the concept of Void pointer with example.

Ans:

A void pointer is a pointer that has no associated data type with it. A void pointer can hold
address of any type and can be typcasted to any type.

Advantages of void pointers:


malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of
any data type.

void pointers in C are used to implement generic functions in C. For example compare function which
is used in qsort().

void pointers cannot be dereferenced.

Eg:

#include<stdlib.h>

int main()

int a = 7;

float b = 7.6;

void *p;

p = &a;

printf("Integer variable is = %d", *( (int*) p) );

p = &b;

printf("\nFloat variable is = %f", *( (float*) p) );

return 0;

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 4

3. Write a C-program to find the factorial of a number using pointers.

Ans:

#include<stdio.h>

void findFactorial(int,int *);

int main()

int i,factorial,n;

printf("Enter a number: ");

scanf("%d",&n);

findFactorial(n,&factorial);

printf("Factorial of %d is: %d",n,*factorial);

return 0;

void findFactorial(int n,int *factorial)

int i;

*factorial =1;

for(i=1;i<=n;i++)

*factorial=*factorial*i;

4. Write a C-program to multiply two matrices.

Ans:

#include <stdio.h>

#include <stdlib.h>

void main()

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 5

int *a, *b, m, n, p, q;

printf("Enter the size of the first matrix : ");

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

a = (int *) calloc(m * n, sizeof(int));

read(a, m, n);

printf("Enter the size of the second matrix : ");

scanf("%d%d", &p, &q);

b = (int *) calloc(p * q, sizeof(int));

read(b, p, q);

printf("The first matrix is\n");

display(a, m, n);

printf("The second matrix is\n");

display(b, p, q);

if (n == p) {

multiplicationOfTwoMatrices(a, b, m, n, q);

} else {

printf("Multiplication is not possible\n");

void read(int *a,int m,int n)

printf("Enter %d elements : ",m*n);

for(int i=0;i<m;i++)

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 6

for(int j=0;j<n;j++)

scanf("%d",a+i*n+j);

void display(int *a,int m,int n)

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

printf("%d ",*(a+i*n+j));

printf("\n");

void multiplicationOfTwoMatrices(int *a,int *b,int m,int n,int q)

int *c;

c=(int *)calloc(m*q,sizeof(int));

int sum;

for(int i=0;i<m;i++)

for(int j=0;j<q;++j)

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 7

sum=0;

for(int k=0;k<n;k++)

sum+=(*(a+i*n+k))*(*(b+k*q+j));

*(c+i*q+j)=sum;

printf("The Multiplication Matrix is\n");

display(c,m,q);

5. What is the difference between pointer to array and array of pointers. Explain with
examples.

Ans:

Pointer to array: Pointer to array is also known as array pointer. We are using the pointer to
access the components of the array.

Eg: #include <stdio.h>

int main()

int(*a)[5]; // Pointer to an array of five numbers

int b[5] = { 1, 2, 3, 4, 5 };

int i = 0;

a = &b; // Points to the whole array b

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

printf("%d\n", *(*a + i));

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 8

return 0;

Array of pointers: Array of pointers is an array of the pointer variables. It is also known as
pointer arrays.

Eg: #include <stdio.h>

const int SIZE = 3;

void main()

int arr[] = { 1, 2, 3 };

int i, *ptr[SIZE];

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

ptr[i] = &arr[i];

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

printf("Value of arr[%d] = %d\n", i, *ptr[i]);

UNIT-2: STRUCTURES AND UNIONS

6. Define structure? Write a C program to print the employee details using structures.

Ans:

structure is user defined data type available in C that allows to combine data items of different
kinds. Structure is a group of variables of different data types represented by a single name. Each
element of a structure is called a member.

Program:

#include <stdio.h>

struct employee{

char name[30];

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 9

int empId;

float salary;

};

int main()

struct employee emp;

printf("\nEnter details :\n");

printf("Name ?:");

gets(emp.name);

printf("ID ?:");

scanf("%d",&emp.empId);

printf("Salary ?:");

scanf("%f",&emp.salary);

printf("\nEntered detail is:");

printf("Name: %s" ,emp.name);

printf("Id: %d" ,emp.empId);

printf("Salary: %f\n",emp.salary);

return 0;

Output:

Enter details :

Name ?:Mike

ID ?:1120

Salary ?:76543

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 10

Entered detail is:

Name: Mike

Id: 1120

Salary: 76543.000000

7. Explain how a structure can be Nested in another structure with example.

Ans:

When a structure contains another structure, it is called nested structure. For example,we have
two structures named Address and Employee. To make Address nested to Employee, we have to
define Address structure before and outside Employee structure and create an object of Address
structure inside Employee structure.

Eg:

#include<stdio.h>

struct Address

char HouseNo[25];

char City[25];

char PinCode[25];

};

struct Employee

int Id;

char Name[25];

float Salary;

struct Address Add;

};

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 11

void main()

int i;

struct Employee E;

printf("\n\tEnter Employee Id : ");

scanf("%d",&E.Id);

printf("\n\tEnter Employee Name : ");

scanf("%s",&E.Name);

printf("\n\tEnter Employee Salary : ");

scanf("%f",&E.Salary);

printf("\n\tEnter Employee House No : ");

scanf("%s",&E.Add.HouseNo);

printf("\n\tEnter Employee City : ");

scanf("%s",&E.Add.City);

printf("\n\tEnter Employee House No : ");

scanf("%s",&E.Add.PinCode);

printf("\nDetails of Employees");

printf("\n\tEmployee Id : %d",E.Id);

printf("\n\tEmployee Name : %s",E.Name);

printf("\n\tEmployee Salary : %f",E.Salary);

printf("\n\tEmployee House No : %s",E.Add.HouseNo);

printf("\n\tEmployee City : %s",E.Add.City);

printf("\n\tEmployee House No : %s",E.Add.PinCode);

Output :

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 12

Enter Employee Id : 101

Enter Employee Name : Suresh

Enter Employee Salary : 45000

Enter Employee House No : 4598/D

Enter Employee City : Delhi

Enter Employee Pin Code : 110056

Details of Employees

Employee Id : 101

Employee Name : Suresh

Employee Salary : 45000

Employee House No : 4598/D

Employee City : Delhi

Employee Pin Code : 110056

8. Define a structure to process student results having Ht-no, Name, and marks in physics,
Chemistry and Maths as members. Write a program in C to display total and average
marks of each student using pointers to structure concept.

Ans:

#include <stdio.h>

void main() {

struct student s;

read(&s);

compute(&s);

display(s);

struct student

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 13

int regdno;

int m1,m2,m3;

int tot;

float avg;

};

struct student read(struct student *s)

printf("Enter regdno, three subjects marks : ");

scanf("%d%d%d%d",&s->regdno,&s->m1,&s->m2,&s->m3);

struct student compute(struct student *s)

s->tot=s->m1+s->m2+s->m3;

s->avg=s->tot/3.00;

struct student display(struct student s)

printf("Student regdno = %d\n",s.regdno);

printf("Total marks = %d\n",s.tot);

printf("Average marks = %f\n",s.avg);

Output:

Enter·regdno,·three·subjects·marks·:·1001 56 78 83

Student·regdno·=·1001

Total·marks·=·217

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 14

Average·marks·=·72.333336

9. What is union? Write the differences between structures and unions.

Ans:

A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same memory
location for multiple-purpose.

Following are the important differences between Structure and Union:

Sr. Key Structure Union


No.

Definition Structure is the container On other hand Union is


defined in C to store data also similar kind of
variables of different type and container in C which can
1
also supports for the user also holds the different type
defined variables storage. of variables along with the
user defined variables.

Syntax Syntax of declare a Structure in On other syntax of declare


C is as follow : a Union in C is as follow:

struct struct_name{ union u_name{

type element1; type element1;


2
type element2; type element2;

. .

. .

} variable1, variable2, ...; } variable1, variable2, ...;

Size As mentioned in definition On other hand Union does


Structure do not have shared not have separate location
location for its members so size for each of its member so
3
of Structure is equal or greater its size or equal to the size
than the sum of size of all the of largest member among
data members. all data members.

4 Value As mentioned above in case of While in case of Union

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 15

Sr. Key Structure Union


No.

storage Structure there is specific there is only one shared


memory location for each input memory allocation for all
data member and hence it can input data members so it
store multiple values of the stores a single value at a
different members. time for all members.

Initialization In Structure multiple members On other hand in case of


can be can be initializing at Union only the first
5
same time. member can get initialize at
a time.

10. Write a C-program to demonstrate unions.

Ans:

#include <stdio.h>

#include <string.h>

union Data

int i;

float f;

char str[20];

};

int main( )

union Data data;

data.i = 10;

printf( "data.i : %d\n", data.i);

data.f = 220.5;

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 16

printf( "data.f : %f\n", data.f);

strcpy( data.str, "C Programming");

printf( "data.str : %s\n", data.str);

return 0;

UNIT-3: FUNCTIONS

11. Distinguish Call by value and Call by reference techniques with examples.

Ans:

Parameters Call by value Call by reference

While calling a function, when While calling a function, in programming


you pass values by copying language instead of copying the values of
Definition
variables, it is known as "Call By variables, the address of the variables is used
Values." it is known as "Call By References.

In this method, a copy of the


Arguments In this method, a variable itself is passed.
variable is passed.

Changes made in a copy of


Change in the variable also affects the value
Effect variable never modify the value
of the variable outside the function.
of variable outside the function.

Alteration of Does not allow you to make any Allows you to make changes in the values of
value changes in the actual variables. variables by using function calls.

Passing of Values of variables are passed Pointer variables are required to store the
variable using a straightforward method. address of variables.

Value
Original value not modified. The original value is modified.
modification

Actual and formal arguments will


Memory Actual and formal arguments will be created
be created in different memory
Location in the same memory location
location

Eg:

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 17

Call by value:

#include <stdio.h>

void swap(int , int);

int main()

int a = 10;

int b = 20;

printf("Before swapping the values in main a = %d, b = %d\n",a,b);

swap(a,b);

printf("After swapping values in main a = %d, b = %d\n",a,b);

void swap (int a, int b)

int temp;

temp = a;

a=b;

b=temp;

printf("After swapping values in function a = %d, b = %d\n",a,b);

Output:

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

After swapping values in main a = 10, b = 20

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 18

Call by reference:

#include <stdio.h>

void swap(*int , *int);

int main()

int a = 10;

int b = 20;

printf("Before swapping the values in main a = %d, b = %d\n",a,b);

swap(&a,&b);

printf("After swapping values in main a = %d, b = %d\n",a,b);

void swap (int a, int b)

int temp;

temp = *a;

*a=*b;

*b=temp;

printf("After swapping values in function a = %d, b = %d\n",a,b);

Output:

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

After swapping values in main a = 20, b = 10

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 19

12. What is a function? Write a C program to check whether the given number is
palindrome or not using functions.

Ans:

A function in programming is a reusable block of code that makes a program easier to test,
understand and can be modified easily without changing the calling program. Functions to divide
the code and modularize the program for better and effective results. In short, a larger program is
divided into various subprograms which are called as functions.

Program:

#include<stdio.h>

int checkPalindrome(int number)

int temp, remainder, sum=0;

temp = number;

while( number!=0 )

remainder = number % 10;

sum = sum*10 + remainder;

number /= 10;

if ( sum == temp ) return 0;

else return 1;

int main()

int number;

printf("Enter the number: ");

scanf("%d", &number);

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 20

if(checkPalindrome(number) == 0)

printf("%d is a palindrome number.\n",number);

else

printf("%d is not a palindrome number.\n",number);

return 0;

Output:-

Enter the number: 1234


1234 is not a palindrome number.

Enter the number: 5225


5225 is a palindrome number.

13. Write a program to find the greatest of 3 numbers using functions.

Ans:

#include <stdio.h>

int largestNumber(int a,int b, int c)

int largest=0;

if(a>b && a>c)

largest=a;

else if(b>a && b>c)

largest=b;

else

largest=c;

return largest;

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 21

int main()

int a,b,c;

printf("Enter three numbers: ");

scanf("%d%d%d",&a,&b,&c);

printf("Largest number is: %d\n",largestNumber(a,b,c));

return 0;

Output:

Enter three numbers: 10 20 11

Largest number is: 20

14. Explain different types of parameter passing techniques using examples.

Ans:

Without parameters and without return value:

#include<stdio.h>

void fact()

int i,fact=1,n;

scanf("%d",&n);

for(i=1; i<=n; i++)

fact=fact*i;

Printf(“Factorial of the given number is: %d\n”,fact);

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 22

int main()

printf("Enter a Number to Find Factorial: ");

fact();

return 0;

With Parameters without return value:

#include<stdio.h>

int fact(int n)

int i,fact=1;

for(i=1; i<=n; i++)

fact=fact*i;

printf("\nFactorial of a Given Number is: %d ",fact);

int main()

int n;

printf("Enter a Number to Find Factorial: ");

scanf(“%d”,&n);

fact(n);

return 0;

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 23

Without Parameters With return value

#include<stdio.h>

int fact()

int i,fact=1,n;

scanf("%d",&n);

for(i=1; i<=n; i++)

fact=fact*i;

return fact;

int main()

printf("Enter a Number to Find Factorial: ");

printf("\nFactorial of a Given Number is: %d ",fact());

return 0;

With Parameters With return value:

#include<stdio.h>

#include<math.h>

int fact(int n)

int i,fact=1;

scanf("%d",&n);

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 24

for(i=1; i<=n; i++)

fact=fact*i;

return fact;

int main()

int n,f;

printf("Enter a Number to Find Factorial: ");

scanf(“%d”,&n);

f=fact(n);

printf("\nFactorial of a Given Number is: %d ",f);

return 0;

Output:

Enter a number to find factorial: 5

Factorial of Given number is: 120

15. What is recursion? Explain execution of recursive function using a simple program in
C.

Ans:

Recursion is the process of repeating items in a self-similar way. In programming languages, if


a program allows you to call a function inside the same function, then it is called
a recursive call of the function. The C programming language supports recursion.

Eg:

#include<stdio.h>

Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 25

long int multiplyNumbers(int n);

int main() {

int n;

printf("Enter a positive integer: ");

scanf("%d",&n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

return 0;

long int multiplyNumbers(int n) {

if (n>=1)

return n*multiplyNumbers(n-1);

else

return 1;

Output:

Enter a positive integer: 5

Factorial of 5 = 120

Mohammed Raamizuddin
Roll No: 19K41A0517

You might also like