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

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

PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Chapter 13.pptx
AnisZahirahAzman
 
PPTX
Arrays in c
Jeeva Nanthini
 
PDF
Arrays
Steven Wallach
 
PDF
array-191103180006.pdf
HEMAHEMS5
 
PDF
C Language Lecture 10
Shahzaib Ajmal
 
PPT
Array 31.8.2020 updated
vrgokila
 
PPTX
Array
HarshKumar943076
 
PPTX
Array
Anil Dutt
 
PDF
Array&amp;string
chanchal ghosh
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PPT
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
PPTX
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
PPT
presentation_arrays_1443553113_140676.ppt
NamakkalPasanga
 
PPTX
Arrays basics
sudhirvegad
 
PPTX
Array
Deep Shah
 
PPTX
Array
PralhadKhanal1
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PDF
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Programming in c Arrays
janani thirupathi
 
Chapter 13.pptx
AnisZahirahAzman
 
Arrays in c
Jeeva Nanthini
 
array-191103180006.pdf
HEMAHEMS5
 
C Language Lecture 10
Shahzaib Ajmal
 
Array 31.8.2020 updated
vrgokila
 
Array
Anil Dutt
 
Array&amp;string
chanchal ghosh
 
Arrays & Strings
Munazza-Mah-Jabeen
 
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
presentation_arrays_1443553113_140676.ppt
NamakkalPasanga
 
Arrays basics
sudhirvegad
 
Array
Deep Shah
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
Introduction to Arrays in C
Thesis Scientist Private Limited
 

Recently uploaded (20)

PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PPTX
Work at Height training for workers .pptx
cecos12
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PDF
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
Work at Height training for workers .pptx
cecos12
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
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