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

Sachin Divekar ssd532 at gmail.com
Tue Mar 6 03:12:59 UTC 2012


> HOMEWORK
>
> Modify the last example to display two columns of output that use the 'a'
> and
> 'total' variables.  Play around with spacing and line breaks.
>
> Make a box around your program, like
>
> +++++++++++++++++++++++++++++
> +This is my awesome loopy program  +
> +
>  +
> + See the value of a in each                    +
> + loop in a nice table                                  +
> +________                                                       +
> +   12                                                                  +
> +  16                                                                   +
> +   20                                                                  +
> +   24                                                                  +
> +   28                                                                  +
> ++++++++++++++++++++++++++++++
>
>


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


More information about the Courses mailing list