6 a) Print the even numbers between 3 and 43.
b) Print every third letter starting with 'B'.
c) Print the numbers 1 to 20 in one column right next
to another column with the numbers 20 down to 1.
d) Print every other character in the computer's
character set.
Write this assignment as one program with 4 void
functions.
I've got A, B and C done. I'm stuck on D and have no clue where to even begin. Please help.
//Ali Amireh
//This program prints all of the even numbers between 3-43.
#include <iostream>
using namespace std;
void a()
{
int i;
for (i = 4; i <= 43; i = i + 2)
cout << i << " ";
cout << endl;
}
//Print every third letter starting with the letter B.
void b()
{
char letter;
for (letter = 'B'; letter <= 'Z'; letter = letter + 3)
cout << letter;
cout << endl;
}
//This program counts up from 1-20 in one column and counts down from 20-1 in the other.
void c()
{
int i;
//counter
for (i = 1; i <= 20; i = i + 1)
cout << i << "\t" << 21 - i << endl;
cout << endl;
}
That problem is worded way too ambiguously. Given the other parts of the problem, I would suppose that the most likely expected response would be something like:
1 2 3 4
for(unsignedchar c = 0; c < 255; c += 2)
{
std::cout << c << std::endl;
}
A "computer's character set" printout would probably require some platform specific API calls to obtain. They probably want you to print out the ascii table, but ask them before taking my word for it!
I tried something similar to the code you supplied, but all it gave me was an infinite program and the previous three programs stopped working. What the professor wants is all the characters in ASCII, which is somewhere under 300 characters.
Do you know what compiler that you are using? Usually if a signedness related infinite loop is encountered, it will let you know. I'm assuming that's what happened in your specific case.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main()
{
for(unsignedchar c = 0; c < 255; ++c) // Works as expected
std::cout << c << std::endl;
for(char c = 0; c < 255; ++c) // A 'SIGNED' char only goes up to 127, it wraps before 255 is reached!
std::cout << c << std::endl;
return 0;
}