Arrays in C / C++ with examples
In this tutorial, we will learn about the arrays in C / C++ with examples. These are common examples when using arrays in C / C++. Do not skip these examples if you want to use arrays proficiently.
Make sure you learn about arrays in C / C++. If not, please visit here to find out.
Top 10 arrays in C / C++ with example
Here, we will give 10 examples of arrays in C / C++. These examples are written in two different languages, C and C++.
Example 1: Import and export elements in an array in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 2: Displaying even numbers in array in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } cout<<"\nEven numbers in array: "; for(int i=0; i<n; i++){ if(arr[i] % 2 == 0) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } printf("\nEven numbers in array: "); for(int i=0; i<n; i++){ if(arr[i] % 2 == 0) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 3: Displaying odd numbers in array in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } cout<<"\nOdd numbers in array: "; for(int i=0; i<n; i++){ if(arr[i] % 2 == 1) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } printf("\nOdd numbers in array: "); for(int i=0; i<n; i++){ if(arr[i] % 2 == 1) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 4: Displaying primes numbers in array in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } cout<<"\nPrimes in array: "; for(int i=0; i<n; i++){ if(isPrimes(arr[i])) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } printf("\nPrimes in array: "); for(int i=0; i<n; i++){ if(isPrimes(arr[i])) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 5: Displaying perfect squares numbers in array in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPerfectSquare(int n){ int i = 0; while(i*i <= n){ if(i*i == n){ return true; } ++i; } return false; } int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } cout<<"\nPerfect squares number in array: "; for(int i=0; i<n; i++){ if(isPerfectSquare(arr[i])) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPerfectSquare(int n){ int i = 0; while(i*i <= n){ if(i*i == n){ return true; } ++i; } return false; } int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } printf("\nPerfect squares number in array: "); for(int i=0; i<n; i++){ if(isPerfectSquare(arr[i])) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 6: Find the sum of elements in array in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n, sum = 0; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } for(int i=0; i<n; i++){ sum += arr[i]; } cout<<"\nSum of elements in array: "<<sum; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int n, sum = 0; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } for(int i=0; i<n; i++){ sum += arr[i]; } printf("\nSum of elements in array: %d",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 7: Find the sum of even elements in array in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n, sum = 0; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } for(int i=0; i<n; i++){ if(arr[i] % 2 == 0) sum += arr[i]; } cout<<"\nSum of even elements in array: "<<sum; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int n, sum = 0; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } for(int i=0; i<n; i++){ if(arr[i] % 2 == 0) sum += arr[i]; } printf("\nSum of even elements in array: %d",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 8: Sort array ascending in C / C++
C++ programming:
#include <iostream> using namespace std; void sort_numbers_ascending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] > number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } cout<<"\nArray after sorting ascending: "; for (i = 0; i < count; ++i) cout<< number[i]<< " "; } int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } sort_numbers_ascending(arr, n); cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> void sort_numbers_ascending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] > number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } printf("\nArray after sorting ascending: "); for (i = 0; i < count; ++i) printf("%d ", number[i]); } int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } sort_numbers_ascending(arr, n); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 9: Sort array descending in C / C++
C++ programming:
#include <iostream> using namespace std; void sort_numbers_descending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] < number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } cout<<"\nArray after sorting descending: "; for (i = 0; i < count; ++i) cout<< number[i]<< " "; } int main() { int n; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } sort_numbers_descending(arr, n); cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> void sort_numbers_descending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] < number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } printf("\nArray after sorting descending: "); for (i = 0; i < count; ++i) printf("%d ", number[i]); } int main(void) { int n; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } sort_numbers_descending(arr, n); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 10: Searching for an element in array in C / C++
C++ programming:
#include <iostream> using namespace std; void show_array(int array[], int length){ for(short i = 0; i < length; i++) cout << array[i] <<' '; cout << endl; } void find_element_index(const int* array, size_t size, int x){ int result[100], count = 0; for (size_t i = 0; i < size - 1; ++i) { if (array[i] == x) { result[count] = i; ++count; } } cout << "Element "<< x <<" appear in position: "; show_array(result, count); } int main() { int n, x; do{ cout<<"Enter the size of the array: "; cin>>n; if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n"; }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ cout<<"Enter the element "<<"[" <<i<<"]: "; cin>>arr[i]; } cout<<"Elements in array is: "; for(int i = 0; i <= n - 1; i++){ cout<<arr[i]<<" "; } cout<<"\nEnter the search element: "; cin>>x; find_element_index(arr,n,x); cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> void show_array(int array[], int length){ for(short i = 0; i < length; i++) printf("%d ",array[i]); printf("\n"); } void find_element_index(const int* array, size_t size, int x){ int result[100], count = 0; for (size_t i = 0; i < size - 1; ++i) { if (array[i] == x) { result[count] = i; ++count; } } printf("Element %d appear in position: ",x); show_array(result, count); } int main(void) { int n, x; do{ printf("Enter the size of the array: "); scanf("%d", &n); if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n"); }while(n <= 0); int arr[n]; for(int i = 0; i <= n - 1; i++){ printf("Enter the element [%d]: ",i); scanf("%d", &arr[i]); } printf("Elements in array is: "); for(int i = 0; i <= n - 1; i++){ printf("%d ",arr[i]); } printf("\nEnter the search element: "); scanf("%d", &x);; find_element_index(arr,n,x); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output: