CP Unit 5
CP Unit 5
com
COMPUTER PROGRAMMING UNIT-5
UNIT-5
Array
Array: Array is a user defined datatype. Array is group of elements, these elements are
homogeneous. Each element shared common name elements are variable with their index
value.
Syntax: datatype arrayname[size];
Use of arrays:
1. Storing more than one value at a time under a single name.
2. Reading, processing and displaying the array elements is easy.
3. Some logics can be implemented only through arrays.
Declaration of arrays:
Syntax: datatype arrayname[size];
Example: int a[10];
Declaration of arrays with datatype, arrayname and size of the array. Size denotes the
maximum number of elements that can be stored in the array.
Accessing elements (initialization of array elements)
Access the array elements in two ways:
1. Design time initialization
2. Run time initialization
int a[10]={1,2,3,4,5,6,7,8,9,10};
Organization of the elements in memory
elements 1 2 3 4 5 6 7 8 9 10
index 0 1 2 3 4 5 6 7 8 9
address 100 102 104 106 108 110 112 114 116 118
The index begin with 0(zero) and ends with one less than the size of the array.
Runtime accessing:
Read the array elements at time of execution from the keyboard using input
statements.
Example:
int a[10];
for(i=0;i<=9;i++)
{
scanf(“%d”,&a[i]);
}
The above loop execute 10times and input the different array elements. For example
entered elements are 5 10 15 20 25 30 35 40 45 50.
Elements
5 10 15 20 25 30 35 40 45 50
index 0 1 2 3 4 5 6 7 8 9
address 200 202 204 206 208 210 212 214 216 218
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf(“Enter array elements \n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Display the array elements \n”);
for(i=0;i<10;i++)
{
printf(“%d”,a[i]);
}
getch();
}
Storing elements:
Declaration and definition only reserve space for the elements in the array. No values
are stored. If we want to store values in the array, we must initialize the elements, read values
from the keyboard or assign values to each individual element.
int sample[5]={22,55,33,77,88};
Sample array have five space for store the five different elements. The elements are
stored in array based on the index. For example 22 stored at sample[0], 55 stored at
sample[1], 33 stored at sample[2], 77 stored at sample[3] and 88 stored at sample[4].
One-dimensional array:
In one dimensional array, the organization of data is only one direction, because 1-D
has only one dimension.
Example: datatype arrayname[size];
int a[10];
In the above statement array name „a‟ has 10 integer locations, „a‟ occupied memory
is 20 bytes (2*10).
Organization of the elements in memory.
a[0] a[1] --------- a[9]
elements
26 14 93 6 7 9 13 43 12 54
index 0 1 2 3 4 5 6 7 8 9
address 100 102 104 106 108 110 112 114 116 118
str[0] str[19]
Elements
M o t h i l a l \0 - - - - - - -
Index 0 1 2 3 4 5 6 7 8
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf(“Enter array elements \n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Entered array elements are\n”);
for(i=0;i<10;i++)
{
printf(“4%d”,a[i]);
}
getch();
}
main()
{
char name[20]=”vijaya”;
clrscr();
printf(“My name is : %s”, name);
getch();
}
a[0][1]
a[0][0] a[0][2]
1
0 1 2 a[1][2]
a[1][0] a[1][1]
Declaration of 2-D:
rows columns
At design time:
datatype arrayname[r-size][c-size]={list values};
int a[2][3]={5,10,15,20,25,30};
Or
int a[2][3]={{5,10,15},{20,25,30}};
At runtime:
datatype arrayname[r-size][c-size];
int a[2][2],i,j;
for(i=o;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
2-D character arrays:
2-D chaarater arrays are nothing but an array(collection) of strings.
Declaration:
char str[5][30];
Initialization:
char str[3][30]={“Lakshmi”,”Ramya”,”Kalyani”};
Stored on:
0 L a k s h m i \0 --------------
1
R a m y a \0 --------------
2
K a l y a n i \0 --------------
0 1 2 3 4 5 6 7 8 29
Initialization:
Declaration and definition only reserve space for the elements in the array.
No values will be stored in the array. If we want to store values, we must either initialize the
elements, read values from the keyboard or assign values to each individual element.
int table[3][5][4]=
{
{{0,1,2,3},{10,11,12,13},{20,21,22,23},{30,31,32,33},{40,41,42,43}}
{{100,101,102,103},{200,201,202,203},{300,301,302,303},
{400,401,402,403},{500,501,502,503} }
{{110,111,112,113},{210,211,212,213},{310,311,312,313},{410,411,412,413},
{510,511,512,513}}
};
void main()
{
char str[128];
int i,count=0;
clrscr();
puts(“Enter a sentence”)
gets(str);
for(i=0;str[i]!=0;i++)
{
if(str[i]==‟ „)
count++;
}
printf(“Number of words=%d”,count+1);
}
2. sample program
main()
{
int a[10],i;
clrscr();
printf(“Enter array elements \n”);
for(i=0;i<2;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Elements\t Index \t Address \n”);
for(i=0;i<2;i++)
{
printf(“%d\t %d\t %d\t\n”,a[i],I,&a[i]);
}
getch();
}
3.Asending order:
#include<stdio.h>
main()
{
int i,j,k;
float n[10],t=0;
clrscr();
printf(“Enter any 10 number: \n”);
for(i=0;i<10;i++)
{
scanf(“%f”,&n[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(n[i]>=n[j])
{
t=n[i];
n[i]=n[j];
n[j]=t;
}
}
}
printf(“The asending order of the given number is : \n”);
for(i=0;i<=9;i++)
printf(“%7.5f”,n[i]);
getch();
}
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf(“Enter array elements \n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
printf(“The elements in reverse order\n”);
for(i=9;i>=0;i--)
{
printf(“%d”,a[i]);
}
getch();
}
main()
{
int number[10],i,large=0,small=32767;
clrscr();
printf(“Enter array elements \n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
if(number[i]>large)
large=number[i];
}
for(i=0;I,=10;i++)
{
if(number[i]<small)
small=number[i];
}
printf(“Largest value is %d”,large);
printf(“Smallest value is %d”,small);
getch();
}
7.
#include<stdio.h>
#include< string.h>
main()
{
char str[20];
int i;
clrscr();
printf(“Enter a string in uppercase”);
gets(str);
for(i=0;str[i]!=‟\0‟)
{
if(str[i]>=‟A‟&&str[i]<=‟2‟)
str[i]=str[i]+32;
}
printf(“The converted string is %s”,str);
getch();
}
C – Strings:
A c-string is a variable length array of characters that is delimited by the null
character.
Storing strings:
In c, a string is stored in an array of characters. It is terminated by the null characters
(„\O‟)
H E L L O \0
String delimiter:
Why do we need a null character at the end of a string?
Answer is that a string is not a data type but a data structure. This means that its
implementation is logical, not physical.
Difference between the strings and character arrays
H E L L O \0 HELLO
Example:
“Have a nice day”
“Hello”
“Hai”
“Prasad”
Strings:
Strings concepts :
String
Length Delimited
controlled
Fixed length strings: When implementing a fixed length string format the first decision is
the size of the variable. If we make it too small, we can‟t store all the data. If we make it too
big, we waste memory.
Variable length strings: In variable length strings the user can use the required memory
only. So reduce the memory wastage and data losses.
1. Length controlled strings: Length controlled strings add a count that specifies the
number of characters in the string.
2. Delimited strings: Another techniques used to identify the end of the string is the
delimiter at the ends of delimited strings.
Example:
„a‟ a as character
“a” a as string
„ „ empty character
“ “ empty string
Declare strings: String declaration defines memory for a string when it is declared as an
array in local memory.
1 2 3 4 5 6 7 8 9 10 11
Syntax: 1. char str[9]; str
Syntax: 2. string pointer declaration.
char *pstr; pstr
Initializing strings: We can initialize a string the same way that we initialize any storage
structure by assigning a value to it when it is defined.
C provide two basic ways to read and write strings. First, we can read and write
strings with the formatted input/output functions.
scanf/fscanf and printf/fprintf
Second, we can use a special set of string only functions.
getstring (gets/fgets)
putstring (puts/fputs)
Example program:
main()
{
char str[9];
clrscr();
printf(“Enter your string”);
scanf(“%s”,str);
printf(“Your string is %s”,str);
getch();
}
main()
{
char str[10];
clrscr();
puts(“Enter your name”);
gets(str);
puts(“Entered name is “);
puts(str);
getch();
}
Output: Enter your name
Prasad
Entered name is
Prasad.
Character input and output functions:
Character input functions:
1. getchar()
2. getche()
3. getch()
Above three statements used for read a single character from keyboard at a time.
getchar(): getchar() function is used read a character from the keyboard and assigned the
character to variable, the variable stored the character.
Syntax: variable=getchar ();
Example program:
#include<stdio.h>
main()
{
char c;
printf(“Enter your character\n”);
c=getchar();
printf(“Entered character is \n”);
putchar(c);
getch();
}
getch():
It is also read the characters from the keyboard and passes it immediately to the
program with echoing on the screen.
Example program:
#include<stdio.h>
main()
{
char ch;
clrscr();
printf(“enter a character\n”);
ch=getch();
printf(“enter character is %c”,ch);
getch();
getche():
Accepts a character and passes it to the program and echos(displays) the same
character on the screen also.
Example program:
#include<stdio.h>
main()
{
char ch;
printf(“Enter your character \n”);
ch=getche();
printf(“Entered character is \n”);
putchar(ch);
getch();
}
Syntax: putchar(variable);
Example program:
#include<stdio.h>
main()
{
char ch;
clrscr();
printf(“Enter your character \n”);
ch=getchar();
printf(“Your character is \n”);
putchar(ch);
getch();
}
Function Description
strlen(): This functions returns the number of character of a string data as an integer.
Syntax: strlen(variable);
Example program:
#include< string.h>
main()
{
char str[20];
int i;
clrscr();
puts(“Enter your string”);
gets(str);
i=strlen(str);
printf(“Length of the given string is %d”,i);
getch();
}
strcpy(): This function is used to copy the data of one string to another string variable.
Syntax: strcpy(stringvariable,stringvariable1);
The string in the second position is the source string that contains the data to be
copied. The string in the first position is destination string that receives the data.
Example program:
#include< string.h>
main()
{
char str[20],str1[20];
clrscr();
puts(“Enter ur string”);
gets(str);
strcpy(str1,str);
puts(str);
puts(str1);
getch();
}
The string in the first position is called destination string and the string in the second
position is called source string the source is joined with the destination and the combined
string is stored the destination itself.
Example program:
#include<string.h>
main()
{
char str1[20],str2[20];
clrscr();
puts(“Enter both string1 and string2”);
gets(str1);
gets(str2);
strcat(str1,str2);
puts(str1);
puts(str2);
getch();
}
Syntax: strcmp(stringvariable1,stringvariable2);
Example program:
#include< string.h>
main()
{
int i;
char str1[20],str2[20];
clrscr();
puts(“Enter both strings”);
gets(str1);
gets(str2);
i=strcmp(str1,str2);
printf(“Difference is %d”,i);
getch();
}
strrev(): This function is used to get the reverse string of a given string.
Syntax: strrev(string);
Example program:
#include< string.h>
main()
{
char str[20];
clrscr();
puts(“Enter a string”);
gets(str);
strrev(str);
puts(“Reverse string of the given string is”);
puts(str);
getch();
}
strlwr(): This function is used to convert the uppercase string to lower case string.
Syntax: strlwr(string);
Example program:
#include< string.h>
main()
{
char str[20];
clrscr();
puts(“Enter the string in uppercase”);
gets(str);
strlwr(str);
puts(str);
getch();
}
strupr(): This function is used to convert the lowercase string to uppercase string.
Syntax: strupr(string);
Example program:
#include< string.h>
main()
{
char str[20];
clrscr();
puts(“Enter the string in lowercase”);
gets(str);
strlwr(str);
puts(str);
getch();
}
#include< string.h>
main()
{
char str[20]str1[20];
clrscr();
puts(“Enter a string ”);
gets(str);
strcpy(str1,str);
strrev(str);
i=strcmp(str,str1);
if(i==0)
puts(“Given string is palindrome”);
else
puts(“Given string is not palindrome”);
getch();
}
#include< string.h>
main()
{
char str[20]str1[20];
clrscr();
Ppts(“Enter a string ”);
gets(str);
puts(“Enter find string”);
gets(str1);
if(strstr(str,str1))
puts(“Yes it is in the string ”);
else
puts(“Not found”);
getch();
}