SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados (20)

best notes in c language
best notes in c languagebest notes in c language
best notes in c language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
String c
String cString c
String c
 
C++ oop
C++ oopC++ oop
C++ oop
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C notes
C notesC notes
C notes
 
C++
C++C++
C++
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Array in c#
Array in c#Array in c#
Array in c#
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Array in C
Array in CArray in C
Array in C
 
Array and string
Array and stringArray and string
Array and string
 
C++ interview question
C++ interview questionC++ interview question
C++ interview question
 
Structures
StructuresStructures
Structures
 

Semelhante a 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
 
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
 
Pooja
PoojaPooja
Pooja
 

Último

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
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Ữ Â...Nguyen Thanh Tu Collection
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
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.docxRamakrishna Reddy Bijjam
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
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)Jisc
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
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...Amil baba
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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Ữ Â...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
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
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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)
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.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...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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