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.
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').
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");
}
}
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;
}
#include <iostream>
usingnamespace 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?
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.