C++ CBSE Class 12 Chapter 1 Part 4 Questions 46 to 56

Indian CBSE class 12 C++ Sumita Arora Chapter 1 Solved Part 4 (Question 46 to 56)


46. What is meant by unsized array initialization in C++? What are it’s benefits?

Answer

Unsized array initialization means to not specify the size of the array during the initialization. In unsized array initialization, the compiler will determine the size of the array for us automatically. This method is useful if you do not know the size of the array prior to its creation.

Example:

char ar[] = “New String”;

Advantages:

i) Less tedious.
 
ii) Safe array initialization as the size is automatically adjusted to fit the number of characters during initialization.
 
iii) Since specific array dimension is not mentioned we may shorten or lengthen the array according to our needs.


47. What do you meant by function prototyping? Write down the advantages of function prototypes in C++.

Answer

Function prototyping means to declare the function argument type, the number of arguments the function will receive and the return type of the function. In function prototyping, the function is only declared not defined.

int Product(int x , int y , int z);

Advantages:

i) By prototyping we are telling the compiler that such function exists somewhere in the file. And if the function is called, it helps the compiler to invoke the appropriate function by checking the arguments number and the type.
 
ii) It tells the program and the user about the return type.
 
iii) It tells the program and user the number of arguments and the type of arguments the function will accept.


48. What are actual and formal parameters of a function?

Answer

The arguments passed to the functions while the function is called is known as the formal parameter, whereas the arguments declared in the function header or during the function definition is known as the actual parameters.

int prod(int a, int b) /*'a', 'b' are the actual parameter*/
{ 
..//your code
}

int main() {
int pro;
pro = prod( 40 , 12); /*40 and 12 are the formal parameter*/

getch();
return 0;
}

49. Construct function prototype for descriptions given below.

test() takes no arguments and has no return value.
convert() takes a float argument and returns an int.
promote() takes two double arguments and returns a double.
sum() takes an int array and an int value and returns a long result.
check() takes a string argument and returns an int.
Answer

(i) void test();
 
(ii) int convert(float a);
 
(iii) double promote(double a, double b);
 
(iv) long sum(int a[], int b);
 
(v) int check(string s);


50. What do you understand by default arguments and constant argument? Write a short note on their usefulness.

Answer
Default arguments:

During the function declaration we are allowed to set some values to the arguments. Such arguments are knonw as default argument. The purpose of default argument is to allow the function call using lesser argument number.

double time(int date, int day , int increment=1 );  

We can call the function ‘time’ by just passing two arguments instead of three.

Constant argument:

All those arguments which are declared as ‘const’ are known as constant arguments. Constant arguments does not allow changing of values inside the function. Their values remain constant.

double func(int x , const int y); 
//x is normal argument 'y' is constant argument

51. How is call-by-value method of function invoking different from call-by-reference method? Give appropriate examples supporting your answer.

Answer
Call By Value:

a)In call-by-value we pass a value/literal to the function.
 
b)When call-by-value is used the value passed to the function are copied to a temporary storage from whcih the function will refer to the value.
 
c) If any changes are done to the value when call-by-value is used the changes is not seen outside the function. This is because the changes are made to the value present in the temporary storage.
 
d)The value is passed so no any extra character like ‘&’ or ‘*’ is used in the argument declaration.

Example:

#include <iostream>

void change(int a , int b){
a = 10; /*change the value of a */
b = 20; /*change the value of b */
}

void change(int a, int b);

int main ( ) 
{
// local variable declaration:
int a = 100;

int b = 200;
cout<<"Before calling \'change\', a="<< a <<endl;
cout<<"Before calling \'change\', b="<< b << endl;

change(a, b);
cout<<"After calling \'change\', a="<< a <<endl;
cout<<"After calling \'change\', b="<< b << endl;

return 0;
}

Even after calling the ‘change; the value of ‘a’ and ‘b’ will remian the same.

Call by reference:

a) In call by reference, the address of the argument is passed not the literal.
 
b)In call by reference, if the argument is accessed inside the function then the value is accessed directly from the original address of the argument. Not form any temporary copy of the argument passed like call-by-value.
 
c) If any changes are done inside the function, they are visible outside the function also.
 
d) It use ‘&’ sign as the reference operator.

Example:

#include <iostream>

void change(int *a , int *b){
*a = 10; /*change the value of a */
*b = 20; /*change the value of b */
}

void change(int *a, int *b);

int main ( ) 
{
// local variable declaration:
int a = 100;

int b = 200;
cout<<"Before calling \'change\', a="<< a <<endl;
cout<<"Before calling \'change\', b="<< b << endl;

change( &a, &b);
cout<<"After calling \'change\', a="<< a <<endl;
cout<<"After calling \'change\', b="<< b << endl;

return 0;
}

The ‘a’ and ‘b’ will be changed to 10and 20 after calling the ‘change’ fucntion.


52. Discuss the similarities and difference between global and local variables in terms of their lifetime and scope.

Answer

Difference
a) A local variable exists only inside the local scope or function Whereas global scope exists until the program terminates.
 
b) A local variable is accessible only inside the function or inside the local scope. The global variable is accessible from anywhere inside the file.
 
c)A local variable is declared inside the function or compound statement. But the global variable is declared outside all the functions.

Similarities
a) Both local and global variable is accessible inside the local scope.
 
b)Both local and global variable can have the same name.

Example:

#include <iostream.h>

double Pi=3.1415; //global variable

double area( double radius )
{ return Pi*radiius*radius; }

int main()
{
double Radius=3.4; //local variable

cout<< area( Radius );

cout<< "\n Area=" << Pi* Radius * Radius ; //work fine

cin.get();
return 0;
}

53. What is structure? Declare a structure in C++ with name, roll number and total marks as components.

Answer

A structure is a collection of variables referenced under one name. A structure is declared using the keyword ‘struct’. The syntax od structure declaration is shown below:

struct Structure_name
{
[public:] | [private:] | [protected:]
/* data members’ declarations */
/* member functios’ declarations */
};

Example:

struct Student
{
char Name[30];

int Rollno;

float Total_Marks;
};

54. What are Nested structures? Give an example.

Answer

A structure within a structure is called nested structures.

Example:


struct Address //structure tag
{
int house_no;
char area[26];
char city[26];
char state[26];
};

struct Employee //structure tag
{
int id;
char name[26] ,
 father_name[16];

Address addr; //Address structure

int age;
};
Employee worker; // create structure variable

The structure ‘Employee’ has the structure ‘Address’ declared inside it as one of its member data. Hence the structure Address is a nested structure.


55. Write a program that asks the user to enter two integers, obtains the two numbers from the user, and outputs the large number followed by the words “is larger by – units than smaller number” to the system console (e.g., if the larger number is 9 and smaller is 6, message should be “9 is larger by 3 units than smaller number”). If the numbers are equal print the message “These numbers are equal”.

Answer
#include<iostream>

using namespace std;

int main ( )
{
   int a , b;

   cout<< "Enter two numbers \n" ;
   cin>> a  >> b;

   cin.ignore();

   if( a==b)
   {
      cout<< "These numbers are equal";
   }
   else if(a>b)
   {
      cout<< a << " is larger by " << (a-b) << " units than smaller number" ;
   }
   else
      cout<< b << " is larger by " << (b-a) << " units than smaller number" ;

   cin.get();
   return 0;
}

56. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tanks of CNG by recording the miles driven and the gallons used for each tank. Develop a C++ program that will input the kilometres driven and gallons used for each tank. The program should calculate and display the kilometres per gallon obtained for each tank of gasoline. After processing all input information, the program should calculate and print the average kilometres per gallon obtained for all tanks.
 
   Formulate the algorithm as flowchart.
   Write a C++ program as instructed.
   Test, debug, and execute the C++ program.

Answer
int main ( )
{
   int tanks=0; //number of tanks

   float total_km , avg_km_per_gal;

   cout<<"Enter how many tanks filled :";
   cin>>tanks;

   cin.ignore();

   float km[tanks] , //kilometr per tank
    gallons_used[tanks] ; //gallon used in the tank

   float km_per_gal[tanks]; //kilometer per gallon

   for(int i=0;i<tanks;i++)
   {
   cout<<"Kilometer driven for the tank number "<< i+1 << " is:";
   cin>>km[i];

   cin.ignore();

   cout<<"How many gallons used from the tank number " << i+1 <<":";
   cin>>gallons_used[i];

   cin.ignore();

   km_per_gal[i]=km[i]/gallons_used[i];

   cout<<"Kilometer per Gallon obtained for tank No. "<< i+1 <<" is::"<< km_per_gal[i]<<endl;
   total_km+=km_per_gal[i];

   cout<<endl;
   }

   avg_km_per_gal=total_km/tanks ;

   cout<<"Average kilometers per gallon obtained for all tanks is:"<< avg_km_per_gal;

   cin.get();
   return 0;
}



Leave a Reply

Your email address will not be published. Required fields are marked *