BSc Sem 1 Computer Paper 2017 Answers of 4 B, C, D
Question 4 (B) answer in brief (Any one) 2 marks
1.
Explain
passing pointer as function argument.
C
programming allows passing a pointer to a function. To do so, simply declare
the function parameter as a pointer type.
Following
is a simple example where we pass an unsigned long pointer to a function and
change the value inside the function which reflects back in the calling
function −
#include <stdio.h>
#include <time.h>
void getSeconds(unsigned long *par);
int main () {
unsigned long sec;
getSeconds( &sec );
/* print the actual value */
printf("Number
of seconds: %ld\n", sec );
return 0;
}
void getSeconds(unsigned long *par) {
/* get the current number of seconds */
*par
= time(
NULL );
return;
}
When
the above code is compiled and executed, it produces the following result −
Number of seconds :1294450468
2.
Explain
Pointer.
Pointers
in C are easy and fun to learn. Some C programming tasks are performed more
easily with pointers, and other tasks, such as dynamic memory allocation,
cannot be performed without using pointers. So it becomes necessary to learn
pointers to become a perfect C programmer. Let's start learning them in simple
and easy steps.
As you
know, every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator, which
denotes an address in memory. Consider the following example, which prints the
address of the variables defined −
#include <stdio.h>
int main () {
int var1;
char var2[10];
printf("Address
of var1 variable: %x\n", &var1 );
printf("Address
of var2 variable: %x\n", &var2 );
return 0;
}
When
the above code is compiled and executed, it produces the following result −
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
Question 4(C) answer in detail (Any one) 3 marks
1.
Explain
Structure.
Arrays
allow to define type of variables that can hold several data items of the same
kind. Similarly structure is another user defined data type
available in C that allows to combine data items of different kinds.
Structures
are used to represent a record. Suppose you want to keep track of your books in
a library. You might want to track the following attributes about each book −
·
Title
·
Author
·
Subject
·
Book ID
Defining a Structure
To
define a structure, you must use the struct statement. The
struct statement defines a new data type, with more than one member. The format
of the struct statement is as follows −
struct [structure tag] {
member
definition;
member
definition;
...
member
definition;
} [one or more structure variables];
The structure
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 structure's definition, before the final semicolon, you can
specify one or more structure variables but it is optional. Here is the way you
would declare the Book structure −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
}
book;
Accessing Structure Members
To
access any member of a structure, we use the member access operator (.).
The member access operator is coded as a period between the structure variable
name and the structure member that we wish to access. You would use the keyword struct to
define variables of structure type.
2.
Explain
Pointer to Structure.
You
can define pointers to structures in the same way as you define pointer to any
other variable −
struct Books *struct_pointer;
Now,
you can store the address of a structure variable in the above defined pointer
variable. To find the address of a structure variable, place the '&';
operator before the structure's name as follows −
struct_pointer = &Book1;
To
access the members of a structure using a pointer to that structure, you must
use the → operator as follows −
struct_pointer->title;
Let us
re-write the above example using structure pointer.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/*
function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C
Programming");
strcpy( Book1.author, "Nuha
Ali");
strcpy( Book1.subject, "C
Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom
Billing");
strcpy( Book2.author, "Zara
Ali");
strcpy( Book2.subject, "Telecom
Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
When
the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
Question 4(d) answer in detail (Any one) 5 marks
1.
Explain
operator overloading with example.
#include< iostream.h>
#include< conio.h>
class time
{
int h,m,s;
public:
time()
{
h=0, m=0; s=0;
}
void getTime();
void show()
{
cout<< h<< ":"<< m<<
":"<< s;
}
time operator+(time); //overloading '+' operator
};
time time::operator+(time t1) //operator function
{
time t;
int a,b;
a=s+t1.s;
t.s=a%60;
b=(a/60)+m+t1.m;
t.m=b%60;
t.h=(b/60)+h+t1.h;
t.h=t.h%12;
return t;
}
void time::getTime()
{
cout<<"\n Enter the hour(0-11) ";
cin>>h;
cout<<"\n Enter the minute(0-59) ";
cin>>m;
cout<<"\n Enter the second(0-59) ";
cin>>s;
}
void main()
{
clrscr();
time t1,t2,t3;
cout<<"\n Enter the first time
";
t1.getTime();
cout<<"\n Enter the second time
";
t2.getTime();
t3=t1+t2; //adding
of two time object using '+' operator
cout<<"\n First time ";
t1.show();
cout<<"\n Second time ";
t2.show();
cout<<"\n Sum of times ";
t3.show();
getch();
}
2.
Explain
function overloading with example.
#include <iostream>
using namespace std;
/* Function arguments are of different
data type */
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
long add(long x, long y)
{
long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}
Comments
Post a Comment