Professional Documents
Culture Documents
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;
return 0;
Output:
&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] = {
};
int i, j;
printf("\n");
return 0;
Output:
10 11 12 13
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 3
20 21 22 23
30 31 32 33
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.
void pointers in C are used to implement generic functions in C. For example compare function which
is used in qsort().
Eg:
#include<stdlib.h>
int main()
int a = 7;
float b = 7.6;
void *p;
p = &a;
p = &b;
return 0;
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 4
Ans:
#include<stdio.h>
int main()
int i,factorial,n;
scanf("%d",&n);
findFactorial(n,&factorial);
return 0;
int i;
*factorial =1;
for(i=1;i<=n;i++)
*factorial=*factorial*i;
Ans:
#include <stdio.h>
#include <stdlib.h>
void main()
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 5
read(a, m, n);
read(b, p, q);
display(a, m, n);
display(b, p, q);
if (n == p) {
multiplicationOfTwoMatrices(a, b, m, n, q);
} else {
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);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
printf("%d ",*(a+i*n+j));
printf("\n");
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;
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.
int main()
int b[5] = { 1, 2, 3, 4, 5 };
int i = 0;
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.
void main()
int arr[] = { 1, 2, 3 };
int i, *ptr[SIZE];
ptr[i] = &arr[i];
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()
printf("Name ?:");
gets(emp.name);
printf("ID ?:");
scanf("%d",&emp.empId);
printf("Salary ?:");
scanf("%f",&emp.salary);
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
Name: Mike
Id: 1120
Salary: 76543.000000
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;
};
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 11
void main()
int i;
struct Employee E;
scanf("%d",&E.Id);
scanf("%s",&E.Name);
scanf("%f",&E.Salary);
scanf("%s",&E.Add.HouseNo);
scanf("%s",&E.Add.City);
scanf("%s",&E.Add.PinCode);
printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
Output :
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 12
Details of Employees
Employee Id : 101
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;
};
scanf("%d%d%d%d",&s->regdno,&s->m1,&s->m2,&s->m3);
s->tot=s->m1+s->m2+s->m3;
s->avg=s->tot/3.00;
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
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.
. .
. .
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 15
Ans:
#include <stdio.h>
#include <string.h>
union Data
int i;
float f;
char str[20];
};
int main( )
data.i = 10;
data.f = 220.5;
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 16
return 0;
UNIT-3: FUNCTIONS
11. Distinguish Call by value and Call by reference techniques with examples.
Ans:
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
Eg:
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 17
Call by value:
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(a,b);
int temp;
temp = a;
a=b;
b=temp;
Output:
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 18
Call by reference:
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(&a,&b);
int temp;
temp = *a;
*a=*b;
*b=temp;
Output:
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>
temp = number;
while( number!=0 )
number /= 10;
else return 1;
int main()
int number;
scanf("%d", &number);
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 20
if(checkPalindrome(number) == 0)
else
return 0;
Output:-
Ans:
#include <stdio.h>
int largest=0;
largest=a;
largest=b;
else
largest=c;
return largest;
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 21
int main()
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
return 0;
Output:
Ans:
#include<stdio.h>
void fact()
int i,fact=1,n;
scanf("%d",&n);
fact=fact*i;
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 22
int main()
fact();
return 0;
#include<stdio.h>
int fact(int n)
int i,fact=1;
fact=fact*i;
int main()
int n;
scanf(“%d”,&n);
fact(n);
return 0;
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 23
#include<stdio.h>
int fact()
int i,fact=1,n;
scanf("%d",&n);
fact=fact*i;
return fact;
int main()
return 0;
#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
fact=fact*i;
return fact;
int main()
int n,f;
scanf(“%d”,&n);
f=fact(n);
return 0;
Output:
15. What is recursion? Explain execution of recursive function using a simple program in
C.
Ans:
Eg:
#include<stdio.h>
Mohammed Raamizuddin
Roll No: 19K41A0517
Date: 11-11-2020 25
int main() {
int n;
scanf("%d",&n);
return 0;
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
Output:
Factorial of 5 = 120
Mohammed Raamizuddin
Roll No: 19K41A0517