I have been working on this for 2 days now and cant seem to get it right. So any help would be greatly appreciated. For this problem i should be able to input a number between 1 and 10. And that number must decrement down to one using a for loop and be displayed diagonally. As for the other side it should increment upwards from 1 to the number inputted and also displayed diagonally. The periods are place holders for spaces.
Example of an output if the input was 7:
7.....1
.6...2
..5.3
...4
..5.3
.6...2
7.....1
do{
cout << "Input a positive number." << endl;
cin >> X;
cout << endl;
if(x<=0){
cout << "Invalid entry." << endl;
}
else{
for(int i = 1, X; i > X, j > 0; ++i, --X)
cout << i << " " << X << endl;
}
while( x <= 0);
I will not being doing this in the future, where I write out the code as I think of it, but I was typing what I was thinking bc Im tired hope it helps. By the way I can't say I completely understand what your saying
"7.....1
.6...2
..5.3
...4
..5.3
.6...2
7.....1" here.
Im sorry i may have made it difficult to understand, and that is what must be outputted. If i input a 7 then it should decrements down to one diagonally, and then the opposite way should be incremented from 1 to 7. That is the purpose of the 'X'. The periods are just a place holder for spaces.
I thought I had solved it without an if.. else... but in fact it printed a zero value instead of a space which I can't think of a way to change to a space without using an if... else... 50001
04020
00300
04040
10005
The problem is I have an answer and I have given you all the clues needed (if you use an if.. else..) without actually giving the complete answer and thus do the assignment for you.
The question is do you want to learn c++ or just have an answer to the assignment?
There is no shame in not working it out as it is probably too hard for a beginner.
Honestly i tried being a good student and try and learn it with only hints, just i really need this problem done. Just means i need to ask my tutor to go over the things im unsure. So if you wouldn't mind giving me the answer, i would greatly appreciate it.
Trust me he knows i have been asking for help. Thank you for your help!! i will see if i can get the best answer and post it on here soon. Thank you again for your help.
#include <string>
#include <iostream>
int main()
{
int L = 0, R = 0, X = 0, temp = 0;
std::string left = "", middle = "";
std::cout << "Enter a number:\n";
std::cin >> X;
for (int i = 1; i <= X; i++)
{
L = X - i + 1 ;
R = i;
if ( L < R )
{
temp = L;
L = R;
R = temp;
}
left.assign( R-1,'*' );
if (L == R)
std::cout << left << L << std::endl;
else
{
middle.assign( L-R-1, '*' );
std::cout << left << L << middle << R << std::endl;
}
}
return 0;
}