I have created a program which prints a square of specific dimensions and uses a particular character,based on user's input.My goal is to print multiple copies of this shape(the user will determine the number of copies each time),each next to each other.I tried using a for loop in main,but the copies are printed vertically.I also thought of printing the first line of the shape,the second one etc, but I don't know how to apply this to my code. Any help?
#include <stdio.h>
int getsize(void);
char get_char(void);
void printsquare(int size,char ch);
int main(){
int size;
char ch;
size=getsize();
ch=get_char();
printsquare(size,ch);
}
int getsize(void){
int size;
printf("Enter the size of selected shape: ");
scanf("%d",&size);
return size ;
}
char get_char(void){
char character;
printf("Enter the character of selected shape: ");
scanf("\n%c",&character);
return character;
}
void printsquare(int size,char ch){
int i,j;
for (i=1;i<=size;i ){
for (j=1;j<i;j ){
printf("-");
}
if (i==1 || i==size){
for (j=1;j<=size;j ){
printf("%c",ch);
}
}
else{
printf("%c",ch);
for (j=1;j<=size-2;j ){
printf("-");
}
printf("%c",ch);
}
printf("\n");
}
return ;
}
CodePudding user response:
remember always try think simple, the following code show how must printsquare
must be
void printsquare(int size,char ch){
int i;
int j;
// print first line
for (i= 0; i < size; i ) {
putchar(ch);
}
putchar('\n');
// print inner line
for (i= 0; i < size; i ) {
putchar(ch);
for (j = 1; j < size - 1; j ) {
putchar('-');
}
putchar(ch);
putchar('\n');
}
// print last line
// print first line
for (i= 0; i < size; i ) {
putchar(ch);
}
putchar('\n');
}
another implementation
void printsquare(int size,char ch){
int i;
int j;
for (i= 0; i < size; i ) {
if (i == 0 || i == size - 1) {
// print last and first line
for (j = 0; j < size; j ) {
putchar(ch);
}
putchar('\n');
}
else {
// print inner lines
putchar(ch);
for (j = 1; j < size - 1; j ) {
putchar('-');
}
putchar(ch);
putchar('\n');
}
}
}
now for multiple copies
void printsquare(int size,char ch, int copies){
int i;
int j;
int c;
for (i= 0; i < size; i ) {
if (i == 0 || i == size - 1) {
for (c = 0; c < copies; c ) {
// print last and first line
for (j = 0; j < size; j ) {
putchar(ch);
}
putchar(' ');
}
putchar('\n');
}
else {
for (c = 0; c < copies; c ) {
// print inner lines
putchar(ch);
for (j = 1; j < size - 1; j ) {
putchar('-');
}
putchar(ch);
putchar(' ');
}
putchar('\n');
}
}
}
CodePudding user response:
A simple solution is to repeat the entire printing logic of each line as many times as you need.
void printsquare(int size,char ch, int reps) {
int i,j;
for (i=1;i<=size;i ) {
for (int k = 0; k < reps; k ) {
for (j=1;j<i;j ){
printf("-");
}
if (i==1 || i==size){
for (j=1;j<=size;j ){
printf("%c",ch);
}
}
else{
printf("%c",ch);
for (j=1;j<=size-2;j ){
printf("-");
}
printf("%c",ch);
}
putchar(' ');
}
printf("\n");
}
return ;
}
I have added a space to differentiate the shapes, but it could be another dash, or nothing at all.
printsquare(4, '@', 2)
gives the stdout
:
@@@@ @@@@
-@--@ -@--@
--@--@ --@--@
---@@@@ ---@@@@