CPR W22
CPR W22
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 1 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
f) State the use of ‘&’ and ‘*’ operators used in / with pointer 2M
Page 2 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Ans. &Operator: -It is used to return the memory address of the variable. It
is also known as address of operator. Use of ‘&’
1M
OR
It is used to retrieve address of a variable from memory. Use of ‘*’
*Operator: - It is used to declare a pointer variable. Also used to 1M
access the value of a variable whose address is stored in a pointer
variable.
Example: int *ptr,a;
ptr=&a;
Example:-
int a;
float b;
a=2;
b=2.5;
printf(“%d%f”,a,b);
Page 3 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Advantages of array:
1. It is better and convenient way of storing the data of same datatype
with same size.
2. It allows to store multiple elements in it.
3. Accessing an element is very easy by using index number.
4. An array allows storing and access of a large number of values by Any two
writing a small piece of code instead of declaring each variable advantages
2M
separately.
5. Arrays store elements in a sequential manner in memory locations,
so no extra memory is allocated thus preventing the wastage of
memory.
6. Iterating the arrays using their index is faster.
7. It allows to store the elements in any dimensional array – supports
multidimensional array.
8. Arrays are one of the most basic data structures and are used for
processing many algorithms like searching and sorting, maximum
and minimum values, reversing, etc. in simple and easy ways
Page 4 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 5 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
}
Here first the value of a increments and then is assigned to variable b.
So both a and b value will be 11.
Page 6 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:
main()
{
int a[2][2]={{1,2},{4,5});
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
c) Describe pointer arithmetic with any two operations 4M
Ans. The pointer arithmetic is done as per the data type of the pointer. The
basic operations on pointers are:
Increment: Any two
It is used to increment the pointer. Each time a pointer is operation
incremented, it points to the next location with respect to memory
size. Descriptio
Example: n
with
If ptr is an integer pointer stored at address 1000, then ptr++ shows example
1002 as incremented location for an int. It increments by two each 2M
locations as it requires two bytes storage.
Decrement:
It is used to decrement the pointer. Each time a pointer is
decremented, it points to the previous location with respect to
memory size.
Example:
If the current position of pointer is 1002, then decrement operation
ptr-- results in the pointer pointing to the location 1000 in case of
integer pointer as it require two bytes storage.
Addition
When addition operation is performed on pointer, it gives the location
incremented by the added value according to data type.
Page 7 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:
If ptr is an integer pointer stored at address 1000, Then ptr+2 shows
1000+(2*2) = 1004 as incremented location for an int.
Subtraction
When subtraction operation is performed on the pointer variable, it
gives the location decremented by the subtracted value according to
data type.
Example:
If ptr is an integer pointer stored at address 1004, Then ptr-2 shows
1004-(2*2) = 1000 as decremented location for an int.
d) Describe the use of Enumerated data type with example. 4M
Ans. Enumerated data type Correct
1. The Enumerated data type allows the programmer to invent the explanatio
n 2M
programmer's own data type and decide what value the variables of
this data type may carry. Example
2. Enum or enumerator is an integer or number with defined meaningful 2M
names.
3. Enum is often used inside C programs to replace a hard-coded
number with some meaningful symbols.
Example:
include <stdio.h>
enumdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday};
int main()
{
for(int i=Sunday;i<=Saturday;i++){
printf("%d, ",i);
}
return 0;
}
4. Attempt any THREE of the following: 12
a) Write an algorithm and draw flowchart to find whether number 4M
entered is even or odd Algorithm
Ans. Algorithm: 2M
Step 1- Start
Step 2- Read / input the number.
Step 3- if n%2==0 then number is even.
Step 4- else number is odd.
Page 8 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Flowchart: Correct
flowchart
with
notations
2M
Page 9 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 10 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Ans. #include<iostream.h>
#include<conio.h> Correct
void swap(int *a, int *b) logic 2M
{ Correct
int temp; syntax 2M
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a,b;
clrscr();
cout<<"\nEnter the value of a:";
cin>>a;
cout<<"\nEnter the value of b:";
cin>>b;
cout<<"\nBefore swapping:";
cout<<"\na="<<a;
cout<<"\nb="<<b;
swap(&a,&b);
cout<<"\n\nAfter swapping:";
cout<<"\na="<<a;
cout<<"\nb="<<b;
getch();
}
OR
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, *p;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
printf("Before swapping: a:%d b:%d",a,b);
Page 11 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Accessing
Initialization of the Structure Pointer structure
Syntax: member
using
ptr = &structure_variable; pointer
1M,
Access Structure member using pointer:
There are two ways to access the member of the structure using
Structure pointer:
1. Using ( * ) asterisk or indirection operator and dot ( . ) operator.
2. Using arrow ( -> ) operator or membership operator.
#include <stdio.h>
struct Subject
{ Example
// declare the member of the Course structure 2M
char sub_name[30];
int sub_id;
char sub_duration[50];
char sub_type[50];
};
int main()
{
struct Subject sub;
struct Subject *ptr;
ptr =⊂
strcpy (sub.sub_name, " Computer Science");
Page 12 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 13 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 14 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
if(strcmp(str1,strrev(str1))==0)
printf("%s is a palindrome\n", str);
else
printf("%s is a not a palindrome\n", str);
}
c) Write a C program to display Fibonacci series using recursion. 6M
Ans. #include<stdio.h> Correct
main() logic 3M
{ Correct
int n, m= 0, i; syntax 3M
printf("Enter Total terms:");
scanf("%d", &n);
printf("Fibonacci series terms are:");
for(i = 1; i<= n; i++)
{
printf("%d ", fibonacci(m));
m++;
}
}
int fibonacci(int n)
{
if(n == 0 || n == 1)
return n;
else
return(fibonacci(n-1) + fibonacci(n-2));
}
Page 15 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 16 / 17
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 17 / 17