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.
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?
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:
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:
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)
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.