Pages

Of Computers, troubles and my Experiments with programming as a beginner.

Wednesday, May 14, 2008

004: Book1,Chapter 1,Program 4

Problem Statement:
1.4 Write a program that will print the following figure using suitable characters
---- ----
>>-------->
---- ----

Solution:
Yet another trick where you have to know to align characters. That's all. I'll be honest and tell you that, I achieved it using trial and error. I mean, no matter how well you count, the alignment screws up on it's own. :) Nothing better than running the program.

Program:
/*Program 1.1.4 */
#include stdio.h
/*This is a header file.*/
#include conio.h
/*This is another header file that we require for using getch()*/

int main()
{
printf("\n ---- ---- ");
printf("\n >>--------> ");
printf("\n ---- ---- ");
getch();
return 0;
}

Output:
---- ----
>>-------->
---- ----

Description:
Quite Simple again. Not much to explain. Do read up on 001 through 003 to get to know the previous programs.

printf()

Sunday, May 11, 2008

003:Book 1,Chapter 1,Program 3

Problem Statement:
1.3 Write a program using one printf statement to print the patter on asterisks as shown below
*
**
***

Solution:
Yet anoter program that tests to see if you have gotten your newline character memorised. :D

Program:
/*Program 1.1.3 */
#include stdio.h
/*This is a header file.*/
#include
/*This is another header file that we require for using getch()*/

int main()
{
printf("*\n**\n***");
getch();
return 0;
}

Output:
*
**
***

Description:
Simple stuff really. I don't reckon there's much to explain.



There is a problem with headers in all of my programs. They are supposed to be typed as lessthansybolheadername.hgreaterthansymbol. That messes with the code and hence the Less than and greater than symbol do not display. :(

Thursday, May 8, 2008

002:Book 1,Chapter 1,Program 2

Problem Statement:

1.2 Modify program 1.1 to provide border lines to the address

Solution:

This is just an extension of the previous program. It's a gimmick basically. All it teaches is to visualise the printf lines inside the " ".Because barring the \n - carriage return or newline, the content appears as such in the output.
Program:
/*Program 1.1.2 */
#include stdio.h
/*This is a header file.*/
#include conio.h
/*This is another header file that we require for using getch()*/

int main()
{
printf(" -----------------------");
printf("\n XYZ, ");
printf("\n 10, Downing Street, ");
printf("\n London, 123456 ");
printf("\n -----------------------");
getch();
return 0;
}
Output:
----------------------
XYZ,
10, Downing Street,
London, 123456
----------------------
Description:
You can achieve the same using a single printf statement too. But that's just left to the choice of the programmer. I have used multiple printf statements because it aids the presentation and you can visualise how the output will actually look.
To know more about these terms, click on them.
stdio.h
conio.h
getch()
/* */-Comment Lines
/n - Carriage Return

Monday, May 5, 2008

001: Book 1,Chapter 1,Problem 1

Problem statement:
1.1 Write a program that will print your mailing address in the following form:
First Line : Name
Second Line: Door No, Street
Third Line : City, Pin Code*


Solution:
All the program wants to test is if you know to use the new line character. That's all really.


Program:

/*Program 1.1.1 */
#include stdio.h
/ *This is a header file.*/
#include conio.h
/* This is another header file that we require for using getch()*/

int main()
{
printf("XYZ,\n10, Downing Street,\nLondon, 123456");
getch();
return 0;
}


Output:
XYZ,
10, Downing Street,
London, 123456

Description:
To know more about these terms, click on them.
stdio.h
conio.h
getch()
/* */-Comment Lines


*In India wa have a 6 digit Pin Code just as in USA they have a 5 digit Zip Code

Friday, May 2, 2008

Book1:Programming in ANSI C by E Balagurusamy

For convenience, I am labeling this book "Book1". Henceforth, I shall be referring to this book as Book 1. So all my posts that have the title Book1 correspond to this book. This is important because I shall be solving the Programming Exercises from each unit. To know the problem statement and to know the pre-requisites of the chapter, you are advised to know what book we are discussing.