Creating an integer array with a for loop

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>
using namespace std;

int main()
{
	const int 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;
}
Create a variable named separateCounter

Set its value to 97

Inside the loop, copy the value of separateCounter to a place in the array.

Increment separateCounter

Keep looping.
I understand in theory, but how would I copy the value to a place in the array?
letters[count] = seperateCounter; count <= 122; count++;

This is what I did, but it's giving me an incorrect output.
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
}


If you want both counters initialized and incremented by the loop header:
1
2
3
4
   for( int count = 0, separateCounter = 97; count <= 25; ++count, ++separateCounter)
   {
       // Set array[count] to the value of separateCounter
   }
Thanks for your help. How do I increment the seperateCounter without exceeding 122?
Thanks for your help. How do I increment the seperateCounter without exceeding 122?


What value does it start at? 97
How many times will you increment it? 25
So what value will it be when you stop incrementing it? 97 + 25 = 122

So when will it exceed 122?
I guess what I'm asking is, how do I write the incrementation?

1
2
3
4
5
6
7
8
const int seperateCounter = 97;
	const int 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 
Why did you make seperateCounter const ?

If you follow the instructions
Use a separate counter variable to assign the values 97-122 to the integer array
you could assign it to the array and then increment it.
how do I write the incrementation?
seperateCounter = seperateCounter + 1;

I think you need to go back a few lessons. Adding one to a number is not meant to be a puzzler.
Also, regarding your loop:


9
10
11
12
13
14
15
16
	const int 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:

12
13
	for (int count = 0; count < NUM_LETTERS; count++) //<<--------
	{  
Topic archived. No new replies allowed.