Thread: Using for loops

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    14

    Using for loops

    Hi. I've been trying to use for loops to create certain shapes depending on user's input, I made it work with triangles made of '*', inverse triangles and such.
    So far I'm having trouble doing this:
    It's suppose to take two inputs ranging from 7-79, if any of them divides by 3 then the frame will be in width of 2 Z's (ZZ), and the rest should be filled with the following: XOX
    OXO
    XOX
    as much as possible, what doesn't fit should be filled with spaces ' '.
    this should be the result and I seem to never get quite the same result.. any ideas?

    Using for loops-1-jpg
    Attached Images Attached Images Using for loops-1-jpg 
    Last edited by Lidor718; 04-11-2018 at 11:33 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    That's probably an even/odd problem. Your algorithm works fine for odd numbers but not for even ones. There's unfortunately no way around that because you can't center an even amount of characters.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Quote Originally Posted by GReaper View Post
    That's probably an even/odd problem. Your algorithm works fine for odd numbers but not for even ones. There's unfortunately no way around that because you can't center an even amount of characters.
    that exe output isn't mine that's what it's suppose to do , and I'm kinda lost on how to do that.. I used for loops to create the frame with a condition regarding the division of the dimension by 3..

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    We need more vague descriptions of your code. Perhaps a video of you miming it like in charades.


    Or you could just post it here in code tags.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Quote Originally Posted by john.c View Post
    We need more vague descriptions of your code. Perhaps a video of you miming it like in charades.


    Or you could just post it here in code tags.
    Thanks for replying. this is my code so far :
    Code:
    #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
    int main(){
    	int sheight = 0, swidth = 0, i1, j1, i2, j2, j3;
    	
    		while (sheight < 7 || sheight> 79){
    			printf("Please enter the square height, range 7-79:\n");
    			scanf("%d", &sheight);
    		}
    		while (swidth < 7 || swidth> 79){
    			printf("Please enter the square width, range 7-79:\n");
    			scanf("%d", &swidth);
    		}
    		//printing 1 or 2 rows of Z's depending on wether height divides by 3 or not.
    		for (i1 = 1; sheight % 3 == 0 && i1 <= 2; i1++){
    			for (j1 = 1; j1 <= swidth; j1++){
    				printf("Z");
    			}
    			printf("\n");
    		}
    		for (i1 = 1; sheight % 3 != 0 && i1 <= 1; i1++){
    			for (j1 = 1; j1 <= swidth; j1++){
    				printf("Z");
    			}
    
    
    			printf("\n");
    		}
    		//printing 1 or 2 columns of Z's depending on wether width divides by 3 or not.
    		if (swidth % 3 == 0 && sheight % 3 == 0){
    			for (i2 = 1; i2 <= (sheight - 4); i2++){
    				printf("ZZ");
    				//print spaces
    				for (j2 = 1; j2 <= (swidth - 4); j2++){ printf(" "); }
    				printf("ZZ\n");
    			}
    			//another 2 rows of Z's
    			for (i1 = 1; sheight % 3 == 0 && i1 <= 2; i1++){
    				for (j1 = 1; j1 <= swidth; j1++){
    					printf("Z");
    				}
    				printf("\n");
    
    
    			}
    		}
    		if (swidth % 3 == 0 && sheight % 3 != 0){
    			for (i2 = 1; i2 <= (sheight - 2); i2++){
    				printf("ZZ");
    				//print spaces
    				for (j2 = 1; j2 <= (swidth - 4); j2++){ printf(" "); }
    				printf("ZZ\n");
    			}
    			//another row of Z's
    			for (i1 = 1; sheight % 3 != 0 && i1 <= 1; i1++){
    				for (j1 = 1; j1 <= swidth; j1++){
    					printf("Z");
    				}
    
    
    				printf("\n");
    			}
    
    
    		}
    		if (swidth % 3 != 0 && sheight % 3 == 0){
    			for (i2 = 1; i2 <= (sheight - 4); i2++){
    				printf("Z");
    				//print spaces
    				for (j2 = 1; j2 <= (swidth - 2); j2++){ printf(" "); }
    				printf("Z\n");
    
    
    			}
    			//print another 2 rows of Z's
    			for (i1 = 1; sheight % 3 == 0 && i1 <= 2; i1++){
    				for (j1 = 1; j1 <= swidth; j1++){
    					printf("Z");
    				}
    				printf("\n");
    
    
    			}
    		}
    		if (swidth % 3 != 0 && sheight % 3 != 0){
    			for (i2 = 1; i2 <= (sheight - 2); i2++){
    				printf("Z");
    				//print spaces
    				for (j2 = 1; j2 <= (swidth - 2); j2++){ printf(" "); }
    				printf("Z\n");
    
    
    			}
    			//print another row of Z's
    			for (i1 = 1; sheight % 3 != 0 && i1 <= 1; i1++){
    				for (j1 = 1; j1 <= swidth; j1++){
    					printf("Z");
    				}
    
    
    				printf("\n");
    			}
    		}
    
    
    	return 0;
    }
    It prints the square's frame according to the given conditions (if height/width divides by 3 double sized top-bottom/ sides is printed), now I should also print the :
    XOX
    OXO
    XOX
    inside the square and also make sure that a whole one fits, and not just a part of it, if there's not enough room then the rest should have spaces

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    That can be simplified quite a bit.
    Code:
    #include <stdio.h>
    
    void print_horz(int sheight, int swidth) {
        int i;
        for (i = 0; i < swidth; i++)
            putchar('Z');
        putchar('\n');
        if (sheight % 3 == 0) {
            for (i = 0; i < swidth; i++)
                putchar('Z');
            putchar('\n');
        }
    }
    
    int main(){
        int sheight = 0, swidth = 0, i, j;
         
        while (sheight < 7 || sheight > 79){
            printf("Enter the square height, range 7-79:\n");
            scanf("%d", &sheight);
        }
    
        while (swidth < 7 || swidth > 79){
            printf("Enter the square width, range 7-79:\n");
            scanf("%d", &swidth);
        }
    
        print_horz(sheight, swidth);
    
        int h = sheight - 2;
        if (sheight % 3 == 0)
            h -= 2;
        int w = swidth - 2;
        if (swidth % 3 == 0)
            w -= 2;
    
        for (i = 0; i < h; i++) {
            
            putchar('Z');
            if (swidth % 3 == 0)
                putchar('Z');
    
            for (j = 0; j < w; j++)
                putchar(' ');
    
            putchar('Z');
            if (swidth % 3 == 0)
                putchar('Z');
    
            putchar('\n');
        }
        
        print_horz(sheight, swidth);
    
        return 0;
    }
    Now replace the inner loop that prints the spaces with code to print the X's and O's.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Thing is I need to print it in XOX blocks, and if it doesn't fit the rest should be filled with spaces.. so far I just can't get it to do all at the same time..
    OXO
    XOX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programing , Loops , infinite loops issue
    By Ibrahim Lawleit in forum C++ Programming
    Replies: 15
    Last Post: 07-14-2014, 03:41 PM
  2. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  3. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  4. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  5. need a lil help with some loops
    By blindleaf in forum C Programming
    Replies: 7
    Last Post: 03-16-2003, 06:32 PM

Tags for this Thread