Creating an integer array with a for loop

Nov 28, 2018 at 7:06pm
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;
}
Nov 28, 2018 at 7:14pm
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.
Nov 28, 2018 at 7:42pm
I understand in theory, but how would I copy the value to a place in the array?
Nov 28, 2018 at 7:51pm
letters[count] = seperateCounter; count <= 122; count++;

This is what I did, but it's giving me an incorrect output.
Nov 28, 2018 at 8:31pm
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
}


Nov 28, 2018 at 9:21pm
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
   }
Nov 28, 2018 at 10:10pm
Thanks for your help. How do I increment the seperateCounter without exceeding 122?
Nov 28, 2018 at 10:24pm
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?
Nov 28, 2018 at 10:28pm
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 
Nov 29, 2018 at 8:33am
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.
Nov 29, 2018 at 10:13am
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.
Nov 29, 2018 at 12:09pm
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.