Program to representing a length Using a structure



#include
#include

#define INCHES_PER_FOOT 12
#define FEET_PER_YARD 3

struct Length
{
unsigned int yards;
unsigned int feet;
unsigned int inches;
};

struct Length add(struct Length first, struct Length second);
void show(struct Length length);

int main(void)
{
char answer = 'n';
struct Length length;
struct Length total = { 0,0,0};
int i = 0;
do
{
printf("Enter a length in yards, feet, and inches: ");
scanf(" %d %d %d", &length.yards, &length.feet, &length.inches);
total = add(total,length);
printf("Do you want to enter another(y or n)?: ");
scanf(" %c", &answer);
fflush(stdin);
}while(tolower(answer) == 'y');
printf("The total of all the lengths is: ");
show(total);
printf("\n");
return 0;
}

struct Length add(struct Length first, struct Length second)
{
unsigned long inches = 0;
struct Length sum;
inches = first.inches + second.inches+
INCHES_PER_FOOT*(first.feet+second.feet+FEET_PER_YARD*(first.yards+second.yards));
sum.inches = inches%INCHES_PER_FOOT;
sum.feet = inches/INCHES_PER_FOOT;
sum.yards = sum.feet/FEET_PER_YARD;
sum.feet %= FEET_PER_YARD;
return sum;
}

void show(struct Length length)
{
printf("%d yards %d feet %d inches", length.yards,length.feet, length.inches);
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!