Computer Science 1511
Computer Science I

Laboratory Assignment 3
Formatted Output (5 points)
Due at the end of Lab

Introduction

This lab will familiarize you with formatting your output so that when it is printed to the screen, it appears readable and clear to the user. In this lab you will learn how to format variables of integer and floating-point types. Formatting a character variable is left to your self study. In this lab formatting will be done with the printf function. To print formatted data printf needs two things: instructions to format the data and the actual data to be printed. A printf statement is of the form:

      printf(Format_String, Data_List);

The format string is enclosed in a set of quotation marks ("...") and contains text to be output and zero or more field specifications. Each field specification (which describes the data to be printed) begins with a percent sign (%) and describes exactly one field or variable. The first specification matches the first parameter, the second matches the second parameter, and so on.

Integer formatting:

For printing integers, the field specification is "%d". A width modifier can be used to specify the minimum number of positions the integer will occupy. A width modifier is an integer that goes between the % and the d in the field specification. If the data needs more positions than given in the width, the printf will override the width.

For example, if 'num' has a value 155,

      printf("Number=%5d",num);
will print
      Number=__155
Note the 2 spaces - here a '_' is used to denote leading spaces.

Floating-point formatting:

For printing floating-point numbers, we use the field specification "%f". A floating point number can also have a width modifier, and it can have a precision modifier. A precision modifier appears after a width modifier and consists of a period (.) and a number. The precision modifier indicates how many characters appear after the decimal point. For example, %6.2f is a floating point specification that indicates that 6 total characters should be used and 2 appear after the decimal point. Note that the decimal point and characters after the decimal point count against the width.

For example, if 'num' has the value 15.5,

      printf("Number=%8.3f",num);
will print
      Number=__15.500
Note the 2 spaces in front and added 0's after the decimal point.

The Program

/*
 * Your name
 * CS 1511 Section ?
 * Today's date
 * Lab #3
 */

#include <stdio.h>

void main()
{
	int cschair, sbechair;
	int csdesk, sbedesk;
	int i = 0;
	float chcost, decost;
	float cscost, sbecost;

	/* Input from user */

	printf("\nORDER FORM FOR OFFICE FURNITURE FOR UMD LABS\n");
	printf("\nEach chair costs $25 and each desk costs $40\n\n");
	printf("Enter the number of chairs and desks required for CS lab: ");
	scanf("%d %d", &cschair, &csdesk);

	printf("Enter the number of chairs and desks required for SBE lab: ");
	scanf("%d %d", &sbechair, &sbedesk);
	
	chcost = 25.0 * (cschair + sbechair);
	decost = 40.0 * (csdesk + sbedesk);
	cscost = 25.0*cschair + 40.0*csdesk;
	sbecost = 25.0*sbechair + 40.0*sbedesk;

	/* Printing out the table */

	printf("\n\nLab    Chairs  Chair cost  Desks  Desk cost  Total for lab\n");
	printf("-----  ------  ----------  -----  ---------  -------------\n");

	/* CHECK */

	printf("CS     %d  $%9.2f  %5d  $%8.2f  $%12.2f\n", cschair, 25.0*cschair, csdesk, 40.0*csdesk,
	cscost);	

	/* CHECK */

	printf("SBE    %6d  $%9.2f  %5d  $%8.2f  $%f\n", sbechair, 25.0*sbechair, sbedesk, 40.0*sbedesk, 
	sbecost);

	for(i=0;i<58;i++)
		printf("-");

	printf("\n");
	
	/* CHECK */

	printf("Total  %6d  $%9.2f  %5d  $%f  $%12.2f\n", cschair+sbechair, chcost, csdesk+sbedesk, 
	decost, chcost+decost);

	for(i=0;i<58;i++)
		printf("-");

        printf("\n");
        fflush(stdin);	
	getchar();	
}

Initial corrections

When the table is initially printed, you will notice that there are some bugs. There are 2 places in the table that do not align properly.

You can correct the program as follows:

Centering Some Columns

Lets assume that the number of chairs and desks is less than 100 (99 or less). In this case it might look better for the number of chairs and desks to be centered under the Chairs and Desks column (rather than be aligned to the right in these columns). Try to change the number of spaces and the width modifiers of these columns so that these columns are centered.
Also try to modify the other columns so that they are left justified instead of right.

For example,

      printf("Number=%-5d",100);
will print 100 left justified, i.e.
 100__ 

Appropriate Data

After you have made the above changes, try to see what happens if you now enter 10000 chairs and 20000 desks for the CS lab. You will notice that the resulting table is not aligned. It would be useful if you told the user what are reasonable values to enter. For example, when accepting the input from the user, mention that you can order a maximum of 99 of each item:

	printf("\nEach chair costs $25 and each desk costs $40");
	printf("\nMaximum of 99 chairs and 99 tables allowed\n\n");

What To Turn In

Turn in hard copies of the source code after all the corrections you have made. You will also need to turn in the output of the initial program, the output after the changes, and the output if you enter very large numbers. Give a brief explanation as to why the table gets disarranged when a large number of chairs or tables are ordered. Be sure to specify your name, section number and lab number when you submit the lab assignment.