0

I am writing code in c++ which must have an output hereinbelow:

1******

12*****

123****

1234***

12345**

123456*

1234567

I have done displaying integers but the issue is to couple up asteriks sign to them.

Here is the code:

#include<iostream>
using namespace std;
int main () {
    int i,j;
    for(int i=1;i<8;i++) {
        for(int j=1;j<=i;j++){

    cout<<j; 
    } cout<<endl;}

return 0;
}

Thank you. This is the output of the process I have done until.

  • 2
    Start slowly. Try just adding an asterisk to every line. Then maybe add an asterisk to every line but the last one, then just keep going. Work out how many asterisks appear on each line and whether you could somehow work that out for each `i`. – Tas May 07 '19 at 01:16
  • 2
    Indeed - the goal of this assignment is to _think about it_, applying some problem solving techniques to reach a conclusion. Just asking us to do it for you will not teach you anything! – Lightness Races in Orbit May 07 '19 at 01:17
  • Much appreciated for your admonition. Getting help only for syntax was my last choice, since I have been thinking and trying for a week. – justabegginer May 07 '19 at 01:27
  • [Have a look](https://ideone.com/dWDdpL) – Aykhan Hagverdili May 07 '19 at 01:36
  • Well thanks, @Ayxan, I will have a good look at it. – justabegginer May 07 '19 at 01:46

1 Answers1

2

Here is the answer to your problem, but I do think that you should think of the solution yourself first before taking a look into the answer.

Here is some tips for your answer:

  1. Try to think about how you print 1 , 12, 123 and how it work reversely (123,12,1)
  2. Replace the number with pure char or string: '' or ""
  3. Think about the place you should put your code
  4. Test it
  5. If it is not the result you want but close enough, try to modify the code section of for([initialization];[end condition];[incremental])

And also here is some advice on your future road of programming:

  1. Don't use using namespace std;, it might be convenience for now, but it is a bad practice to use it on your coding.
  2. Use starting index from 0. This practice can help you loop easily through out an array, list, vector and any other data structure that work like an array (IEnumerable in .NET).
  3. You have already initialize the integer inside the for loop, so actually you can omit the code section int i,j;

Answer:

#include <iostream>
int main () {
    for(int i=0;i<7;i++) {
        for(int j=0;j<=i;j++)
            std::cout<<j+1; 
        for(int k=0;k<(7-(i+1));k++)
            std::cout<<"*";
        std::cout<<std::endl;
     }
return 0;
}
LEE Hau Chon
  • 435
  • 3
  • 12
  • I'm not sure spoiler works for it, but you don't need to use it anyway. You lead your answer with tips and advice first, so if OP truly wanted to use that instead they could, but the answer is there if they also wish to just use it. TBH I'd probs remove the `using namespace std` from your code, since you advise against it. It's not much effort to just replace it, plus you'd be leading by example. – Tas May 07 '19 at 01:52
  • I have read many texts about "using or not using namespace std". Even so I keep writing it as a habit which stems from school. Besides, I just read the method you preferred and your advice. Now I will write it myself. Well thanks! – justabegginer May 07 '19 at 02:06