Printing Corresponding Asterix to numbers

How do i print the corresponding number of asterix (*) with regards to the numbers printed by the loop.
Can't seem to get the logic right. i am only able to print for the minimum and max values as well as the interval but not the incremental.
E.g value range min: 3, max: 12 with interval:3 meaning loop prints 3,6,9,12 but i am only able to print the asterisks for 3 and 12 and not for 6 and 9.

Help please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    int min, max, intvr;

    cout<<"This program prints out the minimum, maximum and interval values"<<endl;
    cout<<endl;
    cout<<"enter mininum number: ";
    cin>>min;
    cout<<"enter maximum number: ";
    cin>>max;
    cout<<"enter interval number: ";
    cin>>intvr;
    
    for(int i=min; i<=max; i+=intvr)
    {
        cout<<i<<" "<<"*";
        cout<<endl;
    }
      
   
    
}
Last edited on
What asterisks? Your code does not attempt to print any.
Why not use modulus math to get the numbers you want?
Then you can use if else statements to print it out
@Karakuik, any help/explanation of how to use the modulus will be much appreciated.
I am new to programming.
My logic is quite weak

modulus is the "remainder" of integer division. that is, 10/3 is 3, remainder (modulus) of 1. this would be found by % operation, eg r = 10%3;

I can't see a good reason to use % here. I mean, you *could*, but its just convolution for no gain. Looks something like
for(int dx = min; dx <= max; dx++)
if(dx%intvr == 0)
cout << dx << "*" << endl;

note that this is not really the same as what you were doing. if you put in 2 and 12 and 3, it would give you 3, 6, 9, 12, not 2,5,... you can correct that but it gets even more convoluted. So I am not sure what the point of that would be.

your program appears to work for me. I see 3,6,9,12 output from your example.
Recompile your current version and make sure it is actually still incorrect in some way?
Last edited on
Your program's output:
This program prints out the minimum, maximum and interval values

enter mininum number: 3
enter maximum number: 16
enter interval number: 4
3 *
7 *
11 *
15 *

Now you print some asterisks.

How does that output differ from what you should print?

What, exactly, should you print?
What is the rule that your program must follow?


Was it in
corresponding number of asterix (*) with regards to the numbers

Do you mean that when the number is N, then you should print N asterisks on that line?
Like:
1
2
3
4
5
6
for (int i=min; i<=max; i+=intvr)
{
  cout<<i<<" ";
  // print i '*' here
  cout << endl;
}


Forget the rest and focus on the // print i '*' here
What is that? Is it:
repeat (print *) for i times

One can easily repeat an operation with a loop.
Last edited on
With this code i am getting the output i want. However, when the maximum value (24
) is a little bigger then i am struggling with the logic to loop through all the interval values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int main() {
    int min, max,intvr,rpt;
    cout<<"This program prints out lines of stars"<<endl;
    cout<<endl;
    cout<<"enter mininum number of stars"<<endl;
    cin>>min;
    cout<<"enter maximum number of stars"<<endl;
    cin>>max;
    cout<<"enter interval number of stars"<<endl;
    cin>>intvr;
    cout<<endl;
    // minimum stars printing
    for (int i=0; i<min; i++)
        cout<<"*";
    cout<<" "<<"("<<min<<" stars)";
    cout<<endl;
    
    //interval stars printing
    for(int t=0; t<min+intvr; t++)
        
        cout<<"*";
    cout<<" "<<"("<<min+intvr<<" stars)";
    cout<<endl;
    
    rpt=min+intvr;
    //repeat interval stars printing
    for(int r=0; r<rpt+intvr; r++)
        
        cout<<"*";
    cout<<" "<<"("<<rpt+intvr<<" stars)";
    cout<<endl;
    
    // maximum stars printing
    for(int k=0; k<max; k++)
        cout<<"*";
    cout<<" "<<"("<<max<<" stars)";
    cout<<endl;
    
}
With this code i am getting the output i want.

No. With that code you get what resembles what you expect, but if and only if your test case happens to match special criteria.

1
2
3
4
5
6
7
for (int i=min; i<=max; i+=intvr)
{
  // INSERT a loop HERE that prints i stars

  cout << " " << "(" << i << " stars)";
  cout << endl;
}
Last edited on
i am missing numbers 12,15,18,21.
i need a loop/logic of some sort to help me capture these values.
This program prints out lines of stars

enter mininum number of stars
3
enter maximum number of stars
24
enter interval number of stars
3

*** (3 stars)
****** (6 stars)
********* (9 stars)
************************ (24 stars)
we gave it.

for(dx = min, dx <= max; dx+= intvr)
{
for(int i = 0; i < dx; i++)
cout <<"*";
cout << " " << dx << "stars" << endl;
}
Manny thanks for the assistance Jonnin.
Any recommended resources to help me with my logic development?

Thank you.
practice! In time you will being to think this way, and it will be natural to you for basic stuff. Just keep doing these types of exercises and move to harder ones as you progress. A lot of it follows the 'can't be unseen' concept. Like the above, now you see it, if you understand it, next time that comes around you should see what to do again.
Last edited on
Topic archived. No new replies allowed.