Learn How to Program in C

by
Dr. Kristin Switala

Introduction

Directives

Variables

Conditionals

Loops

Arrays

Pointers

Strings

Functions

Structures


Site Map

Review: Loops (d)

Instructions: Think about each question and write the answer in your notebook.


White Box Testing

1. Fix the errors in this C source code. There are 2 errors.

#include <stdio.h>
void main( )
{
int loop = 0;
char finished;
int distance
float gas = 0.0f;
float cost = 0.0f;
printf("\nThis program calculates how expensive gas would be for a trip.");
printf("\nHow much does gas cost (per gallon or litre) in your area? ");
scanf("%f", &gas);
for( ; ; )
{

printf("\nHow many miles/kilometers do you want to travel? ");
scanf("%d", &distance);
cost = distance * gas;
printf("\nThat trip would cost %.2f in gas.", cost);
++loop;
printf("\nWould you like to try again (y or n)? ");
scanf(" %c", &finished);
if(finished = 'n')
break;
}
}

2. Fix the errors in this C source code. There are 2 errors.

#include <stdio.h>
void main( )
{
int earth;
float moon = 0.0f;
int outer = 1;
int inner = 1;
printf("\nThe program compares the weights of objects on Earth and on the Moon.");
printf("\nEnter an Earth weight here: ");
scanf("%d", &earth);
printf("\nEarth \t Moon");
for(outer = 1; outer <= earth; outer++)
{

for(inner = 1; inner <= earth; inner++)
moon = outer * .1666;
printf("\n%d \t %.2", outer, moon);
}
}

Copyright
© 2001
Kristin Switala
Hosted by www.Geocities.ws

1