Unit-5 Arrays
Unit-5 Arrays
• UNIT-IV
• Arrays: Introduction to one dimensional and two
dimensional Arrays- Declaration, Initialization and
Accessing array elements, Array applications, C
programming examples.
• Strings: Introduction, String Input/output functions,
Declaration, Initialization and Accessing Strings, Array
of Strings, String Manipulation functions- strlen,
strcat, strcmp, strcpy, strrev, C programming
examples.
Definition of array
• Array- array is a collection of similar data types stored
contiguously in memory under the same name.
• Array- it is a collection of homogeneous elements.
• HOW TO CREATE AN ARRAY
• Before we create an array, we need to decide on two
properties:
• Element type: what kind of data will your array hold?
ints, double, chars? Remember, we can only choose one type.
• Array size: how many elements will your array contain?
• When we initially write our program, we must pick an array
size. An array will stay this size throughout the execution of
the program. In other words, we can change the size of an
array at compile time, but cannot change it at run-time.
• USING ARRAYS IN C
• In C, the arrays can be classified based on how
the data items are arranged for human
understanding
• Arrays are broadly classified into three
categories,
• 1. One dimensional arrays
• 2. Two dimensional arrays
• 3. Multi dimensional arrays
1. ONE DIMENSIONAL ARRAYS
• One dimensional array is a linear list consisting of
related and similar data items. In memory all the data
items are stored in contiguous memory locations one
after the other.
• syntax-
Datatype array_name[size];
Ex- int a[10];
created an array of 10 integers
float x[5];
created an array of 5 floating point values
double z[8];
created an array of 8 double values.
Referencing\ accessing array elements
• Array elements are accessed with subscript, the
number in the brackets following the array name.
• This number specifies the elements position in the
array.
• The subscript would be 0,1,...... n-1.
• // C program to read an array elements and
find the sum of the elements.
#include <stdio.h>
main()
{ int a[10];
int i, SIZE, total=0;
printf(“\n Enter the size of the array : “);
scanf(“%d”,&size);
for (i = 0; i < SIZE ; i++)
scanf(“%d”,&a[i]);
for (i = 0; i < SIZE ; i++)
total += a[i];
printf("Total of array element values is %d\n", total);
getch();}
Few important points about the array
• 1. an array is a collection of similar elements.
• 2. the first element in the array is numbered
from 0 to n-1.
• 3. while declaring an array we have to specify
type and size(dimension) of an array to the
compiler.
• 4. however big an array all the elements are
always stored in contiguous memory location.
Initializing array elements
We have four options for initializing one dimensional arrays.
But, first, REMEMBER THIS: Just like regular variables, arrays that have not been initialized may
contain “garbage values.”
But, now, the stakes are even higher. If you declare an array with 31 elements, and forget to initialize it,
you end up with 31 garbage values!
Option 1 Initializing all memory locations
If you know all the data at compile time, you can specify all your data within
brackets:
int x [5] = {75, 79, 82, 70, 68};
• during compilation, 5 contiguous memory locations are reserved by the
compiler for the variable temp and all these locations are initialized as shown
in Fig. If the size of integer is 2 bytes, 10 bytes will be allocated for the
variable temp.
•
• x[0] x[1] x[2] x[3] x [4]
•
For example:
int x [5] = {5};
x[0] x[1] x[2] x[3] x [4]
• int x[4][3]={
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
• int x[3][2]={1,2,3,4,5,6};
• int x[][2]={1,2,3,4,5,6};
• int x[2][]={1,2,3,4,5,6}; Never work
• int x[][]={1,2,3,4,5,6};
Initializing 2D arrays
char names[3][4] = {
{ 'J', 'o', 'h', 'n'} ,
{ 'M', 'a', 'r', 'y'} ,
• An integer array may be initialized to
{ 'I', 'v', 'a', 'n'}
all zeros as follows:
};
int nums[3][4] = {0};
• This only works for zero.
nums[0][0] = 16;
• To access an element of a 2D
printf("%d", nums[1][2]);
array, you need to specify both the
row and the column: ...
'J' 4269
Address
'J' 'o' 'h' 'n' is stored: 'o' 4270
'M' 'a' 'r' 'y' 'h' 4271
'n' 4272
'M' 4273
'a' 4274
• Two-dimensional arrays in C are stored in "row-major format":
the array is laid out contiguously, one row at a time: 'r' 4275
'y' 4276
...
3. Multidimensional array
• C allows arrays of three or more dimensions.
• Syntax
Data type array_name[size1][size2]..[sizen];
main()
{
char name[30];
printf(“enter the string”);
gets(name);
}
Writing\ display strings on to a screen
• We can use printf() function to display string on
to a screen with %s format specifier.
Printf(“%s”,name);
main()
{
char name[30];
puts(“enter the string”);
gets(name);
puts(name);
}
Print a string using loops
/* Program to demonstrate printing of a string */
main( )
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( i <= 7 )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}
With the help of null character
main( )
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}
String Library Functions
Function Library Description
strcpy(s1, s2) string.h Copies the value of s2 into
s1
strcat(s1, s2) string.h Appends s2 to the end of s1
strrev(s1) string.h reverse characters of s1
onto the other string
strcmp(s1, s2) string.h Compared s1 and s2
alphabetically; returns a
negative value if s1 should
be first, a zero if they are
equal, or a positive value if
s2 should be first
strlen(s1) string.h Returns the number of
characters in s1 not counting
the null
strcpy()
• Since C never lets us assign entire arrays, we use the strcpy
function to copy one string to another:
#include <string.h>
#include<stdio.h>
main()
{
char string1[] = "Hello, world!";
char string2[20];
strcpy(string2, string1);
puts(string2);
}
The above code copies the contents of string1 to string2.
strcmp()
• The standard library's strcmp function compares two strings, and returns 0
if they are identical, or a negative number if the first string is
alphabetically ``less than'' the second string, or a positive number if the
first string is ``greater.'' Here is an example:
#include <string.h>
#include<stdio.h>
main()
{
char string3[] = "this is";
char string4[] = "a test";
if(strcmp(string3, string4) == 0)
printf("strings are equal\n");
else
printf("strings are not equal\n");
}
strcat()
The standard library function is strcat, which concatenates strings. It does not
concatenate two strings together and give you a third, new string; what it really
does is append one string onto the end of another. Here's an example:
#include <string.h>
#include<stdio.h>
main()
{
char string5[20] = "Hello, ";
char string6[] = "world!";
printf("%s\n", string5);
strcat(string5, string6);
puts(string5);
}
The first call to printf prints ``Hello, '', and the second one prints ``Hello, world!'',
indicating that the contents of string6 have been tacked on to the end of string5.
Notice that we declared string5 with extra space, to make room for the appended
characters.
strlen()
The length of the string can be found by using the
function strlen() function.
#include <string.h>
#include<stdio.h>
main()
{
char string7[] = "abc";
Int len;
len = strlen(string7);
printf("%d\n", len);
}