Created
November 18, 2020 11:14
-
-
Save PrajaktaSathe/ec01ed366e6880c981729708a22b0ba6 to your computer and use it in GitHub Desktop.
Program to demonstrate initializing of arrays, take user input, and display elements of the array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program to demonstrate initializing of arrays, take user input, and display elements of the array | |
#include <iostream> | |
using namespace std; | |
int main() { | |
char a[] = {'a', 'b', 'c', 'd', 'e'}; // initialize hard-coded char array (stores only characters) | |
int arr[5]; // initialize integer array of size 5 | |
cout << "Enter array elements: " << endl; // displays message to enter array elements | |
// for loop, where iteration variable i is initialized to 0 | |
// i goes on upto (not including) 5 (i.e. size of the array) | |
// i is incremented by 1 in every iteration | |
for (int i = 0; i < 5; i++) { | |
cout << "Enter number: "; | |
cin >> arr[i]; // cin statement to input element and store in array | |
} | |
cout << "Displaying integer array elements: " << endl; | |
for (int i = 0; i < 5; i++) { | |
cout << arr[i] << " "; // cout statement to display elements of the integer array | |
} | |
cout << "\nDisplaying character array elements: " << endl; | |
for (int i = 0; i < 5; i++) { | |
cout << a[i] << " "; // cout statement to display elements of character array | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment