SlideShare a Scribd company logo
1 of 7
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
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
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.
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.
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
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);
}
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

More Related Content

What's hot

8085 interrupts
8085 interrupts8085 interrupts
8085 interrupts
Ram Babu
 
Microprocessor vs. microcontroller
Microprocessor vs. microcontrollerMicroprocessor vs. microcontroller
Microprocessor vs. microcontroller
aviban
 
fpga programming
fpga programmingfpga programming
fpga programming
Anish Gupta
 
Data manipulation instructions
Data manipulation instructionsData manipulation instructions
Data manipulation instructions
Mahesh Kumar Attri
 

What's hot (20)

Ec8791 lpc2148 pwm
Ec8791 lpc2148 pwmEc8791 lpc2148 pwm
Ec8791 lpc2148 pwm
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
PRESENTATION ON REAL ADDRESSING MODE AND VIRTUAL ADDRESSING MODE
PRESENTATION ON REAL ADDRESSING MODE AND VIRTUAL ADDRESSING MODEPRESENTATION ON REAL ADDRESSING MODE AND VIRTUAL ADDRESSING MODE
PRESENTATION ON REAL ADDRESSING MODE AND VIRTUAL ADDRESSING MODE
 
8085 interrupts
8085 interrupts8085 interrupts
8085 interrupts
 
Chapter 7 8051 programming in c
Chapter 7  8051 programming in cChapter 7  8051 programming in c
Chapter 7 8051 programming in c
 
Arm assembly language programming
Arm assembly language programmingArm assembly language programming
Arm assembly language programming
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
RISC - Reduced Instruction Set Computing
RISC - Reduced Instruction Set ComputingRISC - Reduced Instruction Set Computing
RISC - Reduced Instruction Set Computing
 
java practical file index
 java practical file  index java practical file  index
java practical file index
 
Quality attributes of Embedded Systems
Quality attributes of Embedded Systems Quality attributes of Embedded Systems
Quality attributes of Embedded Systems
 
Embedded C - Lecture 4
Embedded C - Lecture 4Embedded C - Lecture 4
Embedded C - Lecture 4
 
Introduction to stm32-part1
Introduction to stm32-part1Introduction to stm32-part1
Introduction to stm32-part1
 
Microprocessor vs. microcontroller
Microprocessor vs. microcontrollerMicroprocessor vs. microcontroller
Microprocessor vs. microcontroller
 
ALGORITHMIC STATE MACHINES
ALGORITHMIC STATE MACHINESALGORITHMIC STATE MACHINES
ALGORITHMIC STATE MACHINES
 
fpga programming
fpga programmingfpga programming
fpga programming
 
ADC Interfacing with pic Microcontrollert
ADC Interfacing with pic MicrocontrollertADC Interfacing with pic Microcontrollert
ADC Interfacing with pic Microcontrollert
 
Intel x86 and ARM Data types
Intel x86 and ARM Data typesIntel x86 and ARM Data types
Intel x86 and ARM Data types
 
Data manipulation instructions
Data manipulation instructionsData manipulation instructions
Data manipulation instructions
 
EDLC-EMBEDDED PRODUCT DEVELOPMENT LIFE CYCLE
EDLC-EMBEDDED PRODUCT DEVELOPMENT LIFE CYCLEEDLC-EMBEDDED PRODUCT DEVELOPMENT LIFE CYCLE
EDLC-EMBEDDED PRODUCT DEVELOPMENT LIFE CYCLE
 

Similar to Ocs752 unit 2

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Kumar Boro
 
Aae oop xp_05
Aae oop xp_05Aae oop xp_05
Aae oop xp_05
Niit Care
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 

Similar to Ocs752 unit 2 (20)

UNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptxUNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptx
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
Arrays
ArraysArrays
Arrays
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
 
strings.ppt
strings.pptstrings.ppt
strings.ppt
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Aae oop xp_05
Aae oop xp_05Aae oop xp_05
Aae oop xp_05
 
Unit 2
Unit 2Unit 2
Unit 2
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Cunit3.pdf
Cunit3.pdfCunit3.pdf
Cunit3.pdf
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Arrays
ArraysArrays
Arrays
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Ch8 Arrays
Ch8 ArraysCh8 Arrays
Ch8 Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Pooja
PoojaPooja
Pooja
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
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