26x26 Grid

Pages: 12
So, I have no idea where to start with this project.
Create and "display" a 26x26 grid with numbering on all sides from 1-26 with each spot containing a letter from the alphabet.

Ex.
|1||2||3||4||5|...|26|
_[A][B][C][D][E]...[Z]
_[A][B][C][D][E]...[Z]
_[A][B][C][D][E]...[Z]
_[A][B][C][D][E]...[Z]
_[A][B][C][D][E]...[Z]

As I said, I have no idea where to begin.
Do you need this stored or only drawn?

What is stopping you. Have you never seen a 2d array? Don't you know how to write a for loop that prints numbers? Don't you realize that characters are numbers too?
The Multi-directional array and somehow assigning letters to the boxes without having to identify every letter multiple times.
"Don't you realize that characters are numbers too?"
Where did you get that from?
@TruYadoo

Maybe this will help get you started.

1
2
3
4
5
6
7
// Make two loops of 26
x=0;
		for(char letter = 'A'; letter <= 'Z'; letter++)
		{
			Letters[y][x] = letter; // Letters[y][0 to 25] contains one letter of the alphabet
			x++;
		}
Last edited on
Is there a way that I can display the grid? I can understand things a LOT better if I can get a direct result from it instead of just "believing" that it's there and it worked.
You again use two loops, just instead of assignment, write cout << letters[y][x]; and at the end of the outer loop write cout << '\n';.

By the way, It's better not to have two variables that hold similar values. whitenite1 could have written
for(int x = 0; x < 26; x++) letters[y][x] = 'A'+x;
If by display you mean write it to the console, you can do like this. I am assuming you don't know anything of C++ so I'm explaining everything bit by bit.

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> //You must include this library to output or input things to/from the console

using std::cout; //This is used so when you want to use the function std::cout you may write only cout. 
                          //cout writes things to the console.


using std::endl; //This ends the line in the console

int main()
{
int a;  //declared variable "a"
int b; //same thing for "b"

a = 12;  // Assigned random value to "a" in this case 12
b = 76; // Same thing for "b" and 76

cout << "This is an example program for outputting text and variable to the console" << endl; 

cout << a << endl; //Here we use cout to write the value of the variable to the console
cout << b << endl; //Wrote b in the next line;

cout << a << b << endl; // wrote a followed by b


return 0; 
}


Compiling and running this example should get you an idea of how outputting to the console works.

Note: to output arrays you output them exactly like variable
 
cout << array[i] << endl; //Where i is the part of the array you want to display 


To output the 2 by 26 Grid whitenite showed, you may want to start with while loops to display and ifs to check if it's the end of the line.
I'm actually using the CodeBlocks IDE so I don't need to add "using std::cout;" or "using std::endl;". It automatically recognizes "cout" and "endl" by themselves.

I did only start learning C++ on 2/10 and am already working on another program which is already extremely useful during my normal operations.

I've been using the tutorial on C++.com which has helped me quite a bit already but I've just got stuck on arrays. It doesn't seem as straight forward as everything before it.

First, I'm just trying to create a 26x26 grid and have it displayed when I compile the code. So I can actually see that this, does that. Once I create the grid, fill in each row with the alphabet in a specific order.
I know what I want to do but creating the grid with an array and then displaying the entire 676 (26x26) block area grid onto the console.

Edit: So, I have it where it will display a single dimensional array. A->Z.
1
2
char grid[26] = 
{ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };

But it outputs as "ABCDEF..", how can I brackets [] in between each letter?

[A][B][C][D]

From there, making it multi-dimensional and applying a different shift pattern on each row.

[A][B][C][D][E]...[Z]
[B][C][D][E][F]...[A]
Last edited on
@TruYadoo
To print out the array one letter inside a pair of brackets, try ...
1
2
for(x=0;x<26;x++)
  cout << "[" << grid[x] << "]";
Alright thanks whitenite. Next step: Learn how to shift each separate line to the left by a different specific amount.
Left Shift arrangement: s = current shift & L = line # & S = previous line's shift
Line 1: s=0
Other 25 lines: s=S+(L-1)
1 2 3 4 5
[A][B][C][D][E][F][G] s = 0 (original line)
[B][C][D][E][F][G][A] s = 1
[D][E][F][G][A][B][C] s = 3 (from original line)
Would it be easier if I assigned each letter to a value? A=1, B=2
and then use:
1
2
3
4
5
    for(int i = 0; i < length - 1; i--)
    {
      grid[i] = grid[i - 1];
      }
      grid[length] = temp;


?
Actually, no, it wouldn't. The shifting would be the same. Make a string variable, let's call temp. Ask which row to shift. int row = choice. Then how many to shift. int amount = choice 2.
1
2
3
4
5
6
7
8
9
for(x=0;x<choice2;x++)
  temp =  grid[row-1][0]; // row-1 since array starts at zero. 
{
   for(y=0;y<25;y++) \\ 1 less than the total array
       {
          grid[row-1][y] = grid[row-1][y+1]; 
       }
grid[row-1][25] = temp;
}


Then just print out the grids again, and you should see that row shifted by the amount specified.
What should I declare y as?
And error: invalid types 'char[int]' for array subscript
on line 24 being grid[row-1][25] = temp;

by How many to shift, do you mean how many lines to shift or the "equation" for how many letters to shift?
Last edited on
@TruYadoo

Declare y as int. Temp as string. Row, and amount, as int. So, you would have..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int row, amount;
string temp;
cout << "What row do you want shifted? (1 - 26) ";
cin >> row;
// Then do a check to verify row IS between 1 and 26 or a crash occurs when trying to move non-existent array  
cout << "How many times to be shifted? ";
cin >> amount;
for(x=0;x<amount;x++)
  temp =  grid[row-1][0]; // row-1 since array starts at zero. Get first value
{
   for(y=0;y<25;y++) // 1 less than the total array, so it's 1 to 24
       {
          grid[row-1][y] = grid[row-1][y+1]; // shifts total row by one on each pass of amount
       }
grid[row-1][25] = temp; // The value of the grid[row-1][0] is placed in grid[row-1][25] 
}


I understood Learn how to shift each separate line to the left by a different specific amount. to mean the user is asked how many times to shift a single line.
Last edited on
I don't want it to be user inputted. A simple 26x26 table where each line contains the entire alphabet in order but shifted in different ways.

Line 1: shift = 0
line 2: shift = 1 b/c (2-1)+0=1
line 3: shift = 3 b/c (3-1)+1=3
I want the line shifts to follow an equation. current shift = (Line# - 1) + previous line's shift
ill need to assign each line with a value for this however and I guess temp save the previous line's shift to work for the next line.

if amount is the number of shifts;
amount/shift = (row#-1) + oldshift
Ex Line 9 had a left shift of 10. Line 10's shift = (10[line # = 10] - 1) + 10 [line 9's shift]
amount = (10-1)+10 = 9+10 = 19 which is line 10's shift. I'll try to make a mock up using the terms that I know understand
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char grid[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
// Creates the first (default) line. Line 1. 
char x;
int amount; // Current line's shift.
int row; // For when all 26 rows/lines are assigned a line #
int xamount; // Previous line's shift. Will be saved as a temp value, I guess?
char length; // I have this declared as well but I'm confused on what purpose it holds.

int main ()
{
  do {   
      amount = row + xamount; // Doesn't the first line count as 0 anyways? If so, there would be no need
                                                // to subtract 1 as the line # as already one less, right? 
                                                // The ninth line would be seen as 8??
      // Apply amount/shift to current line.
      // do x++
  } while (x<=25);
      // Display all 26 shifted rows.

  return 0;
{
@TruYadoo

My understanding of the shifting you described would then be xamount = (row -1) + xamount. This program uses that shift pattern. Take the ideas from it, and apply it to your version of the program.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Letter Array.cpp : main project file.

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
using namespace System;

int main()
{
	int width = 115, height = 35, row, amount,xamount=1;
	Console::SetWindowSize(width, height ); // Make console window large enough to view lines
	char Letters[26][26], temp;
	int x, y, z;
	for(y=0;y<26;y++)
	{
		for(x=0;x<26;x++)
		{
			Letters[y][x] = 'A'+x;
		}
	}
	cout << endl << endl;
	cout << "      ";
	for (y=0;y<26;y++)
	{
		cout << y+1 << "  ";
		if(y<9)
			cout << " ";
	}
	cout << endl;
 row = 1;
	do
	{
		for(x=0;x<xamount;x++)
		{
		temp = Letters[row][0];
		for(z=0;z<26;z++)
		{
			Letters[row][z] = Letters[row][z+1];
		}
		Letters[row][25] = temp;
		}
		row++;
		xamount=(row-1)+xamount;
	} while(row<26);

	for (y=0;y<26;y++)
	{
		cout << "  " << y+1 << " ";
		if (y<9)
			cout << " ";
		for(x=0;x<26;x++)
		{
			cout << "[" << Letters[y][x] << "] ";
		}
		cout <<  " " << y+1 << endl;

	}
	cout << "      ";
	for (y=0;y<26;y++)
	{
		cout << y+1 << "  ";
		if(y<9)
			cout << " ";
	}
	cout << endl << endl << "\t\t\t\t\t"; // Just adding tabs to center the "Press any key.. " text
	return 0;
}
Thank you whitenite, it works. Partially however but it is shifting as expected once I changed (row-1) to just (row) on line 45. Since the first line counts as 0, the row # is technically already subtracted by 1.
Row 26 is counted as 25 so there is no need to subtract it further.

The partially part, the right side and bottom labeling is backwards, for my purpose.
Should be 26 decreased to 1 instead of 1 increased to 26. I should be able to change that.

Now I just need to figure out how to make this grid communicate with my algorithm.
Ex:
"ABC", A is letter #1 meaning row 1; B is letter #2 meaning row 2; C is letter #3 meaning row 3.
Find A within row 1 and take its column #, (being 1 for row 1), find B within row 2 and take its column #, (being 1 in row 2), find C within row 3 and take its column #. (being 26 in row 3)

I'm thinking that it'd be easier if I just duplicate the grid and flip the copy upside down instead of having it try to read the grid backwards. Where the end of row 26 in grid 1 is the start of row 1 in grid 2 and the start of row 1 in grid 1 is the end of row 26 in grid 2.

Whitenite, could you tell me what x, y and z represent. So I can figure out which creates the vertical numbering on the right side and which creates the horizontal numbering on the bottom so i can reverse both. please and thank you. I really appreciate the help!

@TruYadoo

Here's the program, with the requested numbering. Let me know if you get stuck on the algorithm. Maybe I can be of assistance.
Studying the program should help you understand the logic in it..
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Letter Array.cpp : main project file.

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
using namespace System;

int main()
{
	int width = 115, height = 35, row,xamount=1;
	Console::SetWindowSize(width, height );
	char Letters[26][26], temp;
	int x, y, z;
	for(y=0;y<26;y++) // 
	{
		for(x=0;x<26;x++)
		{
			Letters[y][x] = 'A'+x;
		}
	}
	cout << endl << endl;
	cout << "      ";
	for (y=0;y<26;y++)
	{
		cout << y+1 << "  ";
		if(y<9)
			cout << " ";
	}
	cout << endl;
 row = 1;
	do
	{
		for(x=0;x<xamount;x++)
		{
		temp = Letters[row][0];
		for(z=0;z<26;z++)
		{
			Letters[row][z] = Letters[row][z+1];
		}
		Letters[row][25] = temp;
		}
		row++;
		xamount=(row)+xamount;
	} while(row<26);

	for (y=0;y<26;y++)
	{
		if (y<9)
			cout << " ";
		cout << "  " << y+1 << " ";
		
		for(x=0;x<26;x++)
		{
			cout << "[" << Letters[y][x] << "] ";
		}
		if (y>16)
			cout << " ";
		cout << 26-y << endl;
	}
	cout << "      ";
	for (y=26;y>0;y--)
	{
		cout << y << "  ";
		if(y<=9)
			cout << " ";
	}
	cout << endl << endl << "\t\t\t\t\t";
	return 0;
}
THANK YOU! So, how can I access the information on the grid?
I'll need to have it:
Figure out what place the letter is in, in the original message.
Take the value/number of the letter's position and look through the corresponding row's array.
Find the letter within the row and figure out the column number that the letter is under.
(Either read the grid backwards or copy the grid and flip/rotate the duplicate.)
Find which letter intersects the row number and the column number.
Insert intersecting letter into the converted message and repeat until eof (end of message actually).
spaces carry over as spaces.

Google, here I come.
Last edited on
Pages: 12