Plant propagation: Sexual and Asexual propapagation.pptx
Ocs752 unit 2
1. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 1
DHANALAKSHMI COLLEGE OF ENGINEERING
Tambaram, Chennai
Department of Computer Science and Engineering
OCS752 INTRODUCTION TO C PROGRAMMING
Year / Sem : IV / VII
2 Marks Q & A
2. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 2
UNIT – II
ARRAYS
Introduction to Arrays – One dimensional arrays: Declaration – Initialization – Accessing elements –
Operations: Traversal, Insertion, Deletion, Searching – Two dimensional arrays: Declaration –
Initialization – Accessing elements – Operations: Read – Print – Sum – Transpose – Exercise
Programs: Print the number of positive and negative values present in the array – Sort the numbers
using bubble sort – Find whether the given is matrix is diagonal or not
PART – A
1. What is an array? Give an example. (A/M – 16, N/D – 16, A/M – 17, A/M –18, A/M – 19)
An array is a variable which is capable of holding fixed values of the same type in contiguous memory
locations.
Syntax: datatype arrayname [row size][column size];
Example:
int a[2][3];
2. What are the features of array? (N/D – 17)
Features of array
1) An array holds elements that have the same data type.
2) Array elements are stored in subsequent memory locations.
3) Two dimensional array elements are stored row by row in subsequent memory locations.
4) Array name represents the address of the starting element.
5) Array size should be mentioned in declaration. Array size must be a constant expression and not a
variable.
3. What are the advantages of using arrays? (A/M – 18)
Advantages of using arrays
1) Better and convenient way of storing data of the same data type with the same size
2) Allows to store known number of elements in it
3) Allocates memory in contiguous order.
4) Iterate arrays faster than other methods like linked list etc.
4. Given an array int a[10]={101,012,103,104,105,106,107,108,109,110}. Show the memory
representation and calculate its length. (N/D – 19)
10000 10002 10004 10006 10008 10010 10012 10014 10016 10018
101 012 103 104 105 106 107 108 109 110
3. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 3
Length= N* 2bytes
=10*2
= 20 bytes
5. Is it possible to have negative index in an array?
Yes, it is possible to have negative index in an array. Even if it is illegal to refer to the elements that are
out of array bounds, the compiler will not produce error because C has no check on the bounds of an
array.
6. Distinguish between array and pointer.
S. No. Array Pointer
1
Array allocates space automatically. Pointer is explicitly assigned to point to an
allocated space.
2 It cannot be resized. It can be resized using realloc().
3 It cannot be reassigned. Pointers can be reassigned.
4
Sizeof (array name) gives the number
of bytes occupied by the array.
Sizeof (pointer name) returns the number
of bytes used to store the pointer variable.
7. What is the need of an array? (A/M – 12)
We need to declare a set of variables that are of the same data type. Instead of declaring each variable
separately, we can declare all variables collectively in the format of an array. Each variable can be
accessed either through the array element references or through a pointer that refers the array.
8. List the disadvantages of array.
Disadvantages of array
1) The elements in an array must be of same data type.
2) The size of an array is fixed.
3) If we need more space at run time, it is not possible to extend the array.
4) The insertion and deletion operation in an array require shifting of elements that takes time.
9. What are the types of arrays?
Types of arrays
1) One dimensional Array
2) Two dimensional Array
3) Multi dimensional Array
10. Define – One Dimensional Array
One Dimensional Array is defined as the collection of data that can be stored under one variable name
using only one subscript.
4. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 4
Example:
main()
{
int a[3]={1,2,3};
print(“Array Value = %d”, a[1]);
}
Output
Array Value = 2
11. Define – Two Dimensional Array
Two Dimensional Array is defined as an array with two subscripts. Two Dimensional array enables us to
store multiple row of elements.
Example
main()
{
int a[2][2]={5,10,15,20};
print(“2D array value = %d”, a[1][0]);
}
Output
2D array value = 15
12. Distinguish between One Dimensional and Two Dimensional arrays.
S. No. One Dimensional array Two Dimensional array
1
One Dimensional array stores single list of
elements of similar data type.
Two Dimensional array is a list of lists. It
stores elements as an array of arrays.
2 Stores data as a list. Stores data in row-column format.
3 It is also called single dimensional array. It is multi dimensional array.
13. What are the limitations of One Dimensional array?
The limitations of One Dimensional array
1) It is difficult to initialize large number of array elements.
2) It is difficult to initialize selected array element.
14. What are the limitations of Two dimensional array?
The limitations of Two dimensional array
1) Deletion of any element from an array is not possible.
2) Wastage of memory when the size of array is large.
5. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 5
15. Why are array elements initialized?
After declaration, array elements must be initialized otherwise it holds garbage value. There are two
types of initialization
1) Compile time initialization (initialization at the time of declaration)
Example:
int a[5]={12,34,23,56,12};
2) Run time initialization (when the array has large number of elements it can be initialized at run
time)
Example:
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
16. Why is it necessary to give the size of an array in an array declaration?
When an array is declared, the compiler allocates a base address and reserves enough space in the
memory for all the elements of the array. Thus, the size must be mentioned in an array declaration.
17. What is traversing operation on an array?
In traversing operation of an array, each element of an array is accessed exactly once for processing.
This is also called visiting of an array.
Example:
void main()
{
int array[] = {2,4,6,8,9};
int i, n = 5;
printf("The array elements are:n");
for(i = 0; i < n; i++)
{
printf("array[%d] = %d n", i, array[i]);
}
}
Output
array[0] = 2
array[1] = 4
array[2] = 6
array[3] = 8
array[4] = 9
6. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 6
18. What is the value of b in the following program? (A/M – 12)
main()
{int a[5]={1,3,6,7,0};
int *b;
b=&a[2];
printf(“The value of b is %d”,*b);
}
Output:
The value of b is 6
19. How are array elements accessed?
Array elements are accessed by using an integer index. Array index starts with 0 and goes till the size of
array minus 1.
Example:
main()
{
int arr[3]={2,4,6};
printf(“arr[0] = %dt”, arr[0]);
printf(“arr[1] = %dt”, arr[1]);
printf(“arr[2] = %d”, arr[2]);
}
Output
2 4 6
20. Write a program in C for finding the sum of n numbers using array.
main()
{
int a[10], n, sum=0;
printf(“Enter total number of index value :”);
scanf(“%d”,&n);
for(int i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(int i=0;i<n;i++)
sum=sum+a[i];
printf(“Sum of array is =”, sum);
}
7. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 7
Output
Enter total number of index value: 5
2 4 6 8 10
Sum of array is = 30