Loading....
Coupon Accepted Successfully!

 

Question-1

Define an array.

Solution:
An array is a collection of variables of the same type that are referenced by a common name.

Question-2

List the two types of arrays.

Solution:
The two types of array are one-dimensional array (b) multi-dimensional arrays.

Question-3

Explain Single dimensional array.

Solution:
The simplest from of an array is a single dimensional array. The array is given a name and its elements are referred to by their subscripts or indices. C++ array’s index numbering starts with 0.

Like other variables in C++, an array must be defined before it can be used to store information. Like other definitions, an array definition specifies a variable type and a name along with one more future size to specify how many data items the array will contain. The general form of an array declaration is as shown below;

type array-name [size]

Where type declares the base type of the array, which is the type of each element in the array. The array-name specifies the name with which the array will be referenced and size defines how many elements the array will hold. The size must be an integer value or integer constant without any sign.

Following statement declares an array marks of base type int and which can hold 50 elements.

int marks [50] ;

The above statement declared array marks has 50 elements, marks [0] to marks [49]. The following program reads marks of 50 students and stores them in an array marks.

Question-4

Write a program to accept sales of each day of the day of the month and print the total sales and average sales.

Solution:
#include<iostream.h>

#include<conio.h>

int main ( )

{

        clrscr( ) ;

        const int size = 3 ;

        float sales[size], avg = 0, total = 0;

        for (int I = 0 ; I < size ; i++ )

{

        cout << "Enter sales made on day" << I + 1<< " : " ;

        cin >> sales[i] ;

        total += sales[i] ;

} //Loop over

avg = total / size ;

cout << "\n Total sales = " << total << "n\" ;

cout << "Average sales = " << avg << "\n" ;return 0 ;

Question-5

Define vector.

Solution:
A vector is a mathematical term which refers to the collection of numbers which are analogous i.e., a linear array.

Question-6

Write a program to search for an element in an array.

Solution:
#include<iostream.h>

#include<conio.h>

void main ( )

{

    int A[20], size, I, flag = 0, num, pos ;

    cout << "\n Enter the number of elements in the array :" ;

    cin >> size ;

    cout << "\n Enter the elements of array (in ascending order) ;" ;

    for(i = 0 ; I < size ; i++)

    cin >> A[i] ;

    cout << "\n Enter the elements to be searched for :" ;

    cin >> num ;

    for (i = 0 ; i < size ; i++)

    if(A[ i ] == num)

{

    flag = 1 ;

    pos = i ;

    break ;

}

if(flag == 0)

    cout << "\n Element not found" ;

else

    cout << "\n Element found at position" << pos + 1 ;

getch( ) ;

}

Question-7

Explain string using array.

Solution:
C++ does not have a string data type rather it implements strings as single-dimension character arrays. A string is defined as a character array that is terminated by a null character ‘\0’. For this reason, the character arrays are declared one longer than the largest string they can hold.

For instance, to declare an array strg that holds a 10-character string, you would write

Char strg[11] ;

This makes room from the null at the end of the string.

Individual characters of a string can be easily accessed as they make the elements of the character array. The index 0 refers to the first character, the index 1 to the second, 2 to the third, and so forth. The end of a string is determined by checking for null character.

Question-8

Write a program to find the number of vowels in a given line of text.

Solution:
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

void main( )

{

         clrscr( ) ;

         char line[80] ;

         int vow-ctr = 0 ;

         // Read the line of text

         cout << "Enter the line ;" << endl ;

         gets(line) ; //gets( ) is used to read strings

         for(int i = 0 ; line[i] != ‘\0’ ; ++i )

{

Switch(line[i])

{

         case ‘a’ :

         case ‘A’ :

         case ‘e’ :

         case ‘E’ :

         case ‘i’ :

         case ‘I’ :

         case ‘o’ :

         case ‘O’ :

         case ‘u ’ :

         case ‘U’ :          ++vow_ ctr ;

 

                  }

}

cout<<"The total number of vowels in the given line is"

<< vow_ctr <<endl;

         }

}

Question-9

Explain multidimensional array

Solution:
C++ allows you to have arrays with dimensions more than two. The maximum limit of dimensions is compiler dependent. The general form of a multidimensional array declaration is

type name [a] [b] [c]………[z];

The arrays of three or more dimensions are not often used because of their huge memory requirements and the complexity involved with their manipulation. Manipulation of multidimensional arrays takes more time compared to single-dimensional and two-dimensional arrays.

Question-10

Write a program to concatenate two strings.

Solution:

#include<iostream.h>

#include<conio.h>

#include<string.h>

Int main( )

{      clrscr( ) ;

        Char str1[25], str2[25], str3[50] ; //str3 to store concatenate strings.

        int i, k, x1 ;

        cout << "\nEnter first string (max 24 character) :\n"

        cin.getline (str1, 25) ;

        cout << "\nEnter second string (max 24 character) :\n" ;

        cin.getline (str2, 25) ;

        for(i = 0 ; str1[i] != ‘\0’ ; ++i ) // concatenation.

        str3[i] = str1[i] ;

        for(k = 0 ; str2[k] != ‘\0’ ; ++k)

                str3[i + k] = str2[k] ;

                str3[i +k] = ‘\0’ ;

        x1 = strlen (str3) ;

        cout << "\nThe concatenation Ted string is :\n" ;

        cout.write(str3, x1) ;

        return 0 ;

}

Question-11

Write a program to concatenate the length of a string without using a library function.

Solution:
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

Void main( )

{    clrscr( ) ;

      Char str[80] ;

      int i ;

      cout << "\nEnter any string (max. 80) :" ;

      gets(str) ;

      for(i = 0 ; str[i] != ‘\0’ ; ++i ;

      cout << "\n Length of the string is :" << i ;

      getch( ) ;

}

Question-12

Write a program for reversing a string

Solution:
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

Void main( )

{      clrscr( ) ;

        char str[80] ;

        int i, j ;

        cout << "\nEnter any string (max. 80) : " ;

        gets(str) ;

        for(i = 0 ; str[i] != ‘\0’ ; ++i ) ;

                char rev[80] ;

        int k ;

        for(j = i – 1, k = 0 ; j >=0 ; --j, ++k )

                rev[ k ] = str[j] ;

        rev[ k ] = ‘\0’ ;

        cout << "\nString in reverse order is : " ;

        puts (rev) ;

        getch( ) ;

}

Question-13

Do the array values are stored in an memory location. Justify.

Solution:
Yes, the memory values are stored in a memory location each time when an array is generated.

Question-14

Define base type of an array.

Solution:
The data type of array elements is known as the base type of the array.

Question-15

Write a program to reverse a vector without using a temporary variable.

Solution:
#include<iostream.h>

#include<conio.h>

int main( )

{     clrscr( ) ;

       int vector[10], i, p ;

       cout << "Enter elements of vector(max 10)\n" ;

       for(i = 0 ; i < 10 ; i++ )

              cin >> vector[i] ;

       cout << "The vector originally is as follows :\n " ;

       for(i = 0 ; i < 10 ; i++ )

              cout << " " << vector[i] ;

       for(I = 0, p = 10 – 1 ; i < 10/2 ; i++, p-- )

       {        vector [i] = vector[i] + vector[p] ;

                 vector [p] = vector[i] – vector[p] ;

                 vector [i] = vector[i] – vector[p] ;

       }

Cout << "\nThe reversed vector is as follows :\n" ;

for(i = 0 ; i < 10 ; i++ )

       cout << " " << vector[i] ;

return 0 ;

}





Test Your Skills Now!
Take a Quiz now
Reviewer Name