greyk0
greyk0

Reputation: 35

Letter pyramid C++

i'm a C++ beginner. I figured out that spaces before input can be calculated by the formula total row - current row number.  However, i failed to figure out how to display input backwards. I know C++ does not support negative index. Could anyone please help me out? I'd really appreciate your help. Thank you!

the expected result:

If the user enters 'ABCDEFG', then your program should display:

      A                                                                                                                
     ABA                                                                                                               
    ABCBA                                                                                                              
   ABCDCBA                                                                                                             
  ABCDEDCBA                                                                                                            
 ABCDEFEDCBA                                                                                                           
ABCDEFGFEDCBA

My current result:

      A
     ABC
    ABCDE
   ABCDEFG
  ABCDEFGAB
 ABCDEFGABCD
ABCDEFGABCDEF

Code:

#include <iostream>

#include <string>

int main(){
    std::string user_input;
    std::cout << "Please enter something" << std::endl;
    std::cin >> user_input;
    size_t total_row = user_input.size();
    for(size_t row {1}; row <= total_row; row++){
        for(size_t blank {total_row-row}; blank >= 1; blank--)
            std::cout << " ";
        if ( total_row <= 1){
            std::cout << user_input;
        }
        else {
            for(size_t i {0}; i < ((row * 2) - 1); i++)
                if (i >= total_row){
                   std::cout << user_input.at(i - total_row); 
                }
                    
                else{
                std::cout << user_input.at(i);
                
                }
        }
        std::cout << std::endl;
    }
}

Upvotes: 2

Views: 988

Answers (3)

greyk0
greyk0

Reputation: 35

Finally, I figured out the solution :) Thanks all for helping me to solve this challenge. AS a C++ beginner, this was challenging and fun :)

This is my final solution code:

#include <iostream>
 
#include <string>
 
int main(){
    std::string user_input;
    std::cout << "Please enter something" << std::endl;
    std::cin >> user_input;
    size_t total_row = user_input.size();
    
    for(size_t row {1}; row <= total_row; row++){
        
        for(size_t blank {total_row-row}; blank >= 1; blank--)
        std::cout << " ";
        
        if ( total_row <= 1){
            std::cout << user_input;
        }
        else {
            for(size_t i {0}, j {row-1}; i < ((row * 2) - 1); i++)
                if (i >= row){
                    j--;
                   std::cout << user_input.at(j); 
                   
                }
                    
                else{
                std::cout << user_input.at(i);
                
                }
            
        }
        std::cout << std::endl;
        }
    


    return 0;
}

Upvotes: 0

Yunfei Chen
Yunfei Chen

Reputation: 626

The thing with this problem is that you actually only want to go halfway with your second for loop so you want to stop at arr[size/2] so if you had three values you would stop at AB and later you would go through until size == the full array size, ABA so you would need two for loops. So the last one would be set to the first one of the string, the second last one to the second one and so on, so basically the two for loops would go from zero to arr[size/2] and arr[size/2] to size. Finally you would still need the outer for loop for every level of the pymaid.

Upvotes: 0

einpoklum
einpoklum

Reputation: 131515

Well, at every row, you go forward in the string until the mid-point, then you go backwards:

A (+1) B (+1) C (+1) D (-1) C (-1) B (-1) A

Two ways of achieving this:

  1. Use two inner loops instead of one: from 0 to row, and then down from row - 1 to 0.
  2. Instead of printing the character at i - total_row, calculate more exactly which character needs to be printed at that position - e.g. using the minimum distance from the edges of the pyramid.

Good luck.


PS - As @ShadowMitia mentions in a comment - it's not true that C++ doesn't support negative indices. If you use a signed type (e.g. int) for your indices, they can be negative. But you don't really need negative indices to follow my advice.

Upvotes: 5

Related Questions