BSc Sem 1 Computer Paper 2017 Answers of 3 B, C, D
Question 3 (B) answer in brief (Any one) 2 marks
1.
Structure
vs. Class.
Comparison
between Class and Structure in C++:
Class
|
Structure
|
|
Definition
|
A class in C++
can be defined as a collection of related variables and functions
encapsulated in a single structure.
|
A structure can
be referred to as a user defined data type possessing its own operations.
|
Keyword for the
declaration
|
Class
|
Struct
|
Default access
specifier
|
Private
|
Public
|
Example
|
struct student
{
private:
int data;
public:
void
getdata();
void
dispdata();
};
|
struct student
{
int
data;
void
getdata();
void
dispdata();
};
|
Purpose
|
Data abstraction
and further inheritance
|
Generally,
grouping of data
|
Type
|
Reference
|
Value
|
Usage
|
Generally used
for large amounts of data.
|
Generally used
for smaller amounts of data.
|
2.
Explain
Union.
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.
Defining a Union
To
define a union, you must use the union statement
in the same way as you did while defining a structure. The union statement
defines a new data type with more than one member for your program. The format
of the union statement is as follows −
union [union tag] {
member definition;
member definition;
...
member definition;
} [one or more union variables];
The union tag is optional and each member
definition is a normal variable definition, such as int i; or float f; or any
other valid variable definition. At the end of the union's definition, before
the final semicolon, you can specify one or more union variables but it is
optional.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Question 3 (C) answer in detail (Any
one) 3 marks
1.
Explain
malloc() function.
Allocating Memory
Dynamically
While
programming, if you are aware of the size of an array, then it is easy and you
can define it as an array. For example, to store a name of any person, it can
go up to a maximum of 100 characters, so you can define something as follows −
char name[100];
But
now let us consider a situation where you have no idea about the length of the
text you need to store, for example, you want to store a detailed description
about a topic. Here we need to define a pointer to character without defining
how much memory is required and later, based on requirement, we can allocate
memory as shown in the below example −
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char name[100];
char *description;
strcpy(name, "Piyush Patel");
/* allocate memory dynamically */
description = malloc( 200 * sizeof(char) );
if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else {
strcpy( description, "Piyush Patel a DPS student in class 10th");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );
}
When
the above code is compiled and executed, it produces the following result.
Name = Piyush Patel
Description: Piyush Patel a DPS student in class 10th
2.
Explain
new and delete.
new and delete
Operators
There
is following generic syntax to use new operator
to allocate memory dynamically for any data-type.
new data-type;
Here, data-type could be any built-in data
type including an array or any user defined data types include class or
structure. Let us start with built-in data types. For example we can define a
pointer to type double and then request that the memory be allocated at
execution time. We can do this using the new operator
with the following statements −
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
The
memory may not have been allocated successfully, if the free store had been
used up. So it is good practice to check if new operator is returning NULL
pointer and take appropriate action as below −
double* pvalue = NULL;
if( !(pvalue = new double )) {
cout << "Error: out of memory." <<endl;
exit(1);
}
The malloc() function from C, still exists
in C++, but it is recommended to avoid using malloc() function. The main
advantage of new over malloc() is that new doesn't just allocate memory, it
constructs objects which is prime purpose of C++.
At
any point, when you feel a variable that has been dynamically allocated is not
anymore required, you can free up the memory that it occupies in the free store
with the ‘delete’ operator as follows −
delete pvalue; // Release memory pointed to by pvalue
Let
us put above concepts and form the following example to show how ‘new’ and
‘delete’ work −
#include <iostream>
using namespace std;
int main () {
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
*pvalue = 29494.99; // Store value at allocated address
cout << "Value of pvalue : " << *pvalue << endl;
delete pvalue; // free up the memory.
return 0;
}
If
we compile and run above code, this would produce the following result −
Value of pvalue : 29495
Question 3 (D) write a note on (Any one) 5 marks
1.
Explain
Array of Structure.
There
can be array of structures in C programming to store many information of
different data types. The array of structures is also known as collection of
structures.
Let's
see an example of structure with array that stores information of 5 students
and prints it.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student{
int rollno;
char name[10];
};
void main(){
int i;
struct student st[5];
clrscr();
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
getch();
}
Output:
Enter Records of 5
students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Piyush
Student Information
List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Piyush
2.
Write
a program using structure to display student roll no., name and age.
#include <stdio.h>
#include <string.h>
struct student
{ int rno;
char name[50];
float age;
}s1; //declaring s1 variable for structure
int main( )
{
//store first student information
s1.rno=101;
strcpy(s1.name, "Piyush
Patel");//copying string into char array
s1.age=32;
//printing first student information
printf( "student 1 rno : %d\n", s1.rno);
printf( "student 1 name : %s\n", s1.name);
printf( "student 1 age : %f\n", s1.age);
return 0;
}
Output:
student 1 rno : 101
student 1 name : Piyush Patel
student 1 age : 32.000000
Comments
Post a Comment