If Else

ARRAY

  1. What is an Array?
  2. See Answer Ans: An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.

  3. When to use Arrays?
  4. See Answer Ans: when you have to use multiple variable with same data type and at contigous location.

  5. How will to find the size of array?
  6. See Answer Ans: You can use sizeof operator to find the size of array.

  7. What is Subscript?
  8. See Answer Ans: An array is a collection of like variables that share a single name. The individual elements of an array are referenced by appending a subscript, in square brackets, behind the name. The subscript itself can be any legitimate C expression that yields an integer value, even a general expression.

  9. How the array can be initialize?
  10. See Answer Ans: You can use for loop to initialize the array.

  11. What happen if we declare array using Keyword "static"?
    • eg:
      static int a[4];
      See Answer Ans: Because static variables are initialized once, you should initialize the array and then it will retain the value from previous call.

  12. How to declare 2-D Array?
  13. See Answer Ans: datatype variable_name [no. of rows][no. of columns]

  14. What is logic of greatest element of array?
  15. See Answer Ans: To find the greatest element in array, first intialize the variable with the first element of array.
    int max,a[100],n,i;
    max=a[0];
    for(i=0;i<n;i++){
    if(max<a[i])
    max=a[i];
    }
    cout<<"Max="<<max;

  16. What is logic of search element in array?
  17. See Answer Ans: 1.) Input size and elements in array from user. Store it in some variable say size and arr.
    2.) Input number to search from user in some variable say toSearch.
    3.) Define a flag variable as found = 0. I have initialized found with 0, which means initially I have assumed that searched element does not exists in array.
    4.) Run loop from 0 to size. Loop structure should look like for(i=0; i<size; i++).
    5.) Inside loop check if current array element is equal to searched number or not. Which is if(arr[i] == toSearch) then set found = 1 flag and terminate from loop. Since element is found no need to continue further. Outside loop if(found == 1) then element is found otherwise not.

  18. How many loops are used while multiplication of 2 matrix?
  19. See Answer Ans: 3

  20. How bubble sort works?
  21. See Answer Ans: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.

  22. Diff. b/w Array and Structure.
  23. See Answer Ans: 1.) An array is a collection of related data elements of same type. Structure can have elements of different types.
    2.) An array is a derived data type. A structure is a programmer-defined data type
    3.) Any array behaves like a built-in data types. All we have to do is to declare an array variable and use it. But in the case of structure, first we have to design and declare a data structure before the variable of that type are declared and used.
    4.) Array allocates static memory and uses index / subscript for accessing elements of the array. Structures allocate dynamic memory and uses (.) operator for accessing the member of a structure.