0% found this document useful (0 votes)
31 views37 pages

Outline of This Presentation

The document outlines the key points about arrays including: - Arrays allow storing and accessing related data in a generalized way through indices. - A single dimensional array stores elements in contiguous memory locations. - Common array operations include traversing elements, inserting/deleting elements, searching, and updating elements. - Multidimensional arrays can represent matrices for operations like addition, subtraction, and multiplication.

Uploaded by

Bharani Dharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views37 pages

Outline of This Presentation

The document outlines the key points about arrays including: - Arrays allow storing and accessing related data in a generalized way through indices. - A single dimensional array stores elements in contiguous memory locations. - Common array operations include traversing elements, inserting/deleting elements, searching, and updating elements. - Multidimensional arrays can represent matrices for operations like addition, subtraction, and multiplication.

Uploaded by

Bharani Dharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Outline of this presentation

• Introduction to arrays
• Single dimensional arrays
• Reading, Storing and Accessing elements of a
one-dimensional array
• Memory representation of single dimensional
array
• Operations on a single dimensional array
• programs
Introduction to Arrays
• Consider a problem to find the average of marks secured by five
students in a course.

#include<stdio.h>
int main()
{
int mark1=10, mark2=12, mark3=9, mark4=11, mark5=17;
int sum;
float average;
sum=mark1+mark2+mark3+mark4+mark5;
average=sum/5.0;
printf("Average marks secured is %f",average);
return 0;
• The powerful iteration statements discussed
have not been used here to sum up the marks
secured by the students because the marks are
stored in separate variables and it is not possible
to access them in a generalized way.
• Since there are only five students, it is possible
to find the average in the above mentioned
manner.
• Now suppose there are 200 students in a course.
• For a problem of this scale, it is not feasible to
create separate variables for storing the marks
and finding the average in the above mentioned
manner.
• To solve such problems, a method is required
that helps in storing and accessing data in a
generalized and an efficient manner.
• The C language provides this method in the
form of a derived data type known as array
type or just array.
• An array enables the user to store the
characters of the entered name in a
contiguous set of memory locations, all of
which can be accessed by only one name, i.e.
the array name.
Arrays
Important points about array
• An array (data structure) is a collection of
elements of the same data type.
• The individual elements of an array are not
named. All the elements of an array share a
common name.
• The position of an element in an array is
specified with an integer value known as index
or subscript.
• The array index in C starts with 0.
• The memory space required by an array can
be computed as (size of element type) x
(Number of elements in an array).
• Arrays are always stored in contiguous
(continuous) memory locations.
• Data structure is a logical representation of
data.
• It provides systematic mechanisms for
storage, retrieval and manipulation of data.
• Example of data structures are: arrays, stacks,
queues, linked lists, trees, etc.
Single dimensional array
• The size specifier specifies the number of
elements in an array. The syntactic rules about
the size specifier are as follows:
• It should be a compile time constant
expression of integral type.
• It should be greater than or equal to one.
• The size specifier is mandatory if an array is
not explicitly initialized, i.e. if an intialization
list is not present.
• Like variables can be initialized, similarly the
elements of an array can also be initialized.
Program 01
Program 2
• Write a C program to calculate average of the
marks obtained by ‘n’ number of students. Get
the value of ‘n’ and mark of each student from
the user. Then calculate the average and print
it.
Program 02
#include<stdio.h>
int main()
{
int marks[200],i,n,sum=0;
float average;
printf("Enter the number of students in a class\n");
scanf("%d",&n);
printf("Enter marks of students \n");
for (i=0;i<n;i++)
{
printf("Enter marks of student%d \n",i+1);
scanf("%d",&marks[i]);
}
for (i=0;i<n;i++)
{
sum=sum+marks[i];
}
average=(float)sum/n;
printf("Average marks of the class is %f \n",average);
return 0;
}
Basic Operations on array
1. Traverse − print all the array elements one by
one.
2. Insertion − Adds an element at the given index.
3. Deletion − Deletes an element at the given
index.
4. Search − Searches an element using the given
index or by the value.
5. Update − Updates an element at the given
index.
Program 3
• Write a program to insert an array element in
the desired position.
• Step-1: Get the array from the user.
• Step-2: Get the position and value of the
element which needs to be inserted.
• Step-3: Print the array after insertion.
Insertion program
Output
Program 4
• Write a program to delete an array element
with the desired position.
• Step-1: Get the array from the user.
• Step-2: Get the position of the element which
needs to be deleted from the user.
• Step-3: Print the array after deletion.
Deletion program
Output
Traverse Operation
• To traverse an array means to access
each element (item) stored in the array
so that the data can be checked or
used as part of a process.
• In traversing operation of an array, each
element of an array is accessed exactly
for once for processing. This is also
called visiting of an array.
Update array element
• Write a program to update an array element in
the desired position.
• Step-1: Get the array from the user.
• Step-2: Get the position and value of the
element which needs to be updated.
• Step-3: Print the array after updation.
Update array element
Linear search
• Given a list of n elements and a key. Find
whether the given key exists in the list or not.
If it exists, print its position in the list.
Linear search
Sorting
#include <stdio.h> /* sorting begins ... */
void main ()
{ for (i = 0; i < n; ++i)
{
int number[30]; for (j = i + 1; j < n; ++j)
{
int i, j, a, n; if (number[i] > number[j])
printf("Enter the value of N\n"); {
scanf("%d", &n); a = number[i];
number[i] = number[j];
printf("Enter the numbers \n"); number[j] = a;
for (i = 0; i < n; ++i) }
scanf("%d", &number[i]); }
}

printf("The numbers arranged in


ascending order are given below\n");

for (i = 0; i < n; ++i)


{
printf("%d\n", number[i]);
}

}
Two dimensional array
Program
Matrix addition & Subtraction
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");


for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
}
#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of the matrices:\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}
• #include <stdio.h>

int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of a matrix\n");


scanf("%d%d", &m, &n);
• printf("Enter elements of the matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];

printf("Transpose of the matrix:\n");

for (c = 0; c < n; c++) {


for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}
}

You might also like