0% found this document useful (0 votes)
6 views7 pages

mod1_array

Uploaded by

fkfor3521
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views7 pages

mod1_array

Uploaded by

fkfor3521
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Arrays

•An array is a collection of data that holds fixed number of values of same type.
•The size and type of arrays can not be changed after its declaration.
• Example: If you want to store marks of100 students you can create an array for it.
float marks[100];

Types of Arrays

1.One-dimensionalarrays.
2.Two-dimensionalarrays.
3.Multidimensionalarrays.

One Dimensional Array

•A list of item can be given one variable name using only one subscript and such a variable is
called a single subscripted variable or a one-dimensional array.
•Example:
float height[50];
int group[10];
char name[10];

Array Declaration

Syntax: data_type array_name [ size];

The data type specifies the type of the element that will be contained in the array, such as int,
float, or char and the size indicates the maximum number of elements that can be stored inside
the array.
int mark[5];
•Here we declared an array, with array name mark, type of array int for storing 5 values.

Accessing elements of an array

•Array elements are accessed by indices.


•int mark[5];
•The first element is mark[0] second element is mark[1] and so on.
•Arrays have 0 as the first index not 1. In this example, mark[0].
•If the size of an array is n, to access the last element, (n-1) index is used.
In this example, mark[4].
•If you try to access array elements outside of its bound, let say mark [10], the compiler may
not show any error. However, this may cause unexpected output (undefined behavior).

Array Initialization

After an array is declared, its elements must be initialized.


In C programming an array can be initialized either
•At compile time
•At run time

Compile Time Initialization

Method 1
int mark[5] = {78,45,12,89,56};
Method 2
int mark [ ] = {78,45,12,89,56};
Here, mark[0] is equal to 78
mark[1] is equal to 45
mark[2] is equal to 12
mark[3] is equal to 89
mark[4] is equal to 56

Run Time Initialization

•An array can also be explicitly initialized at run time.


•For example,
for(i=0; i<10; i++)
{
scanf(“%d”, &x[i]);
}
•Looping statements are used to initialize the values of the arrays one by one using assignment
operator or through the keyboard by the user.

Reading and Displaying elements of the array

#include<stdio.h>
void main()
{
int array[5], i;
//Reading elements of the array
printf("Enter 5 numbers to store them in array \n");
for(i=0; i<5; i++)
{
scanf("%d", &array[i]);
}
//Displaying the elements of the array
printf("Element in the array are: \n");
for(i=0; i<5;i++)
{
printf("Element stored at a[%d] =%d \n", i, array[i]);
}
}
Write a program to read the elements of a 1D array from user and add 10 to all elements in
the array.

#include<stdio.h>
void main()
{
int array[50], i, n;
printf("Enter the number of elements to be stored in the array");
scanf("%d", &n);
printf("Enter the numbers to store them in array \n");
for(i=0; i<n; i++)
{
scanf("%d", &array[i]);
array[i]=array[i]+10;
}
printf("Element in the array after adding 10 \n");
for(i=0; i<n; i++)
{
printf("%d \n", array[i]);}
}
}

Write a program to find sum and average of array

#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}

Write a program to read and display even numbers in array

#include<stdio.h>
void main()
{
int n, a[20],i;
printf("Enter the size of Array");
scanf("%d", &n);
printf("Enter the number");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<n;i++)
{
if(a[i]%2 == 0)
{
printf("%d\t", a[i]);
}
}
}

Write a program to read the elements of a 1D array from user and add 10 to all odd numbers
in the array.

Two-dimensionalArrays:

A two dimensional array can be considered as a table which will have m number of rows and n
number of columns.

•type arrayName[x][y];
•Eg: int x[3][5]

A two dimensional array x which contains three rows and three columns can be shown as
follows Two dimensional Arrays:

Thus,every element in the array a is identified by an element name of the form x[i][j], where ‘x’ is
the name of the array, and ‘i’ and ‘j’ are the subscripts that uniquely identifies each element in
‘x’.

Initializing Two-DimensionalArrays:

Following is an array with 3 rows, and each row has 4columns.


int a[3][4] ={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to the previous example
Int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};

#include<stdio.h>
int main()
{
Int a[5][2] ={{0,0},{1,2},{2,4},{3,6},{4,8}};
int i, j;
for(i=0;i<5;i++)
{
for(j=0; j<2;j++)
{
printf(“a[%d] [%d] =%d\n”,i,j,a[i][j]);
}
}
return0;
}

Output:
a[0][0]:0
a[0][1]:0
a[1][0]:1
a[1][1]:2
a[2][0]:2
a[2][1]:4
a[3][0]:3
a[3][1]:6
a[4][0]:4
a[4][1]:8

Reading and displaying a 2D array

#include<stdio.h>
void main()
{
/* 2D array declaration*/
int a[3][2];
/*Counter variables for the loop*/
int i, j;
/*Reading elements of 2D array*/
printf("Enter the elements of 2D array");
for(i=0; i<3; i++)
{
for(j=0;j<2;j++)
{
scanf("%d", &a[i][j]);
}
}
/*Displaying elements of 2D array*/
printf("The elements of 2D array are\n");
for(i=0; i<3; i++)
{
for(j=0;j<2;j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}

Matrix Addition

#include <stdio.h>
void main()
{
int r1, c1, r2, c2,i,j ,a[10][10], b[10][10], sum[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter the elements of first matrix\n");
for (i= 0; i< r1; i++)
for (j= 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &r2, &c2);
printf("Enter the elements of second matrix\n");
for (i= 0; i< r2; i++)
for (j= 0; j < c2; j++)
scanf("%d", &b[i][j]);
if(c1==c2 && r1==r2)
{
printf("Sum of entered matrices:-\n");
for (i= 0; i< r1; i++)
{
for (j= 0; j < c1; j++)
{
sum[i][j] = a[i][j] + b[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
}
else
printf("The matrices are not conformable for addition");
}

Sum of diagonal elements of a matrix

#include<stdio.h>
int main()
{
int mat[12][12];
int i,j,row,col,sum=0;
printf("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
//To add diagonal elements
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %d\n",sum);
return 0;
}

Row sum and column sum of a matrix


Transpose of a matrix
Matrix Multiplication

********************************************************************************

You might also like