Write your own program that will create an integer array of 26 numbers. Use a const int to define the array size. Use a for loop to start at 0 and go through 25. Use a separate counter variable to assign the values 97-122 to the integer array. Use a second for loop to print out (using static_cast<char>) the characters for these values in a table.
I've got the basic skeleton of my program, but I've never used a for loop to set values? Is the syntax correct? Also, how would I use a loop to set the range from 97-122
#include <iostream>
usingnamespace std;
int main()
{
constint NUM_LETTERS = 26;
int letters[NUM_LETTERS]; //Here I would use a for loop to set values instead of {}
for (int count = 0; count <= 25; count++) //<<--------
{
//this loop would set letters[count] with the value range 97 - 122
letters[count] = ;
}
cout << "ASCII Code\tCharacter\n";
cout << "----------\t---------\n";
for (int count = 0; count < NUM_LETTERS; count++)
{
cout << letters[count] << "\t\t";
cout << static_cast<char>(letters[count]) << endl;
}
system("pause");
return 0;
}
You seem to be mixing up what controls your loop with what happens inside your loop.
1 2 3 4 5 6 7
int seperateCounter = 97;
for (int count = 0; count <= 25; count++)
{
// Set array[count] to the value of seperateCounter
// increment seperateCounter
}
I guess what I'm asking is, how do I write the incrementation?
1 2 3 4 5 6 7 8
constint seperateCounter = 97;
constint NUM_LETTERS = 26;
int letters[NUM_LETTERS]; //Here I would use a for loop to set values instead of {}
for (int count = 0; count <= 25; count++) //<<--------
{
letters[count] = 97;
//incrementation goes here
constint NUM_LETTERS = 26;
int letters[NUM_LETTERS]; //Here I would use a for loop to set values instead of {}
for (int count = 0; count <= 25; count++) //<<--------
{
//this loop would set letters[count] with the value range 97 - 122
letters[count] = ;
}
Having correctly created your NUM_LETTERS constant, it's a shame not to use it everywhere it would be useful. The usual idiom would be: