Home     Mosaic     CLIP     Octave     R Project     Processing     Contact  
Search
<http://cnfolio.com/IntroComputingPractical05>
Introduction to Computing – B142L

B142L Computing practical – Standard input and output

Academic year 2009/10


Learning outcomes

  1. Use common functions associate with line oriented text input and output.
  2. Practice using pseudo code and D structures to document program design.
  3. Practice using the scanf() function to obtain input.



Important reference documentation about standard output concepts and functions:



Important reference documentation about standard input concepts and functions:



An important learning outcome of the unit is the ability to read and use technical documentation. Students are required to read and be familiar with the reference documentation listed in the 2 sections above.



Exercise 1: Multiplication tables


Write a C program to display multiplication tables from 1 to 20 in columns which are justified and aligned in straight lines.

Following is a sample output display of multiplication tables for 1 and 10.
Tables:
 1 x  1 =   1
 1 x  2 =   2
 1 x  3 =   3
 1 x  4 =   4
 1 x  5 =   5
 1 x  6 =   6
 1 x  7 =   7
 1 x  8 =   8
 1 x  9 =   9
 1 x 10 =  10
10 x  1 =  10
10 x  2 =  20
10 x  3 =  30
10 x  4 =  40
10 x  5 =  50
10 x  6 =  60
10 x  7 =  70
10 x  8 =  80
10 x  9 =  90
10 x 10 = 100

#include <stdio.h>

int main( void )
{
   int inside, outside;
   
   for ( outside = 1; outside <= 20; outside++ )
   {
      for ( inside = 1; inside <= 10; inside++ )
      {
          printf( "%2d x %2d = %3d \n", outside, inside, outside * inside );
      }
   }
}



Exercise 2: Distance conversion


Write a C program that accepts a minimum and a maximum value in miles. Then the program displays the list of miles and converted kilometer values starting at the minimum and increasing by 5 miles each step until the maximum value is reached.

The conversion formulas are available at Wikipedia:

Following is a sample output display when the minimum value is 100 miles and the maximum value is 120 miles.
Miles     Kilometers
100       160.93
105       168.98
110       177.03
115       185.07
120       193.12

#include <stdio.h>

int main( void )
{
   int minMiles, maxMiles;
   
   scanf( "%d %d", &minMiles, &maxMiles );
   
   printf( "%-12s %-10s \n", "Miles", "Kilometers" );
   for ( ; minMiles <= maxMiles; minMiles += 5 )
   {
      printf( "%-12d %-10.2f \n", minMiles, minMiles * 1.609344 );
   }
}



Exercise 3: Temperature conversion


Write a C program that accepts a minimum and a maximum value in Celsius. Then the program displays the list of Celsius temperature and converted Fahrenheit values starting at the minimum and increasing by 0.5 Celsius degree each step until the maximum value is reached.

The conversion formulas are available at Wikipedia:

Following is a sample output display when the minimum value is 10 Celsius and the maximum value is 15 Celsius.
Celsius     Fahrenheit
10.0        50.0
10.5        50.9
11.0        51.8
11.5        52.7
12.0        53.6
12.5        54.5
13.0        55.4
13.5        56.3
14.0        57.2
14.5        58.1
15.0        59.0

#include <stdio.h>

int main( void )
{
   double minCelsius, maxCelsius;
   
   scanf( "%lf %lf", &minCelsius, &maxCelsius );
   
   printf( "%-12s %-10s \n", "Celsius", "Fahrenheit" );
   for ( ; minCelsius <= maxCelsius; minCelsius += 0.5 )
   {
      printf( "%-12.1lf %-10.1lf \n", minCelsius, ( minCelsius * (9.0 / 5.0 ) ) + 32.0 );
   }
}