0% found this document useful (0 votes)
39 views11 pages

Chapter 6 Arrays

Chapter 6 Arrays

Uploaded by

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

Chapter 6 Arrays

Chapter 6 Arrays

Uploaded by

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

ARRAYS

Definition
▪ An array is a group of related data items that share a
common name. For instance, we can define array name
salary to represent a set of salary of a group of
employees.
▪ A particular value is indicated by writing a number called
index number or subscript in brackets after the array
name.
▪ Structures of related data items
▪ Static entity – same size throughout program
▪ Dynamic data structures

▪ Eg: salary[10]
Name of array (Note that
▪ Structure of Array all elements of this array
have the same name, c)
▪ Array elements are like
normal variables
c[ 0 ] = 3; c[0] -45
c[1] 6
printf( "%d", c[ 0 ] );
c[2] 0

▪ Perform operations in c[3] 72


c[4] 1543
subscript. If x equals 3 c[5] -89
c[6] 0
c[ 5 - 2 ] == c[ 3 ]== c[ x
c[7] 62
]
c[8] -3
c[9] 1
c[10] 6453
c[11] 78

Position number of the


element within array c
Declaring Arrays

▪ When declaring arrays, specify


 Name
 Type of array
 Number of elements
arrayType arrayName[numberOfElements or Size ];
▪ The type specifies the type of element that will be
contained in the array, such as int, float, or char
▪ The size indicates the maximum number of elements
that can be stored inside the array.
▪ Examples:
int c[ 10 ];
float myArray[ 3284 ];
Initializers of Arrays
▪ The general form of initialization of arrays is:
static type array-name[size] = {list of values}
int n[ 5 ] = { 1, 2, 3, 4, 5 };
▪ If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 }
 All elements 0
▪ If too many a syntax error is produced syntax error
 C arrays have no bounds checking
▪ If size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 };
 5 initializers, therefore 5 element array
▪ If the number of values in the list is less than the number of
elements, then only that many elements will be initialized.
The remaining elements will be set to zero automatically.
Types of Arrays

▪ One Dimensional Arrays


▪ Two Dimensional Arrays
▪ Multidimensional Arrays
One Dimensional Arrays
/*Program showing one-dimensional
array*/
main()
{ OUTPUT
int i; Enter 10 real numbers:
float x[10],value,total;
printf(“Enter 10 real numbers:\n”);
for(i =0; i < 10; i++) 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
10.10
{
x[1] = 1.10
scanf(“%f”,&value);
x[2] = 2.20
x[i] = value;
x[3] = 3.30
}
x[4] = 4.40
total = 0.0; x[5] = 5.50
for(i = 0; i < 10; i++) x[6] = 6.60
total = total + x[i] * x[i]; x[7] = 7.70
printf(“\n”); x[8] = 8.80
for(i = 0; i < 10; i++) x[9] = 9.90
printf(“x[%2d] = %5.2f \n”,i+1,x[i]); x[10] = 10.10
printf(“\nTotal = %.2f\n”,total); Total = 446.86
}
Two Dimensional Arrays

▪ Two-dimensional arrays are declared as


follows
type array-name[row_size][column_size];

▪ Eg: product[i][j] = row * column;


#include<stdio.h>
#include<conio.h> for(r=0; r<3; r++)
main() {
{ for(c=0; c<3; c++)
int a[3][3],b[3][3],c1[3][3];
int r,c,k;
{
printf("Enter first matrix elements c1[r][c]=a[r][c] + b[r][c];
: "); }
for(r=0; r<3; r++) }
{ printf("New addition matrix :
for(c=0; c<3; c++) \n");
{
scanf("%d",&a[r][c]);
for(r=0; r<3; r++)
} {
} for(c=0; c<3; c++)
printf("Enter second matrix printf(" %d",c1[r][c]);
elements: "); printf("\n");
for(r=0; r<3; r++)
{ }
for(c=0; c<3; c++) getch();
{
scanf("%d",&b[r][c]); }
}
}
MULTIDIMENSIONAL ARRAY

▪ C allows arrays of three or more dimensions. The


exact limit is determined by the compiler.
▪ The general form of a multidimensional array is
type array_name[s1][s2][s3]…s[m];

▪ Eg: 1. int survey[3][5][12];

2. float table[5][4][5][3];
Contd…

▪ Initialization Of three-dimensional Array


▪ double cprogram[3][2][4]={
{ {-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2} },
{ {0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1} },
{ {8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0} } };
▪ Suppose there is a multidimensional array
arr[i][j][k][m]. Then this array can hold i*j*k*m
numbers of data.
▪ Similarly, the array of any dimension can be
initialized in C programming.

You might also like