SlideShare a Scribd company logo
UNIT-3
ARRAYS AND STRINGS
ARRAYS
Array:
•Arrays are used to store multiple elements in a single variable, instead of declaring
separate variables for each value.
•These elements can be of int, float, char, or double data type or can be of user-defined
data types too
•The elements are stored from left to right with the left-most index being the 0th index
and the rightmost index being the (n-1) index.
Example :
To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].
Syntax :
datatype array_variable name[size];
Example :
int a[5];
Array Declaration:
Syntax :
datatype array_variable name[size];
Example :
float mark[5];
• we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can
hold 5 floating-point values.
•The size and type of an array cannot be changed once it is declared.
Array Initialization :
• It is possible to initialize an array during declaration
Example :
int mark[5] = {19, 10, 8, 17, 9};
We can also initialize an array like this:
int mark[] = {19, 10, 8, 17, 9};
•Here, we haven't specified the size. However, the compiler knows its size is 5 as we
are initializing it with 5 elements.
Access an Array Element:
• To access an array element, refer to its index number.
• Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Example(line of code) :
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
Output :
25
Change an Array Element:
• To change the value of a specific element, refer to the index number
Example(line of code) :
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
Printf("%d", myNumbers[0]);
Output :
33
Loop Through an Array:
• You can loop through the array elements with the for loop.
Example :
The following example displays all elements initialized in an array:
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++) {
printf("%dn", myNumbers[i]);
}
return 0;
}
Output :
Program to take 5 values from the user and store them in an array .Print
the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i) // taking input and storing it in an array
{
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) // printing elements of an array
{
printf("%dn", values[i]);
}
}
return 0;
}
Program to find the average of n numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0;
Float average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i < n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum / n;
printf("Average = %.2lf", average);
return 0;
}
TWO DIMENSIONAL ARRAY
A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It
can be visualized as an array of arrays. The below image ia the representation of two-
dimensional array.
Initialization of 2D-Array:
In the below, we initialize a 2D array ”arr”, with 4 rows and 2 columns as an array of
arrays.
Syntax:
data_type array_name[x][y];
Example 1:
int arr[4][2] = {
{1234, 56},
{1212, 33},
{1434, 80},
{1312, 78}
} ;
Example 2:
We can also initialize a 2D array in the following way:
int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};
Here,arr is a 2D array with 4 rows and 2 columns.
Example 3:
int x[3][4];
Here, x is a two-dimensional array. It can hold a maximum of 12 elements.
Example 4:
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
This array has 2 rows and 3 columns, which is why we have two rows of elements with
3 elements each.
Program to print the elements of 2D-array:
#include <stdio.h>
int main(void)
{
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; // an array with 3 rows and 2 columns.
for (int i = 0; i < 3; i++) { // output each array element's value
for (int j = 0; j < 2; j++) {
printf("Element at x[%i][%i]: ", i, j);
printf("%dn", x[i][j]);
}
}
return (0);
}
Matrix addition using 2D-Array
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
// Taking input using nested for loop
printf("Enter elements of 1st matrixn");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrixn");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%f", &b[i][j]);
}
// adding corresponding elements of two arrays
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
result[i][j] = a[i][j] + b[i][j];
}
// Displaying the sum
printf("nSum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("%.1ft", result[i][j]);
if (j == 1)
printf("n");
}
return 0;
}
arrays in c programming - example programs
List of Experiments:
1.Program to take 5 values from the user and store them in an array .Print the
elements stored in the array
2. Program to find the average of n numbers using arrays
3. Program to print the elements of 2D-array
4.Matrix addition using 2D-Array

More Related Content

PDF
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
PPT
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PDF
VIT351 Software Development VI Unit2
YOGESH SINGH
 
PPTX
Arrays
RaziyasultanaShaik
 
PDF
02 arrays
Rajan Gautam
 
PPTX
6_Array.pptx
shafat6712
 
DOCX
Array assignment
Ahmad Kamal
 
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
VIT351 Software Development VI Unit2
YOGESH SINGH
 
02 arrays
Rajan Gautam
 
6_Array.pptx
shafat6712
 
Array assignment
Ahmad Kamal
 

Similar to arrays in c programming - example programs (20)

PPT
Arrays in c programing. practicals and .ppt
Carlos701746
 
PPTX
Chapter 13.pptx
AnisZahirahAzman
 
PPTX
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PDF
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
 
PPTX
COM1407: Arrays
Hemantha Kulathilake
 
PPTX
Arrays
Neeru Mittal
 
PPTX
Arrays_in_c++.pptx
MrMaster11
 
PPTX
Array
Anil Dutt
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PDF
Arrays-Computer programming
nmahi96
 
PPT
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
PPTX
Arrays in C language
Shubham Sharma
 
PDF
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
PDF
ARRAYS
muniryaseen
 
PPT
Fp201 unit4
rohassanie
 
PDF
SPL 10 | One Dimensional Array in C
Mohammad Imam Hossain
 
PDF
Array&amp;string
chanchal ghosh
 
Arrays in c programing. practicals and .ppt
Carlos701746
 
Chapter 13.pptx
AnisZahirahAzman
 
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
 
COM1407: Arrays
Hemantha Kulathilake
 
Arrays
Neeru Mittal
 
Arrays_in_c++.pptx
MrMaster11
 
Array
Anil Dutt
 
Arrays & Strings
Munazza-Mah-Jabeen
 
Arrays-Computer programming
nmahi96
 
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
Arrays in C language
Shubham Sharma
 
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
ARRAYS
muniryaseen
 
Fp201 unit4
rohassanie
 
SPL 10 | One Dimensional Array in C
Mohammad Imam Hossain
 
Array&amp;string
chanchal ghosh
 
Ad

Recently uploaded (20)

PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Inventory management chapter in automation and robotics.
atisht0104
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Ad

arrays in c programming - example programs

  • 2. ARRAYS Array: •Arrays are used to store multiple elements in a single variable, instead of declaring separate variables for each value. •These elements can be of int, float, char, or double data type or can be of user-defined data types too •The elements are stored from left to right with the left-most index being the 0th index and the rightmost index being the (n-1) index. Example : To create an array, define the data type (like int) and specify the name of the array followed by square brackets []. Syntax : datatype array_variable name[size]; Example : int a[5];
  • 3. Array Declaration: Syntax : datatype array_variable name[size]; Example : float mark[5]; • we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values. •The size and type of an array cannot be changed once it is declared.
  • 4. Array Initialization : • It is possible to initialize an array during declaration Example : int mark[5] = {19, 10, 8, 17, 9}; We can also initialize an array like this: int mark[] = {19, 10, 8, 17, 9}; •Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.
  • 5. Access an Array Element: • To access an array element, refer to its index number. • Array indexes start with 0: [0] is the first element. [1] is the second element, etc. Example(line of code) : int myNumbers[] = {25, 50, 75, 100}; printf("%d", myNumbers[0]); Output : 25 Change an Array Element: • To change the value of a specific element, refer to the index number Example(line of code) : int myNumbers[] = {25, 50, 75, 100}; myNumbers[0] = 33; Printf("%d", myNumbers[0]); Output : 33
  • 6. Loop Through an Array: • You can loop through the array elements with the for loop. Example : The following example displays all elements initialized in an array: #include <stdio.h> int main() { int myNumbers[] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { printf("%dn", myNumbers[i]); } return 0; } Output :
  • 7. Program to take 5 values from the user and store them in an array .Print the elements stored in the array #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); for(int i = 0; i < 5; ++i) // taking input and storing it in an array { scanf("%d", &values[i]); } printf("Displaying integers: "); for(int i = 0; i < 5; ++i) // printing elements of an array { printf("%dn", values[i]); } } return 0; }
  • 8. Program to find the average of n numbers using arrays #include <stdio.h> int main() { int marks[10], i, n, sum = 0; Float average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i < n; ++i) { printf("Enter number%d: ",i+1); scanf("%d", &marks[i]); sum += marks[i]; } average = sum / n; printf("Average = %.2lf", average); return 0; }
  • 9. TWO DIMENSIONAL ARRAY A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It can be visualized as an array of arrays. The below image ia the representation of two- dimensional array.
  • 10. Initialization of 2D-Array: In the below, we initialize a 2D array ”arr”, with 4 rows and 2 columns as an array of arrays. Syntax: data_type array_name[x][y]; Example 1: int arr[4][2] = { {1234, 56}, {1212, 33}, {1434, 80}, {1312, 78} } ; Example 2: We can also initialize a 2D array in the following way: int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78}; Here,arr is a 2D array with 4 rows and 2 columns.
  • 11. Example 3: int x[3][4]; Here, x is a two-dimensional array. It can hold a maximum of 12 elements. Example 4: int test[2][3] = { {2, 4, 5}, {9, 0, 19}}; This array has 2 rows and 3 columns, which is why we have two rows of elements with 3 elements each.
  • 12. Program to print the elements of 2D-array: #include <stdio.h> int main(void) { int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; // an array with 3 rows and 2 columns. for (int i = 0; i < 3; i++) { // output each array element's value for (int j = 0; j < 2; j++) { printf("Element at x[%i][%i]: ", i, j); printf("%dn", x[i][j]); } } return (0); }
  • 13. Matrix addition using 2D-Array #include <stdio.h> int main() { float a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrixn"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter a%d%d: ", i + 1, j + 1); scanf("%f", &a[i][j]); }
  • 14. // Taking input using nested for loop printf("Enter elements of 2nd matrixn"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter b%d%d: ", i + 1, j + 1); scanf("%f", &b[i][j]); } // adding corresponding elements of two arrays for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { result[i][j] = a[i][j] + b[i][j]; }
  • 15. // Displaying the sum printf("nSum Of Matrix:"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("%.1ft", result[i][j]); if (j == 1) printf("n"); } return 0; }
  • 17. List of Experiments: 1.Program to take 5 values from the user and store them in an array .Print the elements stored in the array 2. Program to find the average of n numbers using arrays 3. Program to print the elements of 2D-array 4.Matrix addition using 2D-Array