CH 6
CH 6
Arrays
• 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.
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”;
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