0% found this document useful (0 votes)
4 views16 pages

PPS L 20 Array

The document provides an educational overview of arrays in programming, specifically focusing on their definition, types, applications, and memory representation. It includes detailed explanations of one-dimensional and multi-dimensional arrays, along with examples of declaration and initialization in C language. Additionally, it covers string handling and common string library functions, emphasizing their importance in programming for problem-solving.

Uploaded by

sradha9305
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)
4 views16 pages

PPS L 20 Array

The document provides an educational overview of arrays in programming, specifically focusing on their definition, types, applications, and memory representation. It includes detailed explanations of one-dimensional and multi-dimensional arrays, along with examples of declaration and initialization in C language. Additionally, it covers string handling and common string library functions, emphasizing their importance in programming for problem-solving.

Uploaded by

sradha9305
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

Subject : - Programming For Problem Solving

e-Content-L 20: Array


Self – Declaration
It is hereby declared that the e-content “L-20:-Array” for [Link].-1 sem, Programming For Problem Solving, is
exclusively meant for academic purposes and for enhancing the teaching and learning. Any other use for
economic/commercial purpose is strictly prohibited .The users of the content shall not distribute, disseminate, or
share it with anyone else and its use is restricted to the advancement of individual knowledge. The content, being
related to teaching and learning of under-graduated courses are prepared with the help of existing literature available
in different forms through offline and online portals. The content of the course are authentic and best as of my
knowledge.

Complied by:-

([Link] Sharma)

Department of Computer Science Engg


SCRIET,C.C.S. University, Meerut
Mob: 8171707777
Email-ritu_14aug@[Link]
Array or Subscript Variable
 An array is a collection of similar data type elements stored in contiguous memory locations . In
another words we can say that an array is a type of data structure that stores a fixed-size of a homogeneous
collection of data. In short, we can say that array is a collection of variables of the same type.

 An array is also termed as subscript variable. Here first element is stored as 0th location.
 Till the array is not initialized it contains garbage value while if partially initialized it contains zero.
 Array name is base address of array (base address is the address of first element of array).
 The array is the simplest data structure where each data element can be randomly accessed by using its index
number. C array is beneficial if you have to store similar elements. For example, if we want to store the marks
of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject.
Instead of that, we can define an array which can store the marks in each subject at the contiguous memory
locations.
 Elements of the array can be randomly accessed since we can calculate the address of each element of the array
with the given base address and the size of the data element.
 Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow
the size dynamically like Linkedlist.

Application of Array
 Searching- It is the process of finding the location of specific element in an array of n elements. e.g. linear
search, binary search
Sorting- It is the process of arranging the elements in specific order in an array of n elements. e.g. bubble sort

Types of Array

(1)1-D Array or One-Dimensional Array or Linear Array or Single Dimensional Array

(2)Multi-dimensional Array

(i) 2-D Array or Two-Dimensional Array or Matrix

(ii) 3-D Array or Three-Dimensional Array

(1)One-dimensional Array:- An array having one subscript or dimension is termed as 1-D 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. We don't need to specify the size of the
array if the declaration and initialization are being done simultaneously.

Declaration of Array

Syntax:- datatype array variable name[n];

eg :- int a[5];
here, 5 represents number of elements.

Initialization of Array

Syntax:- array variable[5]={value1,value2,………….value n};

eg:- int a[5]={67,12,33,40,5,};


so the value of a[0]=67 ,a[1]=12 ,a[2]=33 ,a[3]=40 ,a[4]=5

Memory Representation of Single Dimensional Array

Base address of an array a[0] a[1] a[2] a[3] a[4]

67 12 33 40 5

Q (1) Write a program in C language to take a value from user in single dimensional
array and display these values.

Q(2) W.A.P. in C language to add the 10 numbers by using the single dimensional array.
Q (2) Write a program in C language to take a string from user in single dimensional array
and display the string?
Q(3) Write a program in C language to display the characters by using single dimensional
array.
(2)Multi-dimensional Array : C language allows for arrays of two or more dimensions. A two-
dimensional (2D) array is an array of arrays. A three-dimensional (3D) array is an array of arrays of arrays.
In C programming an array can have two, three, or even ten or more dimension.

Two Dimensional Array in C


The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array can be considered
as a matrix which will have x number of rows and y number of columns. The two dimensional (2D) array in
C programming is also known as matrix. A matrix can be represented as a table of rows and columns. The two-
dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented
as the collection of rows and columns. It provides ease of holding the bulk of data at once which can be passed to any
number of functions wherever required.

Declaration of two dimensional Array in C

The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];

for example :- int arr[4][3];

Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of 2D Array in C

In the 1D array, we don't need to specify the size of the array if the declaration and initialization are being done
simultaneously. However, this will not work with 2D arrays. We will have to define at least the second dimension of
the array. The two-dimensional array can be declared and defined in the following way.

int arr[2][2]={
{10,23},
{55,30},
};
Memory Representation of Two-Dimensional Array

Row 1 arr[0][0] 10 23 arr[0][1]

Row 2 arr[1][0] arr[1][1]


55 30

Column 1 Column 2

Q(4) Write a program to take a values in two dimensional array and display these values?

Q(5)Write a program in C language to add two matrices?


Q(6) Write a program in C language to multiply two matrices?
Three-Dimensional Array: A 3D array is a multi-dimensional array(array of arrays). A 3D array is a collection
of 2D arrays. It is specified by using three subscripts:- Block or table size, row size and column size. More dimensions
in an array means more data can be stored in that array.
Accessing elements in Three-Dimensional Arrays is also similar to that of Two-Dimensional Arrays. The difference
is we have to use three loops instead of two loops for one additional dimension in Three-dimensional Arrays.
In similar ways, we can create arrays with any number of dimension. However the complexity also increases as the
number of dimension increases. The most used multidimensional array is the Two-Dimensional Array.

Declaration of Three-Dimensional Array


Syntax :- data type array variable name [t][r][c];
For example:- int arr[3][3][3];

 int shows that the 3D array is an array of type integer.


 arr is the name of 3D array.
 first dimension represents the block size or table size.
 second dimension represents the rows size.
 third dimension represents the columns size.

Initialization in Three-Dimensional Array:- Initialization in Three-Dimensional array is same as that of Two-


dimensional arrays. The difference is as the number of dimension increases so the number of nested braces will also
increase.

For example:- arr[3][3][3]={ columns

{89,67,56} rows

{45,60,34}
Table 1 or
Block 1 {34,45,23,}

},

{34,45,23}
Table 2 or
{23,12,11}
Block 2
{20,78,79}

},

{41,65,70}
Table 3 or
Block 3 {50,90,77}

{56,89,60]

};

Memory Representation in Three Dimensional Array

Table 1 or Block 1

Table 2 or Block 2

Table 3 or Block 3
STRING

 A character type array is termed as string which is terminated by null character. Also the collection of
characters (alphanumeric) enclosed in double quote and terminate by null character is known as string.
 Each character occupy 1 byte and stored at contiguous memory location, start from 0 location.
 Size of string should always exceed by one while declaration of string.
String Declaration
char s[10];
where s is the name of string which can store 10 values of character type (9 character + 1 null character)
Initialization of string
char s[10]= {‘h’,’e’,’l’,’l’,’o’,’\o’}; char s[10]= “hello”;

Receiving string elements


scanf(“%s”,s) --> for single word
gets(s); --> for multiple word
Printing string elements
printf(“%s”,s) --> for single word
puts(s); --> for multiple word
String library function header file
#include<string.h>

Difference between 1. gets() and scanf() 2. puts() and printf()

gets() scanf()

Only for string. For all data type(int ,float ,char)

For single or multi word For single word string


string

Input only one string at a time. Input one or more strings at a time

Unformatted function Formatted function

puts() printf()

Only for string. For all data type(int ,float ,char)

print only one string at a time. print one or more strings at a time
After print cursor goes to next line. After print cursor remains same line.

Unformatted function Formatted function

STRING LIBRARY FUNCTIONS


(1)strlen()
(2)strcpy()
(3)strcat()
(4)strcmp()
(5)strrev()

(1)strlen(s):- it counts number of character present in a string.


void main()
{
char s[10];
int l;
clrscr();
printf(“enter string”);
gets(s);
l=strlen(s);
printf(“string length=%d”,l);
getch();
}

(2)strcpy(s2,s1):- it copies the content of string s1 into string s2.


void main()
{
char s1[10],s2[10];
clrscr();
printf(“enter string”);
gets(s1);
strcpy(s2,s1);
printf(“\n string copied is \n”);
puts(s2);
getch();
}

(3)strcat(s2,s1):- it joins the content of string s1 into string s2.


void main()
{
char s1[10],s2[10];
clrscr();
printf(“enter string 1 \n”);
gets(s1);
printf(“\n enter string 2 \n”);
gets(s2);
strcat(s2,s1);
printf(“\n string after join \n”);
puts(s2);
getch();
}

(5)strcmp(s2,s1):- This function is used to compare two strings to find out whether they are same or different. The
strings are compared character by character until there is a mismatch or null is found. If the strings are same it will
return zero otherwise it will return the difference of ASCII value of characters.
void main()
{
char s1[10]=”pull”, s2[10]=”push”; int c;
clrscr();
c=strcmp(s2,s1);
if(c==0)
printf(“same strings”);
else
printf(“different strings”);
getch();
}

(6)strrev(s):- This function is used to reverse the string.

void main()
{
char s[10];
clrscr();
printf(“enter string”);
gets(s);
strrev(s);
printf(“string reverse is \n”);
puts(s);
getch();
}

Example- program to check whether the string is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
clrscr();
printf(“enter string \n”);
gets(s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s2,s1)==0)
{
printf(“\n string palindrome \n”);
}
else
printf(“\n not palindrome \n”);
getch();
}

You might also like