Printing out the alphabet in specific format

Can anyone help me print out the alphabet like this:

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

EXACTLY how it looks using while and counter statements?

I know I have to declare char letter = 'A' and while (counter = 0) right? I honestly have no sense of how they work. My prof. Grazed over the topic very vaguely. Thanks in advance!
Okay so the first thing you need to do is represent the alphabet in some fashion. One way to do this would be using a container of some sorts. I am not sure what you have learned containers wise but either an array (or std::array) of chars or an std::vector of chars would work perfectly for this.

For example here is a std::vector of chars that goes A-C
std::vector<char> alphabet = { 'A', 'B', 'C' };

Now there are two more problem that you need to solve. The first is setting up your while loop to go over every single element in the vector (or array) and print it to the console.

After you have solved that problem you then need to take care of the next problem. Getting it to only print 5 character (In other words 5 elements) per line. One way to do this would be to keep a counter of sorts that counts up to 5, when it hits 5 it will print a newline character to the console and hence start a new line.

For example here is some pseudo code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int newlineCounter = 0; // The counter I talked about.
int elementCounter = 0; // You will use this counter to tell you where you are at in the vector or array

while (not at the end of the vector)
{
    print element;

    ++newlineCounter;

    // Check to see if there are 5 characters on a line already
    if (newlineCounter equals 5)
    {
        print a newline;
        reset newlineCounter to 0;
    }

    ++elementCounter;
}


That should give you a general idea of the problems that are involved in the assignment and how to possibly go about it. As always it is easier to start small by breaking everything down into small steps that you need to accomplish and then build on the program from there.

Wish you the best of luck with your assignment.
Last edited on
Try this ._.) Idk if works or not
1
2
3
4
5
6
7
8
char letter='A'
int counter=0
while counter < 26
  Print(letter, ' ')
  if counter % 5 is 0
    then print(new_line)
  letter=letter+1;
  counter=counter+1
Last edited on
RE: Zereo's suggestion. Don't forget to reset your counter when you print a newline! (Or use modulo arithmetic.)

Instead of using a container to hold the letters, perhaps you could generate them on the fly. Start with char letter = 'A'; and at the end of the loop body say ++letter; to increment to the next letter. Your while loop would run while (letter <= 'Z').
Last edited on
why not use static_cast<data_type>(variable_name)
1
2
3
4
5
6
7
8
9
int letter = 1;
int counter = 0;
while ( counter < 26 ) {
cout << static_cast<char>(letter);
letter++;
counter ++;
if ( counter % 5 == 0)
cout << endl;
}

sorry if there's an error , i dont have my compiler now
closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
 {
  char alphabet = 'A';
  int c = 0;

  while (alphabet <= 'Z') {
    printf("%c ", alphabet);
    alphabet++; c++;
     if (c == 5 || c == 10 || c == 15 || c == 20 || c == 25)
       printf("\n");
   }
 }


working on a way to make if statement check simpler.

output is

1
2
3
4
5
6
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

used code in brackets to make output look better
letters looked misaligned.
Last edited on
closed account (1CfG1hU5)
for loop or do while could be used also.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
 {
  char alphabet;
  int c = 0;

 for (alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
    printf("%c ", alphabet);
    c++;
      if(c == 5 || c == 10 || c == 15 || c == 20 || c == 25)
         printf("\n");
    }
 }


do while loop is closer to the while one.
not much need write that one out.
Last edited on
closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
 {
  char alphabet = 'A';
  int c = 0;

  while (alphabet <= 'Z') {
    printf("%c ", alphabet);
    alphabet++; c++;
     if (c % 6 == 0)
       printf("\n");
   }
 }


redone for 6 per line.
output is

1
2
3
4
5
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


changed from this inside if statement
c == 6 || c == 12 || c == 18 || c == 24

% mod remainer much better
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
	int counter = 0;

	for (char c = 'A'; c <= 'Z'; c++)
	{
		std::cout << c << " ";
		counter++;

		if (counter % 6 == 0)
			std::cout << std::endl;
	}
	return 0;
}
closed account (1CfG1hU5)
that if with % mod remainder works good.

less work
closed account (48T7M4Gy)
Yeah just building on your good work jt1 :)
closed account (1CfG1hU5)
this works too
should be a way to simplify the if with a smaller statement

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main()
 {
  char a = 'A';

  while (a <= 'Z') {
    printf("%c ", a);
    a++;
     if (a % 71 == 0 || a % 77 == 0 || a % 83 == 0 || a % 89 == 0)
       printf("\n");
   }
 }


that looks a little better with a short char name
Last edited on
closed account (1CfG1hU5)
this if isn't bad

 
if (a % 71 == 0 || a % 77 == 0 || a % 83 == 0 || a % 89 == 0)


not that long

having trouble simplifying with += and == operators. can't check equivalency
and assign.

closed account (1CfG1hU5)
i should use cout more in iostream.h. the format is good.

tend to use %d with printf in stdio

closed account (1CfG1hU5)
changed so "a" increments later. how should have had before
which changes some if statement #'s. "a" doesn't get ahead by 1 when
checking remainders. ie, "a" is not 'B' when if statement starts. char a,
remains decimal value 65 until end of loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
 {
  char a = 'A';

  while (a <= 'Z') {
    printf("%c ", a);

     if (a % 70 == 0 || a % 76 == 0 || a % 82 == 0 || a % 88 == 0)
       printf("\n");
   a++;
   }  // end while

 return 0;

 }

Last edited on
closed account (1CfG1hU5)
iostream version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
 {
  char a = 'A';

  while (a <= 'Z') {
    cout << " " << a;

     if (a % 70 == 0 || a % 76 == 0 || a % 82 == 0 || a % 88 == 0)
       cout << "\n";
   a++;
   }  // end while

 return 0;

 }


closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
 {
  char a = 'A';
  int c = 0;

  while (a <= 'Z') {
    cout << a << " ";
    a++; c++;
     if (c % 6 == 0)
       cout << "\n";
   }
 }


adding an integer for counting seems to be the most efficient way of doing
the if remainder. is there a way that only "a" could be used and get a small
if statement like is above?

Last edited on
closed account (48T7M4Gy)
There is a way:

Delete all references to c and replace line 12 with if((a +1)%6==0)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
 {
  char a = 'A';

  while (a <= 'Z') {
    std::cout << a << " ";
    a++;
     if ((a+1) % 6 == 0)
       std::cout << "\n";
   }
 }


1
2
3
4
5
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  
Last edited on
Thank you all. The point of my assignment was to learn how to use a while loop and print to next line once the has exceeded a specific number. I have a pretty good understanding now.
Topic archived. No new replies allowed.