[Courses] C Programming For Absolute Beginners, Lesson 4: Getting Looped

Carla Schroder carla at bratgrrl.com
Tue Mar 6 06:03:36 UTC 2012


Interesting...I get the same output with both versions:

$ loopybox

This is my awesome loopy program

See the value of a in each
loop in a nice table

+++++++++++
   12     + 
   16     + 
   20     + 
   24     + 
   28     + 
+++++++++++

I'll look at them more closely tomorrow. Thanks for including explanations!

Carla


On Tuesday, March 06, 2012 08:51:42 AM Sachin Divekar wrote:
> On Tue, Mar 6, 2012 at 8:42 AM, Sachin Divekar <ssd532 at gmail.com> wrote:
> > My answer to the homework.
> > 
> > // loopy.c : to demonstrate looop
> > 
> > #include <stdio.h>
> > 
> > void print_repeat(char to_print, int how_many);
> > 
> > int main ()
> > {
> > 
> >  int a = 0;
> > 
> > printf ( "\nThis is my awesome loopy program\n\n" );
> > printf ( "See the value of a in each\nloop in a nice table\n\n" );
> > //printf ( "________ \n" );
> > 
> > print_repeat('+', 10);
> > 
> > for ( a = 12; a <= 30; a = a + 4 )
> > printf("   %-6d + \n",a );
> > 
> > print_repeat('+', 10);
> > printf("\n");
> > 
> > return 0;
> > }
> > 
> > void print_repeat(char to_print, int how_many)
> > {
> > int i = 0;
> > for(i=0;i<=10;i++)
> > printf("%c", to_print);
> > 
> > printf("\n");
> > }
> > 
> > 
> > Here I have included a function to repeat a particular character. I am
> > using this
> > function to create the horizontal lines that we require to create the
> > box. In this
> > function I am using minimum field width specifier(%-6d) of printf
> > formatting
> > to achieve a straight vertical line.
> > 
> > 
> > --
> > Regards,
> > Sachin Divekar
> 
> Sorry, there is a typo in my function definition. Mistakenly I have
> specified number 10
> in the for loop condition. It should be the variable how_many. The
> corrected version
> of print_repeat.
> 
> void print_repeat(char to_print, int how_many)
> {
> int i = 0;
>  for(i=0;i<=how_many;i++)
> printf("%c", to_print);
> 
>  printf("\n");
> }
> 
> 
> --
> Regards,
> Sachin Divekar
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
ace Linux nerd
author of Linux Cookbook,
Linux Networking Cookbook,
Book of Audacity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Courses mailing list