Unit – 1 – Array.
1 – Marks
Please follow me for more practicals....
Q1:
Definition of an Array.
An
array is defined as a collection of similar type of data stored at memory
location.
Q2:
How to declare a 1-D array in C?
Syntax
to declare a 1-D array:
datatype
arrayName[size];
e.g.
int marks[5];
Q3:
How to initialize a 1-D array in C programming?
Syntax
to initialize a 1-D array:
datatype
arrayName[size] = {value1, value2, …, valueN};
e.g.
int marks[5] = {88,65,34,75,99};
Q4:
List out types of an array.
Type
of array are:
1. Single Dimensional Array.
2. Multi Dimensional Array.
Q5:
What is one dimensional array in C?
In
C Programming language single dimensional arrays are used to store list of
values of same datatypes, they store row values in linear form.
Q6:
What are two dimensional arrays in C?
2
- Dimensional arrays are used to store the data in the form of table that is in
rows and columns format. We use 2-D arrays to create mathematical matrices.
Q7:
What is Multi-dimensional array in C?
An
array of an array is called multi-Dimensional Array. An array is created with
more than one dimensional (size) is called multi-dimensional array.
Q8:
How to initialize a 2-D array in C programming?
Syntax
to initialize a 2-D array:
Datatype
arrayName[rowSize][columnSize]
=
{
{R1C1value1, R1C2value2, …, R1CNvalueN},
{R2C1value1, R2C2value2, …, R2CNvalueN},
….
};
e.g.
int matrix[2][3] = { {1,2,3}, {4,5,6} };
Please follow me for more practicals....
Q9:
What are the basic operations of arrays?
Basics
Operations on Array are:
1.
Searching.
2.
Sorting.
3.
Traversing.
4.
Inserting.
5.
Deleting.
Q10:
What is Run time initialization of array?
Run
time initialization of array means the initialization of an array elements
while the program execution by the user.
Q11:
How to declare a 2-D array in C?
Syntax
to declare a 2-D array:
Datatype
arrayName[rowSize][columnSize];
e.g.
int matrix[2][3];
Q12:
Write an Example of an Array using while loop.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int marks[5] = {88, 75, 62, 95, 56}, i = 0;
while (i < 5)
{
printf("%d ", marks[i]);
i++;
}
getch();
}
Please follow me for more practicals....
2-Marks.
Q1:
How to create an Array?
1st
declare an array using following syntax:
datatype
arrayName[size];
e.g.
int marks[5];
2nd
initialize an array with elements:
arrayName[index]
= element;
e.g.
marks[0] = 99;
Q2:
How can we initialize 2D arrays? Explain with examples.
Syntax
to initialize a 2-D array:
Datatype
arrayName[rowSize][columnSize] = { {R1C1value1, R1C2value2, …, R1CNvalueN},
{R2C1value1, R2C2value2, …, R2CNvalueN}, …};
e.g.
int matrix[2][3] = { {1,2,3}, {4,5,6} };
Q3:
Write a Difference Between one-dimensional and two-dimensional array.
One-Dimensional
array stores data only in a row format.
Whereas
Two-Dimensional array stores the data in tabular format that is row and column
format.
Please follow me for more practicals....
Q4:
Write Applications of Arrays.
Applications
of arrays are:
It
is used to store values of the same data type.
For
mathematical problems.
For
Matrix Operations.
For
String Manipulation.
To
implement Search Algorithms.
Arrays
are used to implement Data structures.
Q5:
How can we initialize Multidimensional arrays? Explain with examples.
Syntax
to initialize a Multi-Dimensional array:
Datatype
arrayName[rowSize][columnSize] = { {R1C1value1, R1C2value2, …, R1CNvalueN},
{R2C1value1, R2C2value2, …, R2CNvalueN}, …};
e.g.
int matrix[2][3] = { {1,2,3}, {4,5,6} };
Q6:
Write a Difference Between two-dimensional and multi-dimensional array.
One-Dimensional
array stores data only in a row format.
Whereas
Multi-Dimensional array stores the data in tabular format that is row and
column format.
Q7:
What is mean by transpose? Give the example of it.
Transpose
means to interchange the rows and columns of the original matrix to the new
matrix.
e.g.
a = {{1,2,3}, {4,5,6}};
new
matrix after transpose = {{1,4}, {2,5}, {3,6}};
Q8:
Write a program to read n×n matrix.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int arr[50][50], rowSize, columnSize, i, j;
printf("Enter the row size of an array = ");
scanf("%d", &rowSize);
printf("Enter the column size of an array = ");
scanf("%d", &columnSize);
for (i = 0; i < rowSize; i++)
{
for (j = 0; j < columnSize; j++)
{
printf("Enter arr[%d][%d] = ", i,
j);
scanf("%d", &arr[i][j]);
}
}
printf("\nArray Elements:\n");
for (i = 0; i < rowSize; i++)
{
for (j = 0; j < columnSize; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
getch();
}
Please follow me for more practicals....
3-Marks.
Q1:
Define an Array. What are the types?
An
array is defined as a collection of similar type of data stored in contiguous
memory locations. Each element in the array is accessed by its index.
There
are mainly two types of arrays:
1. 1-Dimensional array.
e.g. int numbers[5];
2. Multi-Dimensional array.
int matrix[3][3];
Q2:
Write an Advantages and Disadvantages of an Array.
Advantages:
Code
optimization: Less
code to access the data.
Easy
access: To element
by using the for loop we can retrieve the element of an array easily.
Shorting
of data: To short
the element of an array we need a few line of code only.
Random
access: We can access any element randomly using the array.
Disadvantages:
Fixed
Size: It has fixed
size, specify the size of the array at the time of declaration.
Array
is homogeneous: Array
can store elements of only one type of data type.
No
Built-in Bounds Checking: No built-in bounds checking on array accesses
Inefficient
Insertions and Deletions: Insertions and deletions can be inefficient when dealing with large
arrays.
Memory
Wastage: They are
static data structures, means they occupy a fixed amount of memory even if the
array is not fully used.
Q3:
Write Properties of an Array. Which are operations of an array?
Properties
of an Array are:
-
An array holds the element that have same datatype.
-
Array elements are storing in subsequent memory location.
-
Array name repeats that address of the storing element.
-
A character array initialized with double quoted string.
-
All elements of an array can be changed separately.
Operations
on array are:
1.
Accessing Elements
2.
Insertion
3.
Deletion
4.
Traversal
5.
Searching
6.
Sorting
7.
Merging
8.
Splitting
9.
Filtering
10.
Mapping
11.
Reducing/Aggregating
Q4:
Explain Single Dimensional Array with its Syntax.
In
C Programming language single dimensional arrays are used to store list of
values of same datatypes, they store row values in linear form.
Syntax:
Declaring
an array: datatype arrayName[size];
e.g.
int marks[5];
Initializing
an array: datatype arrayName[] = {val1, val2, …., valn};
e.g.
int marks[5] = {98,85,67,53,85};
Please follow me for more practicals....
5-Marks.
Q1:
Write a program to declare 1-D array and assign values to array elements.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int marks[5] = {88, 75, 62, 95, 56}, i = 0;
while (i < 5)
{
printf("%d ", marks[i]);
i++;
}
getch();
}
Q2:
Why we need Array in C Programming?
We
need array in C Programming for the following reasons:
It
is used for less code to access the data.
It
is used to access the elements easily.
It
is used to short a data.
It
is used to store values of the same data type.
For
mathematical problems.
For
Matrix Operations.
For
String Manipulation.
To
implement Search Algorithms.
Arrays
are used to implement Data structures.
Q3:
Write a program to insert an element in 1-D array at specified place.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int arr[100], i = 0, size, position, element;
printf("\nEnter the size of an array = ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\nEnter the element to add = ");
scanf("%d", &element);
printf("Enter the position to add = ");
scanf("%d", &position);
for (i = size - 1; i >= position; i--)
{
arr[i + 1] = arr[i];
}
arr[position] = element;
size += 1;
for (i = 0; i < size; i++)
{
printf("\narr[%d] = %d", i, arr[i]);
}
getch();
}
Q4:
Write a program to delete an element from 1-D array.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int arr[100], i, size, position;
printf("Enter the size of an array = ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\nEnter the position to delete the element = ");
scanf("%d", &position);
for (i = position; i < size; i++)
{
arr[i] = arr[i + 1];
}
size -= 1;
for (i = 0; i < size; i++)
{
printf("\narr[%d] = %d", i, arr[i]);
}
getch();
}
Q5:
Write C Programs for Sorting from 1-D array.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int arr[100], i, j, size;
printf("Enter the size of an array = ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter arr[%d] = ", i);
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
for (j = i; j < size; j++)
{
if (arr[i] > arr[j])
{
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}
}
for (i = 0; i < size; i++)
{
printf("\narr[%d] = %d", i, arr[i]);
}
getch();
}
Please follow me for more practicals....
Q6:
Write a C program to Sum of two matrices from 2-D array.
#include
<stdio.h>
#include
<conio.h>
void
main()
{
int arr1[50][50], arr2[50][50], arr3[50][50], i, j, size;
printf("Enter the size of an array = ");
scanf("%d", &size);
printf("\nEnter the elements of 1st array:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("Enter arr[%d][%d] = ", i,
j);
scanf("%d", &arr1[i][j]);
}
}
printf("\nEnter the elements of 2nd array:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("Enter arr[%d][%d] = ", i,
j);
scanf("%d", &arr2[i][j]);
}
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
arr3[i][j] = arr1[i][j] + arr2[i][j];
}
}
printf("\n1st array elements:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}
printf("\n2nd array elements:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d ", arr2[i][j]);
}
printf("\n");
}
printf("\n3rd array elements:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d ", arr3[i][j]);
}
printf("\n");
}
getch();
}
Q7:
List out array operations in an array and explain any two of them.
Operations
on array are:
1.
Accessing Elements
2.
Insertion
3.
Deletion
4.
Traversal
5.
Searching
6.
Sorting
7.
Merging
8.
Splitting
9.
Filtering
10.
Mapping
11.
Reducing/Aggregating
Insertion: Inserting an element into an array
at a specified index, shifting existing elements to accommodate the new one.
Deletion: Removing an element from the
array, which usually involves shifting subsequent elements to fill the gap
created by the removal.
Q8:
Explain One Dimensional Array with its Example.
In
C Programming language single dimensional arrays are used to store list of
values of same datatypes, they store row values in linear form.
Syntax:
Declaring
an array: datatype arrayName[size];
e.g.
int marks[5];
Initializing
an array: datatype arrayName[] = {val1, val2, …., valn};
e.g.
int marks[5] = {98,85,67,53,85};
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[] = {1, 2, 3, 4, 5};
printf("%d\n%d\n%d\n%d\n%d\n",
arr[0], arr[1], arr[2], arr[3], arr[4]);
getch();
}