+ 2

Help me pls. Taking 13 characters in array and the number of rows; i have to print a proper pyramid using (for-do while-while)

https://www.sololearn.com/compiler-playground/cAbgIj1boAFt #include <iostream> using namespace std; int main() { int space, rows; char arr[13]; cout<<"Enter 13 characters : "<<endl; for(int x=0;x<13;x++){ cin>>arr[x]; } cout <<"Enter number of rows: "; cin >> rows; for(int i = 1, k = 0; i <= rows; i++, k = 0) { // for(space = 1; space <= rows-i; space++) // { // cout <<" "; // } space=1; do{ cout<<" "; space++; }while(space<=rows-i); while(k != 2*i-1) { cout <<arr[k]<<" "; k++; } cout << endl; } return 0; } input:1234567891234 input:7 output: 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 1 2 3 4 5 6 7 8 9 1 2 3 4 //as you can see the last line has a space that ruins the pyramid. kindly help me with this. the submission date is near.

23rd Oct 2022, 12:32 PM
Mahdi
Mahdi - avatar
5 Answers
+ 3
Mahdi your pyramid is skewed to the left. To place the peak in the center, the number of leading spaces should be doubled to account for the extra space between characters. Calculate it as (rows-i)*2 spaces.
23rd Oct 2022, 3:09 PM
Brian
Brian - avatar
+ 5
tajinder singh welcome to Sololearn! Yes, there are people here who know C. Please do not hijack another person's discussion. You may post your own question to start a new discussion. When you post, be specific about what help you need.
23rd Oct 2022, 2:46 PM
Brian
Brian - avatar
+ 3
Use while loop, inplace of do-while. Else take i < rows : < instead of <=
23rd Oct 2022, 1:21 PM
Jayakrishna 🇼🇳
- 1
Hello
23rd Oct 2022, 1:35 PM
tajinder singh
tajinder singh - avatar
- 1
Anyone know here c language coding?
23rd Oct 2022, 1:36 PM
tajinder singh
tajinder singh - avatar