Input a random positive integer, n.
Create an empty character array with a size of the inputted random positive integer, n.
In the next succeeding lines, store the n characters into the character array.
Input an integer, m, for an index. This value should be from 0 to n - 1.
Using your understanding on accessing array elements, access and print out the array element having the index position of the inputted random integer, m.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Please, enter a size of character array: ";
cin >> n;
char* arr = new char[n];
cout << "Please, enter characters: ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int m;
cout << "Please, enter an integer value between 0 to n-1: ";
cin >> m;
if (m < 0 || m > n - 1)
cout << "Incorrect value!";
else
cout << "Value arr[m] = " << arr[m];
}
Comments
Leave a comment