Table of Contents (top down ↓)
Program 1: Fill and display the elements of an array
The following program asks the user to enter the elements one by one and then displays them back to the console.
#include <iostream> using namespace std; int main() { // array of 5 elements const int SIZE = 5; // declare an int array int arr [SIZE]; // fill the array for (int i = 0; i < SIZE; i++) { printf("Enter arr[%d]: ", i); cin >> arr[i]; } // display the elements for (int i = 0; i < SIZE; i++) { printf("arr[%d] = %d", i, arr [i]); printf("\r\n======\r\n"); } return 0; }
Output of the program is as follows:
Enter arr[0]: 4 Enter arr[1]: -1 Enter arr[2]: 6 Enter arr[3]: 8 Enter arr[4]: 2 arr[0] = 4 ====== arr[1] = -1 ====== arr[2] = 6 ====== arr[3] = 8 ====== arr[4] = 2 ======
Program 2: Sum of the elements of an array
The following program creates a variable called sum. Then it adds the elements into sum, and displays it back to the console.
#include <iostream> using namespace std; int main() { // array of 5 elements const int SIZE = 5; // declare an int array int arr[SIZE]; // fill the array for (int i = 0; i < SIZE; i++) { printf("Enter arr[%d]: ", i); cin >> arr[i]; } // variable to store the sum int sum = 0; for (int i = 0; i < SIZE; i++) { // aggregate sum += arr[i]; } printf("sum = %d", sum); return 0; }
Output of the program is as follows:
Enter arr[0]: 9 Enter arr[1]: -1 Enter arr[2]: -9 Enter arr[3]: 2 Enter arr[4]: -2 sum = -1
Program 3: Search an element in an array
The following program compares the item to be searched with each element of the array. It prints the indices if the comparison succeeds, otherwise prints a regret message.
#include <iostream> using namespace std; int main() { // array of 5 elements const int SIZE = 5; // create an int array int arr[SIZE] = { 8, 9, 7, -1, 0 }; // element to search int search; printf("Enter the element to search: "); cin >> search; // flag to tell us if // found atleast once bool found = false; for (int i = 0; i < SIZE; i++) { // compare with each element if (search == arr[i]) { printf("Found! at index %d", i); found = true; } } // regret, if not found if (!found) { printf("Element not found."); } return 0; }
Program 4: Find the largest element in an array
The following program starts by assuming the first element as the largest. Then it examines each element and updates the largest. Finally, it prints the largest.
#include <iostream> using namespace std; int main() { // array of 5 elements const int SIZE = 5; // create an int array int arr[SIZE] = { 8, 9, 7, -1, 0 }; // let first be the largest int largest = arr[0]; for (int i = 0; i < SIZE; i++) { // compare with each element if (arr[i] > largest) { // and update the largest largest = arr[i]; } } printf("Largest = %d.", largest); return 0; }
Program 5: Reverse an array
The following program reverses the array by swapping the first half with the second half.
#include <iostream> using namespace std; int main() { // array of 5 elements const int SIZE = 5; // create an int array // pre-filled to keep the code // easier to understand int arr[SIZE] = { 8, 9, 7, -1, 0 }; // swap the first part of the array // with the latter part for (int i = 0; i < SIZE/2; i++) { int temp = arr[i]; arr[i] = arr[SIZE - i - 1]; arr[SIZE - i - 1] = temp; } // print for (int i = 0; i < SIZE; i++) { printf("arr[%d] = %d", i, arr[i]); printf("\n"); } return 0; }
Program 6:
Delete an element of an array and shift the remaining array
The following program obtains the index from the user and shifts the array backwards. Then it fills 0 to the last vacancy.
#include <iostream> using namespace std; int main() { // array of 5 elements const int SIZE = 5; // create or fill an array int arr[SIZE] = { 8, 9, 7, -1, 4 }; cout << "The array is: \n\n"; // print the current array for (int i = 0; i < SIZE; i++) { printf("arr[%d] = %d", i, arr[i]); printf("\n"); } printf("\n"); // at which index to delete int del; printf("Index to delete?[0-4]: "); cin >> del; cout << "\n\n"; // shift elements starting at // the required index for (int i = del; i < SIZE - 1; i++) { arr[i] = arr[i + 1]; } // set last vacancy at, say, 0 arr[SIZE - 1] = 0; cout << "The array after removal "; cout << " at index " << del << " is: "; cout << "\n\n"; // print for (int i = 0; i < SIZE; i++) { printf("arr[%d] = %d", i, arr[i]); printf("\n"); } return 0; }
Output of the program is as follows:
The array is: arr[0] = 8 arr[1] = 9 arr[2] = 7 arr[3] = -1 arr[4] = 4 Index to delete?[0-4]: 2 The array after removal at index 2 is: arr[0] = 8 arr[1] = 9 arr[2] = -1 arr[3] = 4 arr[4] = 0
Program 7:
Insert an element into an array and shift the remaining array
The following program obtains the index from the user and shifts the array forward.
#include <iostream> using namespace std; int main() { // large array of, say, 20 elements const int SIZE = 20; // declare the arrat int arr[SIZE]; // the usable size of the array int n; printf("Size of array?[< 20]: "); cin >> n; // fill the array for (int i = 0; i < n; i++) { printf("Enter arr[%d]: ", i); cin >> arr[i]; } // at what index to insert printf("Insert at which index?: "); int ins; cin >> ins; printf("Which element?: "); int what; cin >> what; // shift the array forward // starting at the required index for (int i = n; i > ins; i--) { arr[i] = arr[i - 1]; } // insert the number arr[ins] = what; // print the elements printf("\n\nArray now is: \n\n"); for (int i = 0; i < n + 1; i++) { printf("arr[%d] = %d", i, arr[i]); printf("\n"); } return 0; }
Output of the program is as follows:
Size of array?[< 20]: 5 Enter arr[0]: 6 Enter arr[1]: 2 Enter arr[2]: 9 Enter arr[3]: 1 Enter arr[4]: 5 Insert at which index?: 2 Which element?: -4 Array now is: arr[0] = 6 arr[1] = 2 arr[2] = -4 arr[3] = 9 arr[4] = 1 arr[5] = 5
This Blog Post/Article "List of C,C++ Programs on one Dimensional Arrays for Beginners" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.