Nested Loops Modulus

+5 Doctor Doctor · December 3, 2014
how can i create stars using c++ nested loops or modulus in this order 1-3-5-3-1

thank you to whoever that can help me, i really appreciate it

Post a Reply

Replies

Oldest  Newest  Rating
0 Homer Simpson · December 3, 2014

#include <iostream>
#include <string>

int main()
{
    const int LOOP_SIZE = 10;

    for(int i = 0; i < LOOP_SIZE; i++){
        std::string stars(i, '*');

        if(stars.size() % 3 == 0){
            std::cout << stars << std::endl;
        } else if (stars.size() % 5 == 0){
            std::cout << stars << std::endl;
        }
    }
}

I don't really understand your question. How exactly do you want to display the stars? Everything modulus 1 = 0 so that would just print out 1 star then 2..3..4 so on. Anywho, this code prints this.

***
*****
******
*********
0 Doctor Doctor · December 31, 2014
#include <iostream> 
using namespace std; 
int main() 

int a , b ; 
int operation ; 
cout<<" enter two numbers : " << endl ; 
cin>> a ; 
cin>> b ; 
cout<<"what the operation do you want :" <<endl; 
cout<<"1.add " << endl ; 
cout<<"2.subtract " << endl; 
cin >> operation ; 

switch(operation){ 

case 1 : 
cout<<"a+b = " << a+b; 
break; 
case 2 : 
cout<<"a-b = " << a-b; 
break; 
default: 
cout<<" " <<endl; 
break; 


system ("pause"); 
return 0; 
}
I have to declare new functions and call them in Main(). Main should only have 4-5 lines max in it. Outside of main you'll have the functions.I would like it to start with 

What would you like to do add or subtract
then
after user selects add or subtract
then 
perform the operation either add or subtract that was selected.
Can you tell me what I did wrong and I am using C++
0 Stan Botha · December 22, 2014

#include <iostream>
using namespace std;

void displayRow(int n)
{
for (int i = 1; i <= n; i++)
cout << '*';
cout << endl;
}

int main( )
{
int m = 10;

for (int j = 1; j <= m; j++)
     {
          if(j<=(m/2) && j%2!=0)
               displayRow(j);
          else if (j>(m/2+1) && j%2!=0)
               {
                    displayRow(m-j);
               }
     }
return 0;
}
0 Franz Schmidt · December 31, 2014
Your code just has one function (the main function) and you just need to split the main in some partfunction which you combine in the main.

So you mean something like this or something else?
https://paste.ab-storm.de/view/d1065372034bf1b270535fb35adb2d13

Hope you are familiar with functions and struct.
Hope you understand everything. If there are any questions, just ask :D
  • 1

C++

106,985 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator