Help Please?

Write the following 3 functions.

a) A function the prints three asterisks with three spaces in between each, starting in the first column:
* * *
Do not print a new-line in this function.

b) A function that prints the same three asterisks with three spaces between each, but with two blanks (spaces) at the beginning of the line.

c) A function that prints a new-line (and nothing else).

Using only calls to these three functions in the main program, print the following 15-line design:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
* * *
* * *
* * *

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

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




So I am absolutely horrible at functions...
I was hoping that somebody could walk me through this, but please don't just give me the code. I would like to learn from this and be walked through the steps of creating this program. Thank you so much to anybody that is willing to help!

This is all I have at this point. Please somebody tell me what I've done wrong and then the next step. I really don't understand this...

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
  //CSC150 Lab8
//Ian Heinze
//10/28/2015

#include <iostream>

using namespace std;


void function1()
{
    std::cout << "*   *   *";
}
void function2()
{
    std::cout << "  *   *   *";
}
void function3()
{
    std::cout << endl;
}


int main()
{
    function1();
}
{
    function2();
}
{
    function3();
}

This will straighten out what you have...


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
 //CSC150 Lab8
//Ian Heinze
//10/28/2015

#include <iostream>

using namespace std;


void function1()
{
    std::cout << "*   *   *";
}
void function2()
{
    std::cout << "  *   *   *";
}
void function3()
{
    std::cout << endl;
}


int main()
{
    function1();
    function2();
    function3();
}



For this...
* * * 1 and 3
* * * 1 and 3
* * * 1 and 3
3
* * * 1 and 3
* * * 2 and 3
* * * 1 and 3
* * * 2 and 3
* * * 1 and 3
2
* * * * * * 1 , 1 , and 3
* * * 2 and 3
* * * * * * 2 , 1 , and 3
* * * 2 and 3
* * * * * * 1 and 1


Last edited on
First OP, your main function is broken down with curly brackets and nothing will run without each function being in main plus you don't have a return statement within main.

To correct all of this here:

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
//CSC150 Lab8
//Ian Heinze
//10/28/2015

#include <iostream>

using namespace std;


void function1()
{
    std::cout << "*   *   *";
}
void function2()
{
    std::cout << "  *   *   *";
}
void function3()
{
    std::cout << endl;
}


int main()
{
    function1();

    function2();

    function3();
    return 0;
}


Now from here repetiition is your best friend.
Okay, so do I just need to put it into the form like

function1
function1
function1
function3
function1
function2
function1
function2
function1
function3
function1, function1
function2
function2, function1
function2
function1, function1

Hopefully that makes sense?...
Right now when I run the code the result I get is,

* * * * * *

Edit--I think I'm starting to get it actually!
Last edited on
function1
function1
function1
function3
function1
function2
function1
function2
function1
function3
function1, function1
function2
function2, function1
function2
function1, function1


outcome:

* * ** * ** * *
* * * * * ** * * * * ** * *
* * ** * * * * * * * ** * * * * ** * ** * *
In a way.

take what you said here:
function1
function1
function1


and ask yourself what can I do to get what I just said. IN ONE STEP.

instead of calling 3 functions how can I in that ONE function repeat * * *

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//CSC150 Lab8
//Ian Heinze
//10/28/2015

#include <iostream>

using namespace std;


void function1()
{
    std::cout << "*   *   *  ";
}
void function2()
{
    std::cout << "  *   *   *  ";
}
void function3()
{
    std::cout << endl;
}


int main()
{
    function1();

    function3();

    function1();

    function3();

    function1();

    function3();

    function3();

    function1();

    function3();

    function2();

    function3();

    function1();

    function3();

    function2();

    function3();

    function1();

    function3();

    function3();

    function1();

    function1();

    function3();

    function2();

    function3();

    function2();

    function1();

    function3();

    function2();

    function3();

    function1();

    function1();


    return 0;
}



Oops, wish I would have checked back here before doing this...I'm assuming I should have just used for loops?
If I wanted to simplify this code using for loops, how exactly would i use them inside of the function call? The same as always using a for loop?

But also my assignment is to build those designs using only function calls. If I used used for loops, would that be breaking that parameter of the assignment?
Last edited on
I believe your instructions said using ONLY function calls...

BTW... Function1, 2 and 3,,, are really BAD names for functions.
If I wanted to simplify this code using for loops, how exactly would i use them inside of the function call?

The easiest way would be to pass the number of desired repetitions to each function.

10
11
12
13
14
void function1(int nrep)
{  for (int i=0; i<nrep; i++)
        std::cout << "*   *   *  ";
}
// Do the same for function2 and function3. 


But also my assignment is to build those designs using only function calls. If I used used for loops, would that be breaking that parameter of the assignment?

If you were told not to use loops, then yes.



Last edited on
closed account (E0p9LyTq)
mrlowkey941 wrote:
and ask yourself what can I do to get what I just said. IN ONE STEP.

instead of calling 3 functions how can I in that ONE function repeat * * *


Reread the OP's program requirements:

ianheinze wrote:
Write the following 3 functions.

a) A function the prints three asterisks with three spaces in between each, starting in the first column:
* * *
Do not print a new-line in this function.

b) A function that prints the same three asterisks with three spaces between each, but with two blanks (spaces) at the beginning of the line.

c) A function that prints a new-line (and nothing else).

Using only calls to these three functions in the main program, print the following 15-line design:


Yes, all those function calls are messy, but that is the requirements the OP was given.

@ianheinze,

Consider combining multiple function calls that produce a single line of output to a single line of source. It won't change the output, but make it easier to follow the logic IMO.

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>

void function1()
{
   std::cout << "*   *   *  ";
}
void function2()
{
   std::cout << "  *   *   *  ";
}
void function3()
{
   std::cout << std::endl;
}


int main()
{
   function1(); function3();

   function1(); function3();

   function1(); function3();

   function3();

   function1(); function3();

   function2(); function3();

   function1(); function3();

   function2(); function3();

   function1(); function3();

   function3();

   function1(); function1(); function3();

   function2(); function3();

   function2(); function1(); function3();

   function2(); function3();

   function1(); function1();

   return 0;
}


I am a big proponent of making source easier to read. Makes for easier maintenance and debugging.
closed account (E0p9LyTq)
ianheinze wrote:
If I wanted to simplify this code using for loops, how exactly would i use them inside of the function call? The same as always using a for loop?


If I were to rewrite the program so it used loops I would consider doing the following:

1. create an array (or vector) that stored the pattern to call each of the 3 functions

2. loop through the array and use a switch to determine which function to call

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
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

void function1()
{
   std::cout << "*   *   *  ";
}
void function2()
{
   std::cout << "  *   *   *  ";
}
void function3()
{
   std::cout << std::endl;
}

int main()
{
    // messy, but you can read the pattern for each output line
    int pattern[] = {1, 3,
                   1, 3, 
                   1, 3, 
                   3, 
                   1, 3, 
                   2, 3, 
                   1, 3, 
                   2, 3, 
                   1, 3, 
                   3, 
                   1, 1, 3, 
                   2, 3, 
                   2, 1, 3, 
                   2, 3, 
                   1, 1};
   
   // get the number of elements in the array
   // if you used a C++ container such as vector no need to determine the size
   int pattern_size = sizeof(pattern) / sizeof(pattern[0]);
   
   for (int i = 0; i < pattern_size; i++)
   {
      switch (pattern[i])
      {
      case 1:
         function1();
         break;

      case 2:
         function2();
         break;

      case 3:
         function3();
         break;

      default:
         std::cout << "ERROR!" << std::endl;
      }
   }

   return 0;
}


If I thought about it I am sure I could come up with better ways to do this, this is a quick and dirty rewrite.

Staying within the parameters of the assignment...

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

void function1(bool);
void function2(bool);
void function3();

int main()
{
   function1(1);
   function1(1);
   function1(1);
   function3();
   function1(1);
   function2(1);
   function1(1);
   function2(1);
   function1(1);
   function3();
   function1(0); function1(1);
   function2(1);
   function2(0); function1(1);
   function2(0);
   function1(0); function1(0);

   return 0;
}


void function1(bool ret)
{
   std::cout << "*   *   *  ";
   if (ret==true){
    function3();
   }
}

void function2(bool ret)
{
   std::cout << "  *   *   *  ";
   if (ret==true){
    function3();
   }
}
void function3()
{
   std::cout << std::endl;
}
Topic archived. No new replies allowed.