Need help with homework

Here's a picture of the worksheet.

http://imgur.com/a/43HzTPz
There's no source code like what it says in instruction 2.

Just wanna know how would you guys do it?
Does it have to be exactly that smiley face? Clearly, one answer would be to just hardcode the string that produces the required characters, and print that.

If we want to get a very tiny bit more creative, you could make an array of {character, run length} pairs.
For example, the first line output looks like it starts with ~26 spaces, so the first entry in your array would be {' ', 26}. This is then followed by * characters, so the second entry would be {'*', 31}. After that, you can simply print a newline, so {'\n', 1}.

e.g. your array would start to look like:
1
2
3
4
5
6
7
struct PrintPair {
    char character;
    int runlength;
};

PrintPair arr[] = { {' ', 26}, {'*', 31}, {'\n', 1}, // first line
                    {' ', 23} };                     // start of second line  

Then, you do a 2x-nested for loop to print the each character runlength number of times.
Last edited on
thank you very much, yeah it has to be exactly that smiley face to the '*'.
I don't understand what your code is doing exactly, but this is what I found out.

1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;
int main()
{
	cout << string(26, ' ')<<string(31,42)<<endl;
	cout << string(23, ' ') << string(37, '*') << endl; //etc... etc...
    return 0;
}


I'm definitely going to do it your way. I just wish i understood more of what your code is doing. I JUST started programming
Last edited on
@jemeu There is nothing wrong with the other way. They both do the job.

Your's does exactly the same execpt \\etc ... should be // etc ... '//' is a comment '\\' in your case is an error :(
@againtry alright as long as I'm not trying cout for each line I think I'm happy with it!!

But I would like to know how his is working else I'll figure it out on my own
'His' when completed would be e.g. for arr[1] which is {'*', 31}

1
2
3
4
for( int i = 0; i < 31; i++)
    cout << '*';

    cout << '\n';


which is the same 'your' way as:
cout << string(31, '*') << '\n';
Right, same thing.
Or, equivalently, you could do
1
2
3
4
for (int i = 0; i < arr_size; i++)
{
    cout << string(arr[i].runlength, arr[i].character);
}

But it's the same thing essentially. My way just stores all the data up-front instead of inside the statements themselves.
Last edited on
Since each line has the pattern: spaces, stars, spaces, stars, ..., newline
you could just store the runlengths.

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
#include <iostream>
#include <iomanip>
#include <vector>

void segment(int n, char ch) {
    std::cout << std::setfill(ch) << std::setw(n) << "";
}

int main() {
    std::vector<std::vector<int>> data {
        { 26, 31 },
        { 23, 37 },
        //...
        {  1, 18,  9, 18,  9, 22 },
        {  0, 18, 11, 16, 11, 22 },
        // ...
    };
    const int indent = 4; // overall indent from left side
    const char BlankChar = ' ', MarkChar = '*';

    for (const auto& row: data) {
        segment(indent, BlankChar);
        bool spaces = true;
        for (int runlength: row) {
            segment(runlength, spaces ? BlankChar : MarkChar);
            spaces = !spaces;
        }
        std::cout << '\n';
    }
}

Last edited on
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
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

const double PI = 4.0 * atan( 1.0 );


class Picture
{
   int width, height;
   vector<string> canvas;
public:
   Picture( int w, int h, char c = ' ' ) : width( w ), height( h ) { canvas = vector<string>( h, string( w, c ) ); }

   void arc( double xc, double yc, double r1, double r2, double a1, double a2, char c = ' ' )
   {
      int imin = xc - r2 - 1;   if ( imin <  0      ) imin = 0;
      int imax = xc + r2 + 1;   if ( imax >= width  ) imax = width - 1;
      int jmin = yc - r2 - 1;   if ( jmin <  0      ) jmin = 0;
      int jmax = yc + r2 + 1;   if ( jmax >= height ) jmax = height - 1;

      for ( int i = imin; i < imax; i++ )
      {
         double dx = i - xc;
         for ( int j = jmin; j < jmax; j++ )
         {
            double dy = j - yc;
            double r = sqrt( dx * dx + dy * dy );
            double theta = atan2( dy, dx ) * 180.0 / PI;   if ( theta < 0 ) theta += 360;
            if ( r >= r1 && r <= r2 && theta >= a1 && theta <= a2 ) canvas[height-j-1][i] = c;
         }
      }
   }

   void draw() { for ( const string &row : canvas ) cout << row << '\n'; }
};


int main()
{
   Picture picture( 51, 51 );
   picture.arc( 25, 25, 0, 24,   0, 360, '*' );
   picture.arc( 15, 32, 0,  7,   0, 360, ' ' );
   picture.arc( 35, 32, 0,  7,   0, 360, ' ' );
   picture.arc( 25, 25,15, 17, 210, 330, ' ' );
   picture.draw();
}



                                                   
                         *                         
                   *************                   
                *******************                
              ***********************              
            ***************************            
           *****************************           
          *******************************          
         *********************************         
        ***********************************        
       *************************************       
      ********* ******************* *********      
     *******       *************       *******     
     ******         ***********         ******     
    ******           *********           ******    
    *****             *******             *****    
   ******             *******             ******   
   ******             *******             ******   
   *****               *****               *****   
  *******             *******             *******  
  *******             *******             *******  
  *******             *******             *******  
  ********           *********           ********  
  *********         ***********         *********  
  **********       *************       **********  
 ************** ******************* ************** 
  ***********************************************  
  ***********************************************  
  ***********************************************  
  ***********************************************  
  ***********************************************  
  ***********************************************  
   *********************************************   
   ********* ************************* *********   
   ********   ***********************   ********   
    ********  ***********************  ********    
    *********  *********************  *********    
     ********    *****************    ********     
     **********   ***************   **********     
      **********    ***********    **********      
       **********                 **********       
        ************           ************        
         **************** ****************         
          *******************************          
           *****************************           
            ***************************            
              ***********************              
                *******************                
                   *************                   
                         *       





If you've just been told off by @JLBorges then you can make the last arc statement
picture.arc( 25, 0,15, 17, 30, 150, ' ' );
Then you get
                                                   
                         *                         
                   *************                   
                *******************                
              ***********************              
            ***************************            
           *****************************           
          *******************************          
         *********************************         
        ***********************************        
       *************************************       
      ********* ******************* *********      
     *******       *************       *******     
     ******         ***********         ******     
    ******           *********           ******    
    *****             *******             *****    
   ******             *******             ******   
   ******             *******             ******   
   *****               *****               *****   
  *******             *******             *******  
  *******             *******             *******  
  *******             *******             *******  
  ********           *********           ********  
  *********         ***********         *********  
  **********       *************       **********  
 ************** ******************* ************** 
  ***********************************************  
  ***********************************************  
  ***********************************************  
  ***********************************************  
  ***********************************************  
  ***********************************************  
   *********************************************   
   ********************** **********************   
   *****************           *****************   
    *************                 *************    
    ************    ***********    ************    
     **********   ***************   **********     
     ********    *****************    ********     
      *******  *********************  *******      
       *****  ***********************  *****       
        ***   ***********************   ***        
         *** ************************* ***         
          *******************************          
           *****************************           
            ***************************            
              ***********************              
                *******************                
                   *************                   
                         *                         

Last edited on
Touché
@lastchance, that's brilliant
Topic archived. No new replies allowed.