SlideShare a Scribd company logo
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

DOC
Ocs752 unit 1
DOC
Ocs752 unit 4
DOC
Ocs752 unit 3
DOC
Ocs752 unit 5
PPTX
Function in c program
PPTX
PPT
C Language
Ocs752 unit 1
Ocs752 unit 4
Ocs752 unit 3
Ocs752 unit 5
Function in c program
C Language

What's hot (20)

PPTX
Abstract class in c++
PPTX
Object Oriented Programming Concepts
PPTX
Functions in c++
PPTX
C programming interview questions
PPTX
Android Intent.pptx
PPTX
Formatted Console I/O Operations in C++
PDF
Computer graphics lab manual
PPTX
C if else
PDF
Deep C
PPTX
Python-Polymorphism.pptx
PPTX
Conditional Statement in C#
PPTX
Function Pointer
PPTX
Steps for c program execution
PPT
FUNCTIONS IN c++ PPT
PPT
OOP in C++
DOC
C AND DATASTRUCTURES PREPARED BY M V B REDDY
PDF
C++ interview question
PPT
Object Oriented Language
PPT
Polymorphism
Abstract class in c++
Object Oriented Programming Concepts
Functions in c++
C programming interview questions
Android Intent.pptx
Formatted Console I/O Operations in C++
Computer graphics lab manual
C if else
Deep C
Python-Polymorphism.pptx
Conditional Statement in C#
Function Pointer
Steps for c program execution
FUNCTIONS IN c++ PPT
OOP in C++
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C++ interview question
Object Oriented Language
Polymorphism
Ad

Similar to Ocs752 unit 2 (20)

PDF
Cunit3.pdf
PPTX
Array 2 hina
PPTX
ARRAYS.pptx
PPT
Arrays Basics
PDF
Arrays
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPTX
Arrays_and_Strings_in_C_Programming.pptx
PPTX
array.pptx ppt ggsipu bpit delhi rohini.
PDF
Array&amp;string
PPTX
Array
PPTX
Array
PPTX
Arrays
PPTX
Module_3_Arrays - Updated.pptx............
PPTX
Chapter 13.pptx
PDF
Array in C full basic explanation
PPTX
Arrays.pptx
PDF
Array Data Structure for programing language
PPTX
Arrays & Strings
PDF
Unit ii data structure-converted
PPT
Mesics lecture 8 arrays in 'c'
Cunit3.pdf
Array 2 hina
ARRAYS.pptx
Arrays Basics
Arrays
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Arrays_and_Strings_in_C_Programming.pptx
array.pptx ppt ggsipu bpit delhi rohini.
Array&amp;string
Array
Array
Arrays
Module_3_Arrays - Updated.pptx............
Chapter 13.pptx
Array in C full basic explanation
Arrays.pptx
Array Data Structure for programing language
Arrays & Strings
Unit ii data structure-converted
Mesics lecture 8 arrays in 'c'
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
master seminar digital applications in india
PDF
Insiders guide to clinical Medicine.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
RMMM.pdf make it easy to upload and study
PPTX
Lesson notes of climatology university.
PDF
Basic Mud Logging Guide for educational purpose
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
Final Presentation General Medicine 03-08-2024.pptx
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
master seminar digital applications in india
Insiders guide to clinical Medicine.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial disease of the cardiovascular and lymphatic systems
VCE English Exam - Section C Student Revision Booklet
Renaissance Architecture: A Journey from Faith to Humanism
RMMM.pdf make it easy to upload and study
Lesson notes of climatology university.
Basic Mud Logging Guide for educational purpose
human mycosis Human fungal infections are called human mycosis..pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.

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