Writing an array without if statement

Dear Friends, I need a code to print this array without using if statement:
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
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 A
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z 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
use a for loop.

for(int i=0;i<=strlen(array)-1;i++)
cout<<array[i];
Last edited on
You know that the ASCII code for 'A' is 65, and for 'B' it's 66, etc. So 'B' = 'A' + 1, 'C' = 'A' + 2, ...

And you know how operator% works?

Andy
Yes Mr. Andy, I know the ASCII Code. But I couldn't do it.

Is there anyone can help me,please :)
Last edited on
Can you display A B C ... with a single loop and ASCII codes?

Andy
Yes,I can

char x;
for(x=65;x<91;x++)
cout<<x<<endl;

but I don't how to make that doubled array above
4%2==0
5%2==1

I know how it works too
If you join those two pieces together, you can display the array you show in your opening post.

The outer loop controls the starting char (or ASCII code)
The innner loop prints a line
operator% wraps the code to display so that it stays in range

Andy
OK Andy, excellent idea!
I tried another way to accomplish this thread: I filled a vector with the English alphabet(see the next bold!) followed by rotations(two positions each time!) to obtain 26 lines.
Last edited on
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
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
	std::vector<char> myVec;
	std::vector<char>::iterator it;
	
//Fill myVec: English alphabet + Space after each letter
	for (int i = 65; i < 91; ++i)
	{
		myVec.push_back((char)i);
		myVec.push_back((char)32);
	}
	
//Print out initial myVec
	for (it = myVec.begin(); it < myVec.end(); ++it)
		std::cout << *it;
	std::cout << '\n';	
	
//Rotate & print out myVec
	for (int i = 0; i < 25; ++i)
	{
		std::rotate(myVec.begin(), myVec.begin() + 2, myVec.end());
		for (it = myVec.begin(); it < myVec.end(); ++it)
			std::cout << *it;
		std::cout << '\n';	
	}
	return 0;
 }
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 
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 A 
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B 
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D 
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E 
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F 
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G 
I J K L M N O P Q R S T U V W X Y Z A B C D E F G H 
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I 
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J 
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K 
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L 
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M 
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N 
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O 
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P 
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q 
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R 
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S 
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T 
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U 
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V 
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X 
Z 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 
Topic archived. No new replies allowed.