### Convert numbers into letters and multiply the number by itself

If I Input
8 5 12 12 15 23 15 18 12 4

It should Output
helloworld
8:8-16-24-32-40-48-56-64
5:5-10-15-20-25
12:12-24-36-48-60-72-84-96-108-120-132-144
12:12-24-36-48-60-72-84-96-108-120-132-144
15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225
23:23-46-69-92-115-138-161-184-207-230-253-276-299-322-345-368-391-414-437-460-483-506-529
15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225
18:18-36-54-72-90-108-126-144-162-180-198-216-234-252-270-288-306-324
12:12-24-36-48-60-72-84-96-108-120-132-144
4:4-8-12-16

I think I got the answer but I'm doing something wrong in my code, I'm pretty new to programming still and have been doing fine so far till I encounter the loop and functions.

Here's the closest to what I've got.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include using namespace std; char secretCode(char number) { if(number >= 1 && number <= 26) { return static_cast('a' - 1 + number); } else if (number >= 27 && number <= 52) { return static_cast('a' - 27 + number); } else if (number >= 53 && number <= 104) { return static_cast('a' - 53 + number); } } void printSequence(int number[10]) { for (int i=0; i<10; i++) { cout << secretCode(number[i]); } } int main() { int number[10]; for(int x=0; x<10; ++x) { cin >> number[x]; printSequence(number); for(int y=number[x]; y<=number[x]; ++y) { for(int z=1; z<=number[x]; ++z) { if (z>1) { cout << "-"; } if (z<1) { cout <
You are trying to print the letter sequence out in the middle of inputting the numbers. You need to separate those operations.

You've also got a lot of magic numbers hanging around, an unnecessary if block, and an unnecessarily difficult way of printing out a '-' character.

Why don't you break it down into functions? Then you can ensure that separate things are done at the right time.
 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; void getNumbers( int numbers[], int n ) { // Your code } void printSequence( int numbers[], int n ) { // Your code } void printMultiples( int n ) { // Your code } int main() { const int SIZE = 10; int numbers[SIZE]; getNumbers( numbers, SIZE ); printSequence( numbers, SIZE ); for ( int e : numbers ) printMultiples( e ); }``````

 ```Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 abcdefghij 1:1 2:2-4 3:3-6-9 4:4-8-12-16 5:5-10-15-20-25 6:6-12-18-24-30-36 7:7-14-21-28-35-42-49 8:8-16-24-32-40-48-56-64 9:9-18-27-36-45-54-63-72-81 10:10-20-30-40-50-60-70-80-90-100```
> I think I got the answer but I'm doing something wrong in my code,
¿what are the symptoms?

`for(int y=number[x]; y<=number[x]; ++y)`
¿how many times does that execute?
hi thanks for trying to help but i got my question answered by a fellow friend
here's the answer for those who wanted to know
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include using namespace std; char secretCode(char number) { if(number >= 1 && number <= 26) { return static_cast('a' - 1 + number); } else if (number >= 27 && number <= 52) { return static_cast('a' - 27 + number); } else if (number >= 53 && number <= 104) { return static_cast('a' - 53 + number); } } void printSequence(int number[10]) { for (int i=0; i<10; i++) { cout << secretCode(number[i]); } cout << endl; int len = 0; while(len < 10){ cout << number[len] << ":"; for(int z=1; z<=number[len]; ++z){ if (z>1) { cout << "-"; } if (z<1) { cout <> number[x]; } printSequence(number); }``````
Last edited on
Well don't always trust your friend - that code has issues!

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include constexpr size_t maxNum {10}; char secretCode(int number) { if (number >= 1 && number <= 26) return static_cast('a' - 1 + number); if (number >= 27 && number <= 52) return static_cast('a' - 27 + number); if (number >= 53 && number <= 104) return static_cast('a' - 53 + number); return static_cast(('a' - 105 + number) % 127); } void printSequence(int number[maxNum]) { for (size_t i = 0; i < maxNum; ++i) std::cout << secretCode(number[i]); std::cout << '\n'; for (size_t len {}; len < maxNum; ++len) { std::cout << number[len] << ":" << number[len]; for (int z = 2; z <= number[len]; std::cout << '-' << number[len] * z++); std::cout << '\n'; } } int main() { int number[maxNum] {}; for (size_t x = 0; x < maxNum; ++x) std::cin >> number[x]; printSequence(number); }``````

 ``` 8 5 12 12 15 23 15 18 12 4 helloworld 8:8-16-24-32-40-48-56-64 5:5-10-15-20-25 12:12-24-36-48-60-72-84-96-108-120-132-144 12:12-24-36-48-60-72-84-96-108-120-132-144 15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225 23:23-46-69-92-115-138-161-184-207-230-253-276-299-322-345-368-391-414-437-460-483-506-529 15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225 18:18-36-54-72-90-108-126-144-162-180-198-216-234-252-270-288-306-324 12:12-24-36-48-60-72-84-96-108-120-132-144 4:4-8-12-16 ```

Last edited on
 ``1234`` `````` else if (number >= 53 && number <= 104) { return static_cast('a' - 53 + number); }``````
There are 52 numbers from 53 to 104. If `number` is 100, your code will return character number 97 - 53 + 100 = 144, which is outside the ASCII range.

Also, are you guaranteed to get exactly 10 input numbers? Your code assumes it.
@kazeroth900,

Your "secretCode" doesn't make much sense. Most codes are invertible/decodeable - yours isn't, because it's many-to-one.

It is also nearly - but not quite -
`return (char)( 'a' + ( number - 1 ) % 26 );`
Was that what you meant? It wouldn't be any more decodeable, but it would at least give a meaningful and plausible output for large n.

There's no need at all for an if block to output '-'. The first parts of the output are always
 `number : number`

and you can easily start the multiples loop from 2* instead of 1*, with '-' output each time.

Maybe you should give the whole problem in its original form. Your "friend" has given you dire code.
Last edited on
Topic archived. No new replies allowed.