I hope that you tried this on your own first. In any case, I am happy to help.
Note the nuance on the array bounds for going forward and backward. The forward case indices go from 0 to 15. The reverse case goes from 15 to 0.
In fillArray(), the code leverages the fact that chars are internally stored as integers, and the letters are represented in order, so ‘A’+1 is ‘B’, etc.
#include <iostream>
using namespace std;
const int NUM_ELEMENTS = 16;
//Declare a 16 element character array with global scope.
char yourArray[NUM_ELEMENTS];
//Fill it with letters A to P in fillArray(),
void fillArray()
{
char iterChar="A";
for (int i = 0; i < NUM_ELEMENTS; i++)
{
yourArray[i] = iterChar;
iterChar++;
}
}
//output the array forward in forwardArray(),
void forwardArray()
{
cout << "Forward Array [";
for (int i = 0; i < NUM_ELEMENTS; i++)
{
cout << yourArray[i];
}
cout <<"]" << endl;
}
//and then output the array backwards in backArray()."
void backwardArray()
{
cout << "Backward Array [";
for (int i = NUM_ELEMENTS-1; i >= 0; i--)
{
cout << yourArray[i];
}
cout << "]" << endl;
}
int main()
{
fillArray();
forwardArray();
backwardArray();
return 0;
}
4
solved How to declare a 16-element character array? [closed]