The question is this:
a = square, b = Z, c = X
You need to input a or b or c and the size of the shape.
It is required to use function with nested loops
Imma wondering whats the problem with my first part of code
It print nothing... pleaseeee helppp
#include <iostream>
using namespace std;
void print(int a){
int size;
cin >> size;
for (int r=1; r<=size; r++){
for (int c=1; c<=size; c++){
if (r==1 || c==1 || r==size || c==size)
cout << "*";
else
cout << " ";
}
}
cout << endl;
}
int main (){
int input, size;
cin >> input;
cin >> size;
if (input == 'a')
print(size);
if you insert 'a' is char then you have the problem.
on function print you declare another int size..
is this what you really wanted to do? otherwise that command line is useless since you are passing 'size' through the function parameter
1 2 3 4 5 6 7 8 9 10
int main (){
char input ; // change from int to char
int size ;
cin >> input;
cin >> size;
if (input == 'a')
print(size);
return 0;
}
a
6
7
******** ** ** ** ** ********
Program ended with exit code: 0
@ar2007
Thank you so much for ur reply
I get the part changing int to char, i know i declare int size twice but if i didnt in the function part,
It didnt read the size... what can i do?
The programme requires
E.g. you need to input e.g. "a" (the square) and 4 (the size)
Then output
****
* *
* *
****
the parameter 'a', is the copy of 'size' when you call the 'print (size)' function inside the 'main' function..
then a == size. ( then the declaration 'int size' on the void function 'print' is useless ).
#include <iostream>
usingnamespace std;
void print( int a )
{
for ( int r = 1; r <= a; r++ )
{
for ( int c = 1; c <= a; c++)
{
if ( r == 1 || c == 1 || r == a || c == a )
cout << "* ";
else
cout << " ";
}
cout << endl;
}
}
int main (){
char input ;
int size ;
cin >> input; // don't understand this but is your code
cin >> size;
if ( input == 'a' )
print(size);
return 0;
}
a
6
* * * * * *
* *
* *
* *
* *
* * * * * *
reading your problem, I think you have misinterpreted the role of square ...
I think you should have inserted int a = 49, then int size = sqrt (49).
therefore 'a' is not the name of the square, but the value of the surface of the square, from which to derive the value of the side ...........
in this case your code becomes:
#include <iostream>
#include <math.h> // sqrt() function
usingnamespace std;
void print( int a )
{
for ( int r = 1; r <= a; r++ )
{
for ( int c = 1; c <= a; c++)
{
if ( r == 1 || c == 1 || r == a || c == a )
cout << "* ";
else
cout << " ";
}
cout << endl;
}
}
int main ()
{
int square = 0 ;
int size = 0 ;
cin >> square ;
size = sqrt( square ) ;
print( size ) ;
return 0;
}