SlideShare a Scribd company logo
2
Most read
12
Most read
13
Most read
PPS
Unit – 4
Arrays
4.1 Array Declaration & Initialization
Array is defined as a collection of elements of same data type. Hence it is also called as
homogeneous variable. Total number of elements in array defines size of array. Position
of element in an array defines index or subscript of particular element. Array index
always starts with zero.
4.2 Bound Checking Arrays (1-D, 2-D)
1. One Dimensional Array:
The array which is used to represent and store data in a linear form is called as 'single or
one dimensional array.'
Syntax:
data-typearray_name[size];
Example 2:
int a[3] = {2, 3, 5};
charch[10] = "Data" ;
float x[3] = {2.5, 3.50,489.98} ;
Total Size of an array(in Bytes) can be calculated as follows:
total size(in bytes) = length of array * size of data type
In above example, a is an array of type integer which has storage size of 3 elements.
One integer variable occupies 2 bytes. Hence the total size would be 3 * 2 = 6 bytes.
Memory Allocation :
P1: Program to display all elements of 1-D array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];
intn,i;
printf("n Enter the size of array :");
scanf("%d",&n);
printf("size of array is %d",n);
printf("n Enter Values to store in array one by one by pressing ENTER :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("n The Values in array are..");
for(i=0;i<n;i++)
{
printf("n %d",a[i]);
}
getch();
}
PPS 4.4ARRAYS  ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D, 2-D), CHARACTER ARRAYS AND STRINGS.
P2: Program to copy 1-D array to another 1-D array
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[100], b[100];// a is original array and b is copied array
printf("Enter the number of elements in array an");
scanf("%d", &n);
printf("Enter the array elements in an");
for (i= 0; i< n ; i++)
scanf("%d", &a[i]);
/*
Copying elements from array a into array b */
for (i =0;i<n;i++)
b[i] = a[i];
/*
Printing copied array b
.
*/
printf("Elements of array b aren");
for (i = 0; i< n; i++)
printf("%dn", b[i]);
getch();
}
Output
P3:Program to find reverse of 1-D array
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[100], b[100];// a is original array and b is reversed array
printf("Enter the number of elements in arrayn");
scanf("%d", &n);
printf("Enter the array elementsn");
for (i= 0; i< n ; i++)
scanf("%d", &a[i]);
/*
* Copying elements into array b starting from end of array a
*/
for (i = n - 1, j = 0; i>= 0; i--, j++)
b[j] = a[i];
/*
Printing reversed array b
.
*/
printf("Reverse array isn");
for (i = 0; i< n; i++)
printf("%dn", a[i]);
getch();
}
Output:
PPS 4.4ARRAYS  ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D, 2-D), CHARACTER ARRAYS AND STRINGS.
2. Multi Dimensional Array
1-D array has 1 row and multiple columns but multidimensional array has of multiple
rows and multiple columns.
Two Dimensional array:
Itis the simplest multidimensional array. They are also called as matrix.
Declaration:
data-typearray_name[no.of rows][no. of columns];
Total no of elements= No.of rows * No. of Columns
Example 3: inta[2][3]
This example illustrates two dimensional array a of 2 rows and 3 columns.
Total no of elements: 2*3=6
Total no of bytes occupied: 6( totalno.of elements )*2(Size of one element)=12
Initialization and Storage Representation:
In C, two dimensional arrays can be initialized in different number of ways. Specification
of no. of columns in 2 dimensional array is mandatory while no of rows is optional.
int a[2][3]={{1,3,0}, // Elements of 1st
row
{-1,5,9}}; // Elements of 2nd
row
OR
int a[][3]={{1,3,0}, // Elements of 1st
row
{-1,5,9}}; // Elements of 2nd
row
OR
int a[2][3]={1,3,0,-1,5,9};
Fig. 3 gives general storage representation of 2 dimensional array
Eg 4. int a[2][3]={1,3,0,-1,5,9}
Accessing Two-Dimensional Array Elements:
An element in 2-dimensional array is accessed by using the subscripts i.e. row index
and column index of the array. For example:
intval= A[1][2];
The above statement will access element from the 2nd
row and third column of the array
A i.e. element 9.
4.3 Character Arrays And Strings
If the size of array is n then index ranges from 0 to n-1. There can be array of integer,
floating point or character values. Character array is called as String.
Eg.1 A=
0 1 2 3 4
This example illustrates integer array A of size 5.
 Array index ranges from 0 to 4.
 Elements of array are {10, 20, 40, 5, 25}.
Arrays can be categorized in following types
1. One dimensional array
2. Multi Dimensional array
These are described below.
The abstract model of string is implemented by defining a character array data type and
we need to include header file ‘string.h’, to hold all the relevant operations of string. The
string header file enables user to view complete string and perform various operation
on the string. E.g. we can call ‘strlen’ function to find length of string, ‘strcat’ to
concatenate two string, ‘strcpy’ to copy one string to another, ‘strrev’ to reverse the
string. The definitions of all these functions are stored in the header file ‘string.h’. So
string functions with the user provided data becomes the new data type in ‘C’.
Fig. 3.1 : String data type
To use the functions related to strings, user only need to include ‘string.h’ header file.
All the definitions details of these functions are keep hidden from the user, user can
directly use these function without knowing all the implementation details. This is known
as abstraction or hiding.
String Manipulation in C Language
Strings are also called as the array of character.
 A special character usually known as null character (0) is used to indicate the end
of the string.
 To read and write the string in C program %s access specifier is required.
 C language provides several built in functions for the manipulation of the string
 To use the built in functions for string, user need to include string.h header file.
 Syntax for declaring string is
Char string name[size of string];
Char is the data type, user can give any name to the string by following all the rules of
defining name of variable. Size of the string is the number of alphabet user wants to
store in string.
Example:
Sr. No. Instructions Description
1. #include<stdio.h> Header file included
2. #include<conio.h
>
Header file included
3. void main() Execution of program begins
4. { String is declare with name str and size of
storing 10 alphbates
5. char str[10];
6. clrscr(); Clear the output of previous screen
7. printf("enter a
string");
Print “enter a string”
8. scanf("%s",&str); Entered string is stored at address of str
9. printf("n user
entered string is
%s",str);
Print: user entered string is (string entered
by user)
10. getch(); Used to hold the output screen
11. } Indicates end of scope of main function
Following are the list of string manipulation function
Sr. No.
String
Function
Purpose
1. strcat use to concatenate (append) one string to
another
2. Strcmp use to compare one string with another.
(Note: The comparison is case sensitive)
3. strchr use to locate the first occurrence of a particular
character in a given string
4. Strcpy use to copy one string to another.
5. Strlen use to find the length of a string in bytes,

More Related Content

Similar to PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D, 2-D), CHARACTER ARRAYS AND STRINGS. (20)

PPTX
Module 4- Arrays and Strings
nikshaikh786
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
DOCX
Unitii classnotes
Sowri Rajan
 
PPTX
Lesson 7-computer programming case study-FINAL.pptx
claritoBaluyot2
 
PDF
Unit 2
TPLatchoumi
 
PPTX
ARRAY's in C Programming Language PPTX.
MSridhar18
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PPTX
Array, string and pointer
Nishant Munjal
 
PDF
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
PPS
C programming session 04
Dushmanta Nath
 
PPTX
Arrays & Strings.pptx
AnkurRajSingh2
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
duttasoumyajit5
 
PDF
Array&amp;string
chanchal ghosh
 
PPTX
Array and string
prashant chelani
 
DOCX
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
PPTX
3.ArraysandPointers.pptx
FolkAdonis
 
PPTX
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
Module 4- Arrays and Strings
nikshaikh786
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Unitii classnotes
Sowri Rajan
 
Lesson 7-computer programming case study-FINAL.pptx
claritoBaluyot2
 
Unit 2
TPLatchoumi
 
ARRAY's in C Programming Language PPTX.
MSridhar18
 
Arrays & Strings
Munazza-Mah-Jabeen
 
Array, string and pointer
Nishant Munjal
 
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
C programming session 04
Dushmanta Nath
 
Arrays & Strings.pptx
AnkurRajSingh2
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Array THE DATA STRUCTURE. ITS THE STRUCT
duttasoumyajit5
 
Array&amp;string
chanchal ghosh
 
Array and string
prashant chelani
 
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
3.ArraysandPointers.pptx
FolkAdonis
 
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 

More from Sitamarhi Institute of Technology (20)

PDF
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
PDF
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
DOCX
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
PDF
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
PDF
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
PDF
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
PDF
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
PDF
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Sitamarhi Institute of Technology
 
PDF
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
PDF
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
PDF
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
PDF
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
PDF
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
PPTX
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
PDF
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
PDF
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
PDF
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 
PDF
MS Excel Notes PDF in Hindi माइोसॉट एसेल
Sitamarhi Institute of Technology
 
PDF
beltron-programmer-2023-previous-year-question.pdf
Sitamarhi Institute of Technology
 
PDF
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Sitamarhi Institute of Technology
 
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Sitamarhi Institute of Technology
 
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 
MS Excel Notes PDF in Hindi माइोसॉट एसेल
Sitamarhi Institute of Technology
 
beltron-programmer-2023-previous-year-question.pdf
Sitamarhi Institute of Technology
 
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Sitamarhi Institute of Technology
 
Ad

Recently uploaded (20)

PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Design Thinking basics for Engineers.pdf
CMR University
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Ad

PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D, 2-D), CHARACTER ARRAYS AND STRINGS.

  • 1. PPS Unit – 4 Arrays 4.1 Array Declaration & Initialization Array is defined as a collection of elements of same data type. Hence it is also called as homogeneous variable. Total number of elements in array defines size of array. Position of element in an array defines index or subscript of particular element. Array index always starts with zero. 4.2 Bound Checking Arrays (1-D, 2-D) 1. One Dimensional Array: The array which is used to represent and store data in a linear form is called as 'single or one dimensional array.' Syntax: data-typearray_name[size]; Example 2: int a[3] = {2, 3, 5}; charch[10] = "Data" ; float x[3] = {2.5, 3.50,489.98} ; Total Size of an array(in Bytes) can be calculated as follows: total size(in bytes) = length of array * size of data type
  • 2. In above example, a is an array of type integer which has storage size of 3 elements. One integer variable occupies 2 bytes. Hence the total size would be 3 * 2 = 6 bytes. Memory Allocation : P1: Program to display all elements of 1-D array. #include<stdio.h> #include<conio.h> void main() { int a[20]; intn,i; printf("n Enter the size of array :"); scanf("%d",&n); printf("size of array is %d",n); printf("n Enter Values to store in array one by one by pressing ENTER :"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("n The Values in array are.."); for(i=0;i<n;i++) { printf("n %d",a[i]); } getch(); }
  • 4. P2: Program to copy 1-D array to another 1-D array #include <stdio.h> #include<conio.h> void main() { int n, i, j, a[100], b[100];// a is original array and b is copied array printf("Enter the number of elements in array an"); scanf("%d", &n); printf("Enter the array elements in an"); for (i= 0; i< n ; i++) scanf("%d", &a[i]); /* Copying elements from array a into array b */ for (i =0;i<n;i++) b[i] = a[i];
  • 5. /* Printing copied array b . */ printf("Elements of array b aren"); for (i = 0; i< n; i++) printf("%dn", b[i]); getch(); } Output
  • 6. P3:Program to find reverse of 1-D array #include <stdio.h> #include<conio.h> void main() { int n, i, j, a[100], b[100];// a is original array and b is reversed array printf("Enter the number of elements in arrayn");
  • 7. scanf("%d", &n); printf("Enter the array elementsn"); for (i= 0; i< n ; i++) scanf("%d", &a[i]); /* * Copying elements into array b starting from end of array a */ for (i = n - 1, j = 0; i>= 0; i--, j++) b[j] = a[i]; /* Printing reversed array b . */ printf("Reverse array isn"); for (i = 0; i< n; i++) printf("%dn", a[i]); getch(); } Output:
  • 9. 2. Multi Dimensional Array 1-D array has 1 row and multiple columns but multidimensional array has of multiple rows and multiple columns. Two Dimensional array: Itis the simplest multidimensional array. They are also called as matrix.
  • 10. Declaration: data-typearray_name[no.of rows][no. of columns]; Total no of elements= No.of rows * No. of Columns Example 3: inta[2][3] This example illustrates two dimensional array a of 2 rows and 3 columns. Total no of elements: 2*3=6 Total no of bytes occupied: 6( totalno.of elements )*2(Size of one element)=12 Initialization and Storage Representation: In C, two dimensional arrays can be initialized in different number of ways. Specification of no. of columns in 2 dimensional array is mandatory while no of rows is optional. int a[2][3]={{1,3,0}, // Elements of 1st row {-1,5,9}}; // Elements of 2nd row OR int a[][3]={{1,3,0}, // Elements of 1st row {-1,5,9}}; // Elements of 2nd row
  • 11. OR int a[2][3]={1,3,0,-1,5,9}; Fig. 3 gives general storage representation of 2 dimensional array Eg 4. int a[2][3]={1,3,0,-1,5,9}
  • 12. Accessing Two-Dimensional Array Elements: An element in 2-dimensional array is accessed by using the subscripts i.e. row index and column index of the array. For example: intval= A[1][2]; The above statement will access element from the 2nd row and third column of the array A i.e. element 9. 4.3 Character Arrays And Strings If the size of array is n then index ranges from 0 to n-1. There can be array of integer, floating point or character values. Character array is called as String. Eg.1 A= 0 1 2 3 4 This example illustrates integer array A of size 5.  Array index ranges from 0 to 4.  Elements of array are {10, 20, 40, 5, 25}.
  • 13. Arrays can be categorized in following types 1. One dimensional array 2. Multi Dimensional array These are described below. The abstract model of string is implemented by defining a character array data type and we need to include header file ‘string.h’, to hold all the relevant operations of string. The string header file enables user to view complete string and perform various operation on the string. E.g. we can call ‘strlen’ function to find length of string, ‘strcat’ to concatenate two string, ‘strcpy’ to copy one string to another, ‘strrev’ to reverse the string. The definitions of all these functions are stored in the header file ‘string.h’. So string functions with the user provided data becomes the new data type in ‘C’. Fig. 3.1 : String data type To use the functions related to strings, user only need to include ‘string.h’ header file. All the definitions details of these functions are keep hidden from the user, user can directly use these function without knowing all the implementation details. This is known as abstraction or hiding. String Manipulation in C Language Strings are also called as the array of character.
  • 14.  A special character usually known as null character (0) is used to indicate the end of the string.  To read and write the string in C program %s access specifier is required.  C language provides several built in functions for the manipulation of the string  To use the built in functions for string, user need to include string.h header file.  Syntax for declaring string is Char string name[size of string]; Char is the data type, user can give any name to the string by following all the rules of defining name of variable. Size of the string is the number of alphabet user wants to store in string. Example: Sr. No. Instructions Description 1. #include<stdio.h> Header file included 2. #include<conio.h > Header file included 3. void main() Execution of program begins 4. { String is declare with name str and size of storing 10 alphbates 5. char str[10]; 6. clrscr(); Clear the output of previous screen 7. printf("enter a string"); Print “enter a string” 8. scanf("%s",&str); Entered string is stored at address of str 9. printf("n user entered string is %s",str); Print: user entered string is (string entered by user) 10. getch(); Used to hold the output screen 11. } Indicates end of scope of main function Following are the list of string manipulation function Sr. No. String Function Purpose 1. strcat use to concatenate (append) one string to another
  • 15. 2. Strcmp use to compare one string with another. (Note: The comparison is case sensitive) 3. strchr use to locate the first occurrence of a particular character in a given string 4. Strcpy use to copy one string to another. 5. Strlen use to find the length of a string in bytes,