Thread: Help with part of program

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Help with part of program

    I have two thirds of my program working, but the last part I am having a hard time figuring out.

    For the last part of the program it should output a 'plus' symbol using asterisks. The size of this is user input.

    Ex:
    Code:
      *  
      *  
    *****
      *  
      *
    Unfortunately I am somewhat lost as to how I can accomplish this.

    This is my program so far, the bottom part with the "Plus" comment is where I am having difficulty.


    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int width=0;
    int shape=0;
    
    int row_counter=0;
    int col_counter=0;
    
       printf("Enter how wide the shape is to be: ");
       scanf("%d", &width);
       
       printf("Enter:\n");
       printf(" 1) For a filled square,\n");
       printf(" 2) For a lower triangle, or,\n");
       printf(" 3) For a plus\n");
       scanf("%d", &shape);
       
    int mid= (width+1)/2;
       
      if (shape==1) //square
    {
      for (row_counter=0; row_counter<width; row_counter=row_counter+1)
      {
      
        for (col_counter=0; col_counter<width; col_counter=col_counter+1)
    	{
    		printf("*");
    	}
    	
      printf("\n");
    	
      }
        
    } 
       
       
      if (shape==2) //triangle
    {
      for (row_counter=1; row_counter<=width; row_counter=row_counter+1)
      {
        for (col_counter=1; col_counter<=row_counter; col_counter=col_counter+1)
    	{
    	   printf("*");
    	}
    	printf("\n");
      }
    } 
      
      
        if (shape==3) //plus
    {
      for (row_counter=0; row_counter<width; row_counter=row_counter+1)
      {
        for (col_counter=0; col_counter<mid; col_counter=col_counter+1)
    	{
    		printf(" ");
    	}	
    	for (col_counter=0; col_counter==mid; col_counter=col_counter+1)
    	{
    		printf("*");
    	}
    	
      }
      printf("\n");
    } 
      
    }

    Any help or hints would be greatly appreciated! =)

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have a few problems. First:
    Code:
    for (col_counter=0; col_counter==mid; col_counter=col_counter+1)
    That is the "keep going" condition. Unless mid is zero, this loop will never execute.

    Second, your printf("\n"); needs to be inside your for(row_counter...) loop.

    Third, you need to do two different things for the plus case. One is print some blanks followed by a single star, the other is print a row of just stars. Which one you do depends on which row you're printing, and the number of spaces or stars is related to mid. I suggest working this out on paper (perhaps graph paper to help track rows and columns) for several different widths, like 3, 4, 5, 6, 7, and see what the relationship is.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Okay so I worked on it for a bit and I'm getting much closer. This is what I've changed it to.

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int width=0;
    int shape=0;
    
    int row_counter=0;
    int col_counter=0;
    
       printf("Enter how wide the shape is to be: ");
       scanf("%d", &width);
       
       printf("Enter:\n");
       printf(" 1) For a filled square,\n");
       printf(" 2) For a lower triangle, or,\n");
       printf(" 3) For a plus\n");
       scanf("%d", &shape);
       
    int mid= (width+1)/2;
       
      if (shape==1) //square
    {
      for (row_counter=0; row_counter<width; row_counter=row_counter+1)
      {
      
        for (col_counter=0; col_counter<width; col_counter=col_counter+1)
    	{
    		printf("*");
    	}
    	
      printf("\n");
    	
      }
        
    } 
       
       
      if (shape==2) //triangle
    {
      for (row_counter=1; row_counter<=width; row_counter=row_counter+1)
      {
        for (col_counter=1; col_counter<=row_counter; col_counter=col_counter+1)
    	{
    	   printf("*");
    	}
    	printf("\n");
      }
    } 
      
      
        if (shape==3) //plus
    {
      for (row_counter=0; row_counter<width; row_counter=row_counter+1)
      {
        for (col_counter=0; col_counter<=mid; col_counter=col_counter+1)
    	{
    	
    		if (row_counter!=mid)
    		{
    	
    			if (col_counter != mid)
    			{
    			printf(" ");
    				}
    	
    			if (col_counter==mid)
    			{
    			printf("*");
    			}
    		}
    		else 
    		{
    		printf("*");	
    		}
    	}
      printf("\n");
      }
    
    } 
      
    }

    If anyone knows what I'm doing wrong please point it out.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It usually helps if you show us the output you're getting, but never mind for now. Here it is:
    Code:
    $ ./shape
    Enter how wide the shape is to be: 5
    Enter:
     1) For a filled square,
     2) For a lower triangle, or,
     3) For a plus
    3
       *
       *
       *
    ****
       *
    Looks like you print the right number of rows, but your cross bar and columns are off a bit. When you worked this out by hand and counted rows/columns, did you start at 0 like your loops, or did you start at 1? When did you stop counting?

    EDIT: You said main would return an int, so do so. Put a return 0; at the end.
    Last edited by anduril462; 10-13-2011 at 03:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why the control goes to else part in this program
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 07:09 AM
  2. Problem with part of program
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2006, 06:45 AM
  3. help with letter counting part of program
    By bluegoo06 in forum C++ Programming
    Replies: 7
    Last Post: 04-26-2005, 08:25 PM
  4. Replies: 3
    Last Post: 12-17-2003, 06:02 PM
  5. How do you branch to another part of the program?
    By gcn_zelda in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 12:27 PM