C Array ?



Why Need Array?


void main()
{
 int r1,r2,r3,r4;
 print("\n Enter Roll Number of 4 students");
 scanf("%d%d%d%",&r1,&r2,&r3,&r4);
 printf("%d%d%d%d",r1,r2,r3,r4);
}

The above program works fine however, if we want ot read roll number of 50 students , then we have to declare 50 or more variables, process them and print them. this is a very tedious task due to the following reasons.

  • Difficult To declare more number of variables.
  • Difficult to process large amount of data easily.
  • Difficult to write program to read data into more number of variables and difficult to print the data stored in more number of variables
  • the length of program even if the number of variables increases.
  • We can reduce the time to type, modify and correct.
  • it is not a good programming style
  • Consumes too much time to type modify and correct

What is solution for this problem ?

Array Can Solve this problem

What is Array ?


Array is a collection of similar data items. All elements of the array share a common name each elements in the array can be accessed by the index. Array is used to store, process and print large amount of data using a single variable.


Example of Array:-



steps-shineskill
Types of array:-

  1. Single dimensional arrays
  2. Multi dimensional arrays

Basic properties of Array:-

  • The array elements should be of the same data types
  • The data items are stored contiguously in memory.
  • The subscript of first item is always zero

Note:- The amount of memory to be allocated is decided during compilation but not during execution. So, size has to be specified and it must be evaluated to an integer value greater than or equal to 1. When we declare and define an array, its size must be known.


Applications of Array

  • Array Store large amount of same data types.
  • Arrays can be used for CPU scheduling.
  • Used to Implement other data structures like Stacks, Queues, Heaps, Hash tables, etc.

C-MCQ Question


Q1.Arrays can be considered as a set of elements stored in consecutive memory locations but having __________.
  1. Same data type
  2. Different data type
  3. Same scope
  4. None of these

Answer:- a.

Q2.Array is an example of _______ type memory allocation.
  1. Compile time
  2. Run time
  3. Both A and B
  4. None of the above

Answer:- a.

Q3.Size of the array need not be specified, when
  1. Initialization is a part of definition
  2. It is a formal parameter
  3. It is a declaration
  4. All of the above

Answer:- a.

Q4.Array elements are always stored in ______ memory locations.
  1. Random
  2. Sequential
  3. Sequential and Random
  4. None of the above

Answer:- a.


Q4. Let x be an array. Which of the following operations are illegal?
  1. ++x
  2. x+1
  3. x++
  4. x*2

Answer:- a.


Q4. What are the advantages of arrays?
  1. Objects of mixed data types can be stored
  2. Elements in an array cannot be sorted
  3. Index of first element of an array is 1
  4. Easier to store elements of same data type

Answer:- d.