0% found this document useful (0 votes)
35 views8 pages

Unit 3 Introduction Programming

Uploaded by

shabanasarwani
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)
35 views8 pages

Unit 3 Introduction Programming

Uploaded by

shabanasarwani
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/ 8

UNIT III

Arrays and Strings


Arrays indexing, memory model, programs with array of integers, two dimensional arrays,
Introduction to Strings.
Arrays:
C supports a derived data type known as array. An array is a fixed-size sequenced collection of
elements of the same data type. It is simply a grouping of like-type data. In its simplest form, an array
can be used to represent a list of numbers, or a list of names.
Some examples where the concept of an array can be used:

1. List of temperatures recorded every hour in a day, or a month, or a year.


2. List of employees in an organization.
3. List of products sold by a store.
4. Test scores of a class of students.
5. List of customers.
6. Table of daily rainfall data, etc.
Types:
1. One-dimensional arrays.
2. Multidimensional arrays.
Single/One-Dimensional Arrays
The one dimensional arrays are also known as Single dimension array and is a type of Linear
Array. In the one dimension array the data type is followed by the array name which is further
followed by the size (an integer value) enclosed in square brackets (subscript) and this represents the
dimension either row wise or column wise.
Declaration:
Syntax
data_type array_name [SIZE];

 Where data type can be any basic or user defined data type.
 Array name should follow the rules of an identifier.
 SIZE takes an integer value that specifies the number of adjacent cells required for that array.

Example:
int a[10];
Where a is the name of the array, the integer value 10 indicates the size of the array i.e., 10 adjacent
cells are allocated with each cell storing integer element as the data type is int.
The memory is allocated with index starting from zero and ends with its size-1. Pictorially the
memory allocation and its identification is represented as shown below:

20 bytes
References a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

Addresses 1000 1002 1004 1006 1008 1010 1012 1014 1016 1018

Fig: Memory model


Referencing the Array:
An array element can be accessed using reference.
Syntax
arrayname[index];
a[0] is a first reference , used to access first element of the array
a[1] is a second reference , used to access second element of the array.
|
|
a[9] is a tenth reference , used to access tenth element of the array.
Index is used to access the particular element in an array, index starts from 0 and end at size – 1.Index
is stored inside square brackets.
Initializing array elements:
1. Using = and {} brackets all at a time.
Syntax:
data_type array_name [SIZE] ={value1, value2,….,valueN};
Example:
int a[10]={10,20,30,40,50,60,70,80,90,100};

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018

Fig: Memory model after initialization.


2. Using = one at a time.
Syntax:
array_name [index]=value;
Example:
int a[10];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
a[5]=60;
a[6]=70;
a[7]=80;
a[8]=90;
a[9]=100;
3.Using loops:
int number[10];
for ( i=0; i < 10 ; i++ )
{
scanf( “ %d ”, & number[ i ] );
}
/* Sample program to illustrate the declaration, initialization and accessing elements of the array */
#include <stdio.h>
#include <conio.h>
void main()
{
int number[10]={1,2,3,4,5,6,7,8,9,10}; /* array declaration and initialization*/
int i;
clrscr();
/* printing the elements of the array */
printf(“elements in the array are:\n”);
for(i=0; i<10; i++)
{
printf(“%d\t”,number[i]);
}
getch();
}
Output:
elements in the array are:
1 2 3 4 5 6 7 8 9 10
/* Sample program to illustrate the declaration, reading and printing elements of the array */
#include <stdio.h>
#include <conio.h>
void main()
{
int number[10] /* array declaration */
int i;
clrscr();
/* reading the elements of the array */
printf(“Enter the elements into the array are:\n”);
for(i=0; i<10; i++)
{
scanf(“ %d ”, &number[i]);
}
/* printing the elements of the array */
printf(“elements in the array are:\n”);
for(i=0; i<10; i++)
{
printf(“%d\t”,number[i]);
}
getch();
}
Enter the elements into the array are:
1 2 3 4 5 6 7 8 9 10
elements in the array are:
1 2 3 4 5 6 7 8 9 10
Multi-dimensional arrays
C programming language allows multidimensional arrays.
Declaration:
Syntax:
type name[size1][size2]...[sizeN];

Example:
int threedim[5][10][4];
Two-Dimensional Arrays:
The simplest form of the multidimensional array is the two-dimensional array.
Declaration:
Syntax:
type arrayName [size1][ size2 ];

Where type can be any valid C data type and arrayName will be a valid C identifier.
A two-dimensional array can be think as a table which will have x number of rows and y number of
columns.
Example:
int a[3][4];

Fig. Memory model


Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of
the array, and i and j are the subscripts that uniquely identify each element in array a.
Initializing Two-Dimensional Arrays:
1. Using = and {} brackets all at a time.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to
previous example:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

0 1 2 3
4 5 6 7
8 9 10 11
Fig. Array after initialization.
2.Using = one at a time.
int a[3][4];
a[0][0]=0;
a[0][1]=1;
a[0][2]=2;
a[0][3]=3;
a[1][0]=4;
a[1][1]=5;
a[1][2]=6;
a[1][3]=7;
a[2][0]=8;
a[2][1]=9;
a[2][2]=10;
a[2][3]=11;
3. Using loops:
int a[3][4];
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
Scanf(“%d”,&a[i][j]);
}
}
Accessing Two-Dimensional Array Elements:
An element in 2-dimensional array is accessed by using the reference, using, row index and column index of
the array. For example:
int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array.
//program on two dimensional array.
#include <stdio.h>
#include <conio.h>
int main( )
{
/* an array with 3 rows and 4 columns*/
int a[3][4] = { {0,1,2,3}, {3,5,6,7}, {8,9,10,11}};
int i, j;
clrscr( );
/* output each array element's value */
printf("The array elements are:\n");
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 4; j++ )
{
printf("%d \t", a[i][j] );
}
printf("\n");
}
getch();
}
Output:
The array elements are:
0 1 2 3
3 5 6 7
8 9 10 11
Introduction to Strings
Strings are sequence of characters enclosed in double quotes. By default a string gets terminated with null
character (\0) marking the end of the string and that character is not visible to the user.
Whereas a character is enclosed in single quote and it is not terminated with any default character.
Example:
printf(“welcome to strings”); // “welcome to strings” is string constant.
Declaration of string variables.
The declaration of string variable is very much similar to declaring a character array.
Syntax:

char stringname[size];

Example:
char name[20];
where name is the name of the string.
Initialization character array:
1. Using string.
Syntax:
char stringname[size]=”value”;
where value can be sequence of characters.
Exmple:
char name[20]=”learn strings”;
From the above declaration, “name” is the name of the string or character array and “learn strings” is the
value initialized to the character array “name” and by default string is terminated with null character(\0)
marking the end of the string. Below we will see how the string is initialized and also see how memory is
allocated.

Name of Name
the array
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Value l e a r N s t r i n g s \0
2. Using characters.
Example:
char name[20]={‘l’,’e’,’a’,’r’,’n’,’ ‘,’s’,’t’,’r’,’i’,’n’,’g’,’s’,’\0’};
Reading a string to the character array:
If we suppose that a character array is declared as shown below:
char college_name[25];
then a string can be read using the scanf statement as shown below:
scanf(“%s”,college_name);
or
for(i=0;i<25;i++)
scanf(“%c”,&college_name[i]);
}
Printing a string from the character array:
If we suppose that a character array is declared as shown below:
char college_name[25];
the contents of the character array can be printed as string as shown below:
printf(“%s”,college_name);
or
for(i=0;i<25;i++)
scanf(“%c”, &college_name[i]);
or
for(i=0;i<25;i++)
printf(“%s”,college_name[i]);

String Library Functions:


C supports a wide range of functions that manipulate strings. The most common are listed here:

Name Function
strcpy(s1, s2) Copies s2 into s1
strcat(s1, s2) Concatenates s2 onto the end of s1
strlen(s1) Returns the length of s1
strcmp(s1,s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2
strchr(s1, ch) Returns a pointer to the first occurrence of ch in s1
strstr(s1, s2) Returns a pointer to the first occurrence of s2 in s1

These functions use the standard header <string.h>. The following program illustrates the use of these string
functions:
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main(void)
{
char s1[80], s2[80];
clrscr();
printf("Enter first string:");
gets(s1);
printf("Enter second string:");
gets(s2);
printf("lengths: %d %d\n", strlen(s1), strlen(s2));
if(!strcmp(s1, s2)) printf("The strings are equal\n");
strcat(s1, s2);
printf ("%s\n", s1);
strcpy(s1, "This is a test.\n");
printf(s1);
if(strchr("hello", 'e')) printf("e is in hello\n");
if(strstr("hi there", "hi")) printf("found hi");
getch();
return 0;
}
Output:
Enter first string:hello
Enter second string:hello
lengths: 5 5
The strings are equal
hellohello
This is a test.
e is in hello
found hi
Array of Strings:
array of strings is 2-D array of characters in which each row is one string.
Example:
char weekdays[7][12]={“Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”,”Sun”};
#include<stdio.h>
#include<conio.h>
int main()
{
// declaring and initializing 2D String
char weekdays[7][12]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
clrscr();
// Dispaying strings
printf("Week days are:\n");
for(int i=0;i<7;i++)
puts(weekdays[i]);
getch();
return 0;
}
Output:
Week days are:
Mon
Tue
Wed
Thu
Fri
Sat
Sun

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy