a

aaa
Last edited on
1
2
3
4
5
6
7
bool validateNumRow(int RowAmount){

  if(RowAmount > 0 && RowAmount < 20)
       return true;

  return false;
}
I put that in the function definition but how do I made RowAmount that the user inputs go thru the function.

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

bool validateNumRow(int RowAmount);

int main()
{
        int RowAmount;
        cout << "Please enter an ODD number in range 1-19: ";
        cin >> RowAmount;
        return 0;
}

bool validateNumRow(int RowAmount)

{
        if (RowAmount > 0 && RowAmount < 20)
                return true;
        return false;
}
Do you mean how to call the function and pass RowAmount?

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
#include <iostream>
using namespace std;

bool validateNumRow(int RowAmount);

int main()
{
	int RowAmount;
	cout << "Please enter an ODD number in range 1-19: ";
	cin >> RowAmount;
	if (validateNumRow(RowAmount))
	{
		cout << "num good" << endl;
	}
	else
	{
		cout << "num no good" << endl;
	}
	return 0;
}

bool validateNumRow(int RowAmount)

{
	if (RowAmount > 0 && RowAmount < 20)
		return true;
	return false;
}
Pass the the input RowAmount into the function. If the bool returns false, re-prompt user to input another number.
aaa
Last edited on
You’d better not to take this code to your teacher without cleaning off comments.
It will takes two posts.

First part:
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
#include <iostream>

// ------------------------------------------------------------------------
// You will validate the user’s input by creating a programmer-defined
// function to ensure that the integer is an odd integer
// in the range 1 to 19, inclusively.
//    --> checkOddNum()
// The integer entered by the user should be passed
// as a parameter to this function.
// You may assume that the user enters an integer...
//    --> checkOddNum(int num_to_check)
// The return type of this function should be a boolean data type...
//    --> bool checkOddNNum(int num_to_check)
// ------------------------------------------------------------------------
bool checkOddNum(int num_to_check);

// ------------------------------------------------------------------------
// ...you will calculate and return the sum of the integers from 1
// to the integer entered by the user using another programmer-defined
// function.
//    --> calculateSumOfIntegers()
// Again, the integer entered by the user should be passed as a parameter
// to this function.
//    --> calculateSumOfIntegers(int upper_boundary)
// The return type of this function should be an integer data type...
//    --> int calculateSumOfIntegers(int upper_boundary)
// ------------------------------------------------------------------------
int calculateSumOfIntegers(int upper_boundary);

// ------------------------------------------------------------------------
// ...draw the diamond using a programmer-defined function
//       --> drawDiamond()
// that accepts both the integer and the printable character entered
// by the user as parameters. The integer entered by the user will specify
// the number of rows in the diamond.
//       --> drawDiamond(int rows, char symbol)
// This function should be a void- function that does not return a value.
//       --> void drawDiamond(int rows, char symbol)
// ------------------------------------------------------------------------
void drawDiamond(int rows, char symbol);

Second part:
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
int main()
{
   // ------------------------------------------------------------------------
   // You will first prompt the user to enter an odd integer
   // between 1 and 19, inclusively.
   // ------------------------------------------------------------------------
   std::cout << "Please enter an odd interger number between 1 and 19: " ;
   int odd_num;
   std::cin >> odd_num;

   // ------------------------------------------------------------------------
   // If the number is not valid, you will display a meaningful
   // error message before re-prompting the user to enter
   // the integer again.
   // The return type of this function should be a boolean data type
   // and you are to use this boolean result in determining whether
   // or not the user input is valid.
   // ------------------------------------------------------------------------
   // Logic: while(condition) will loop until condition is true;
   // if checkOddNum() returns false, while would not run again;
   // but we need the opposite, i.e. that while asks for a number
   // until an even ("not odd") is provided, and exit when the user
   // inputs an odd number.
   // So we need to say: while the check for "oddity" returns false:
   while(!checkOddNum(odd_num)) {
      std::cout << "Your number was either even or out of the range 1-19"
                << std::endl;
      std::cout << "Please enter an odd interger number between 1 and 19: " ;
      std::cin >> odd_num;
   }

   // ------------------------------------------------------------------------
   //• Once validated, you will calculate and return the sum of
   // the integers from 1 to the integer entered by the user
   // using another programmer-defined function...
   // ...you are to use this integer result and print it to the screen
   // in a meaningful message.
   std::cout << "The sum of all the integers between 1 up to "
             << odd_num << " is " << calculateSumOfIntegers(odd_num)
             << std::endl << std::endl;

   //• You will then prompt the user for and read in a printable character
   // that will be used to draw the diamond. You may assume that the user
   // enters a printable character.
   std::cout << "Now, would you mind entering a printable character, please? ";
   char print_char;
   std::cin >> print_char;

   //• Finally, you will draw the diamond...
   // ...It should print a diamond of the appropriate size using
   // the printable character entered by the user.
   // See the sample program --> SAMPLE MISSING!!!
   drawDiamond(odd_num, print_char);

   return 0;
}

bool checkOddNum(int num_to_check)
{
   // Logic: if you try to divide something by 2 and there is not a rest,
   // then the number is even.
   // To check the rest of a division you can use the modulo division "%",
   // which yields the rest of division.
   // If the check fails, you can already return false, because the number
   // is not odd.
   if( (num_to_check % 2) == 0 )
      return false;

   // Ok, if we get here, the number is even.
   // Now, let's check if it's in the range 1-19 included:
   if(num_to_check < 1 || 19 < num_to_check)
      return false;

   // num_to_check passed all the exams...
   return true;
}

int calculateSumOfIntegers(int upper_boundary)
{
   // Logic: we need all trhe integers between 1 up to the number provided
   // by the user.
   //     --> for(int i = 1; i <= upper_boundary; i++)
   // As the numbers go along, we need to sum them; therefore we need
   // another integer variable which stores the result of the sum:
   //    --> int result
   // "result" must be declared outside the for-loop, otherwise it would
   // die at the end of the loop.
   int result = 0;
   for(int i=1; i<=upper_boundary; i++) {
      result += i;
   }

   return result;
}

void drawDiamond(int rows, char symbol)
{
   // ------------------------------------------------------------------------
   // You may only use cout statements that print a single character
   // (i.e., that passed in by the user), a single space, or
   // a single new-line character (such as ‘\n’ or endl).
   // Maximize your use of repetition with nested for loops and
   // minimize the number of cout statements.
   // It should print a diamond of the appropriate size using
   // the printable character entered by the user.
   // See the sample program run for an example of what should be output.
   // ------------------------------------------------------------------------
   // You did not provide the example and this is a problem. What type of
   // shape is required? Only the borders:
   //     *
   //    * *
   //   *   *
   //  *     *
   // *       *
   //  *     *
   //   *   *
   //    * *
   //     *
   // Or a filled shape?
   //     *
   //    ***
   //   *****
   //  *******
   // *********
   //  *******
   //   *****
   //    ***
   //     *
   // Let's assume it's the second. In this case, you could consider this (I'm
   // going to use hyphens to represent spaces):
   // One row diamond (upper part):
   // *              line 1 - spaces=0 asterisks=1
   // Two rows diamond (upper part):
   // -*             line 1 - spaces=1 asterisks=1
   // ***            line 2 - spaces=0 asterisks=3
   // Three rows diamond (upper part):
   // --*            line 1 - spaces=2 asterisks=1
   // -***           line 2 - spaces=1 asterisks=3
   // *****          line 3 - spaces=0 asterisks=5
   // Four rows diamond (upper part):
   // ---*           line 1 - spaces=3 asterisks=1
   // --***          line 2 - spaces=2 asterisks=3
   // -*****         line 3 - spaces=1 asterisks=5
   // *******        line 4 - spaces=0 asterisks=7
   // Five rows diamond (upper part):
   // ----*          line 1 - spaces=4 asterisks=1
   // ---***         line 2 - spaces=3 asterisks=3
   // --*****        line 3 - spaces=2 asterisks=5
   // -*******       line 4 - spaces=1 asterisks=7
   // *********      line 5 - spaces=0 asterisks=9
   // ...end so on.
   // To print the lower part, you just need to reverse the code.

   // Printing diamond procedure
   // - number of rows --> given by the user
   // - maximum number of spaces needed --> rows - 1 (because the number of
   //       characters in every row will be the equal to the row, but at least
   //       one of those character must be the symbol)
   int spaces = rows - 1;
   // Upper part
   // We are going to print as many rows as requested (it means that the
   // largest row, the central, will be printed by this part of the
   // procedure)
   for(int this_row=1; this_row <= rows; this_row++) {
      // The following two loops will repeat for every row
      // 1) firstly we need the spaces
      for(int i=1; i<=spaces; i++)
         std::cout << ' ';

      // 2) then we can print the characters:
      for(int i=1; i <= 2*this_row-1; i++)
         std::cout << symbol;

      // Now we can start a new line
      std::cout << std::endl;

      // but we will need 1 space less:
      spaces--;
   }
   // Lower part
   spaces = 1;
   for(int this_row = 1; this_row < rows; this_row++) {
      // Spaces will grow row by row
      for(int i=1; i <= spaces; i++)
         std::cout << ' ';

      // Symbols will decrease row by row
      for(int i=1 ; i <= 2*(rows-this_row)-1; i++)
         std::cout << symbol;

      std::cout << std::endl;
      spaces++;
   }
}
You can find more than one solution for the "print diamond" part.
Here's a different one:
http://www.cplusplus.com/forum/beginner/14955/
Topic archived. No new replies allowed.