0% found this document useful (0 votes)
26 views28 pages

CH 6

Uploaded by

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

CH 6

Uploaded by

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

CHAPTER 6

Arrays

• Arrays are a collection of the same data type values


which shares common name.
Types of arrays
There are two types of arrays:
1. One-dimensional(1D) array
2.Mult-dimensional array
1.One-dimensional(1D) array: A one-dimensional array
(or single dimension array) is a type of linear array.
Accessing its elements involves a single subscript which
can either represent a row or column index.
1
Declaration
• Like any other variable in C an array must be declared before it is used.
• Arrays may consist of any of the valid data types.
• Arrays are declared along with all other variables in the declaration
section of the program and the following syntax is used
Syntax:
data-type array-name[size];
int id[3]; /* declaration of array id to store 3 int values */
float id[5]; /* declaration of array id to store 5 float values */
• Notice that the array name must not be separated from the square
brackets containing the index.
• When declaring an array, the number of array elements should be
constant.
• During declaration consecutive memory locations are reserved for the
array and all its elements.
2
Initializing single dimensional Array
• The elements of an array can be initialized in two ways.
• In the first approach, the value of each element of the array is listed within
two curly brackets ({}) and a comma (,) is used to separate one value from
another.
• Syntax:
data type arry_name[size]={list of elements};
• For example:
m[5] = {55, 33, 86, 81, 67};
m[0]=55 m[1]=33 m[2]= 86
m[3]=81 m[4]= 67
Index always starts from 0
Note: If the list of initial elements is shorter than the
number of array elements, the remaining elements are
initialized to zero.
Eg a[3]={1,5};
a[0]=1 a[1]=5 a[2]=0
3
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,9,2,3, 4};
for(int i=0;i<5;i++)
{
printf(“%d \t”,a[i]);
}
getch();
}
4
• Reading 1D array :
Example :
int array[50];
for(int i=0;i<n;i++)
{
scanf(“%d”, &array[i])
}

• Printing 1D array
Example
for(i=0;i<n;i++)
{
printf(“%d”,array[i]);
} 5
Eg1. write a program to find min age values of 10 students
void main()
{
clrscr();
int i,age[10],min;
printf(“enter age values \n");
for(i=0;i<10;i++)
scanf("%d",&age[i]);
min=age[0];
for(i=1;i<=10;i++)
{
if(age[i]<min)
min=age[i];
}
printf(“min age is %d\n",min);
getch();
} 6
Eg2. write a program to sort a given array in descending order
void main()
{
clrscr(); //for printing result
int i,j,temp; for(i=1;i<=5;i++)
int A[5]={3,2,4,6,1}; {
for(i=1;i<=5;i++) printf(“%d”,A[i])
{ }
for(j=0;j<5;j++) getch();
{ }
if(A[j]<A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
7
Multi-Dimensional Arrays
• Multi-dimensional arrays have two or more index
values which are used to specify a particular
element in the array.
• Two-Dimensional Arrays :
• Two-dimensional arrays, the most common
multidimensional arrays, are used to store
information that we normally represent in table
form.
• Two-dimensional arrays, like one-dimensional
arrays, are homogeneous. This means that all of the
data in a two-dimensional array is of the same type.

8
• Syntax:
data-type array-name[size-1][size-2];
• The first index value size-1 specifies a row index, while size-2 specifies a
column index.
• Example:
float b[3][5];
char c[5][5];
int a[2][4];
• A useful way to picture a 2D array is as a grid or matrix. Picture array b as

9
Initializing Multi-Dimensional Arrays
• This procedure is entirely analogous to that used to initialize 1D arrays at
their declaration. For example, this declaration
int age[2][3]={4,8,12,19,6,-1};
• will fill up the array age as it is stored in emory. That is, the array is
initialized row by row. Thus, the above statement is equivalent to:
age[0][0]=4; age[0][1]=8; age[0][2]=12;
age[1][0]=19; age[1][1]=6; age[1][2]=-1;
• As before, if there are fewer initialization values than array elements, the
remainder are initialized to zero.
• To make your program more readable, you can explicitly put the values to be
assigned to the same row in inner curly brackets:
int age[2][3]={{4,8,12},{19,6,-1}};
age[0][0]=4 age[1][0]=19
age[0][1]=8 age[1][1]=6
age[0][2]=12 age[1][2]=-1

10
Example
int A[3][4] = {{8, 2, 6, 5}, //row 0
{6, 3, 1 ,0}, //row 1
{8, 7, 9, 6}}; //row 2
Memory for the array may be visualized as:

11
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int A[3][4] = {{8,2,6,5},
{6,3,1,0},
{8,7,9,6}};
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“ %d”, A[i][j]);
}
printf(“\n”);
}
getch();
}
12
• Reading 2D array Contd. • Printing 2D array
Example: • Example:
int array[5][5], for(i=0;i<n;i++)
i,j,n,m;
{
for(i=0;i<5;i++)
for(j=0;j<m;j++)
{
for(j=0;j<5;j++) {
{ Printf(“%d”, array[i][j]);
}
scanf(“%d”,&array[i] }
[j]);
}
}

13
Eg2. let A= 10 20 B= 4 1
30 40 , 2 4 find c=A+B
for(i=0;i<2;i++) //calculation
void main()
{
{
for(j=0;j<2;j++)
clrscr();
{
int i,j,temp;
C[i][j]=A[i][j]+B[i][j];
int A[2][2]={10,20,30,40};
}}
int B[2][2], C[2][2];
for(i=1;i<2;i++) //for printing result
//input for B
{
for(i=0;i<2;i++)
for(j=1;j<2;j++)
{
printf(“%2f”,C[i][j])
for(j=0;j<2;j++)
printf(“\n”);
{
}
scanf(“%d”,&B[i][j]);
getch();
}
}
}

14
Character handling functions
• Manipulate and check the character build in ctype.h( header file).
Function Meaning
1. isalpha(c) if ‘c’ is alphabet ,it gives true
Example: isalpha(x)=>true
isalpha(2)=>false
2. isdigit(c) if ‘c’ is a digit(0-9),it gives true
Example: isdigit(x)=>false
isdigit(2)=>true
3. isalnum(c) if ‘c’is either alphabet or a digit, it gives true
Example: isalnum(a)=>true
isalnum(2)=>true
4. isupper(c) if ‘c’ is upper case letter, it gives true
Example: isupper(a)=>false
isupper(A)=>true

15
Contd.

5. islower(c) if ‘c’ is lower case letter,it gives


true
Example: islower(a)=>true
islower(A)=>false
6. tolower(c) converts ‘c’ into lower case
Example: tolower(A)=>a
7. toupper(c) convert ‘c’ into upper case
Example: toupper(a)=>A

16
Example: WAP to read a lower case character
through the keyboard and print it upper case.
#
#
#
void main()
{
char c;
printf(“ enter a character\n”);
c=getchar();
printf(“upper case=%c”,toupper(c));
getch();
}
17
Exercise:
WAP to print a vowel or consonant for a given alphabet.
Solution:
#
#
# case ‘I’:
void main() case ‘O’:
{ case ‘U’: printf(“Vowel\n”);break;
char c; default:printf(“consonant\n”);
c=getchar();
}
if(isalpha(c))
}
{
switch(toupper(c))
else
{ printf(“not alphabet\n”);
case ‘A’: getch();
case ‘E’: }

18
Strings
• Strings are 1D arrays of characters. Strings must
be terminated by the null character '\0' which is
(naturally) called the end-of-string character.
• Array of characters or is a series of characters
treated as a single unit.
• Declaration:
char stringname[size];
Example:
char s1[10];=> declares a string s1 to store 10
characters.

19
Initialization of strings
• Example:
char mystring[]={‘H’,’e’,’l’,’l’,’o’,’\o’};
In this case we would have declared a string of characters (array) of 6 elements of
type char initialized with the characters that compose Hello plus a null
character ‘\o’;
Strings enclosed b/n double quotes have always a null character (‘\n’)
automatically appended at the end. i.e.
char mystring[]={‘H’,’e’,’l’,’l’,’o’,’\o’}; =
char mystring[]=“Hello”;

String I/O Functions


• There are special functions designed specifically for string I/O. They are
gets(string_name);
puts(string_name);
• The gets function reads in a string from the keyboard. When the user hits a
carriage return the string is inputted.
• The function puts displays a string on the monitor.
20
Reading a string:
gets(s1); or scanf(“%s”,s1);

Printing string With out &


puts(s1); or printf(“%s”,s1);

String handling functions


#include<string.h> header file is required.
Function Purpose
(a)strlen(s) Gives the length of the
string. i.e. number of
characters in the string.
Example: strlen(“DEC”);=>3;
21
(b) strcpy(s1,s2); copies string s1 to s2.
Syntax: strcpy(string1,string2);
• When this function executes, string2 is copied into
string1 at the beginning of string1. The previous
contents of string1 are overwritten.
Example: Char s2[4]=“DEC”;
=> s1=“DUC”;
strcpy(s2,s1);
=> s2=“DUC”;
(c) strcat(s1,s2) concatenates s2 at the end of s1;
Example: strcpy(s1,”DUC”);
strcpy(s2,”COLLEGE”);
strcat(s1,s2);=> s1 becomes
“DUCCOLLEGE”
22
(d) strcmp compare s1 and s2
- gives “0” is s1 & s2 are equal
- gives >0 if s1 is greater than s2
- gives <0 if s1 is less than s2
syntax: strcmp(string1,string2);
• String comparison is done character by character
using the ASCII numerical code
Example:
strcmp(“dec”,”dec”); => 0;
strcmp(“dec”,”ece”); => <0;
strcmp(“dec”,”abc”); => >0;

23
Exercise:
• WAP (a) to accept two string (s1 and s2)
(b) print the length of two strings
(c) copy 1st string value to s3
(d) append s2 at the end of s3
(e) compare s1 and s2.

24
Solution:
#
#
#<string.h>
void main()
{
char s1[10],s2[10],s3[20];
int L1,L2;
printf(“enter two strings\n”);
gets(s1);
gets(s2);
25
Contd.

L1= strlen(s1);
L2= strlen(s2);
printf(“the 1st string is %s\n”, s1);
printf(“length of 1st string=%d\n”,L1);
printf(“the 2nd string is %s\n”, s2);
printf(“length of 2nd string=%d\n”,L2);
strcpy(s3,s1);
strcat(s3,s2);
printf(“ s3=%s\n”,s3);
printf(“the d/ce b/n %s & %s is%d\n”,s1,s2,strcmp(s1,s2));
getch();
}

26
Passing arrays to functions
We can not pass an array as an argument but pass a pointer to an
array by specifying the array’s name without an index.

Example:
#include<stdio.h> void main()
#include<conio.h> {
int sum(int a[],int i) int m;
{ int a[5]={3,5,6,8,9};
int sum=0; m=sum(a,5);
for(i=0;i<=4;i++) printf(“\n%d”,m);
sum=sum+a[i]; getch();
return (sum); }
}

27
exercise
On
Lab 9

28

You might also like