Circle exercise - Hold1 - ugeseddel for første lektion

Solution index                   Textual C program


/**
 CIRCLE.C   Exercise solution 1.7.
 **/

#include <stdio.h>

int main(void) {  
  
  float radius = -1.0; 
  float x = 0.0;
  float y = 0.0;
  int status = 0;

  while (radius < 0.0) {
    printf("\nEnter radius: ");
    scanf("%f",&radius);
  }

  printf("\nEnter the x coordinate: ");
  scanf("%f",&x);
  printf("\nEnter the y coordinate: ");
  scanf("%f",&y);

  if (x*x + y*y <= radius*radius) {
    printf("\nInside!\n");
  }
  if (x*x + y*y > radius*radius) {
    printf("\nOutside!\n");
  }
  
   return status;    

} 

float radius = -1.0; 
float x = 0.0;
float y = 0.0;
int status = 0;
Declaration of variables. radius is initialized to -1, such that the while-loop will be executed at least once.
 

while (radius < 0.0) {
  printf("\nEnter radius: ");
  scanf("%f",&radius);
}
Data is entered using a while loop, such that if the user enters a negative radius, the program will ask again.
 

printf("\nEnter the x coordinate: ");
scanf("%f",&x);
printf("\nEnter the y coordinate: ");
scanf("%f",&y);
The user of the program is asked to enter point coordinates
 

if (x*x + y*y <= radius*radius) {
  printf("\nInside!\n");
}
if (x*x + y*y > radius*radius) {
  printf("\nOutside!\n");
}
Calculate whether the point is inside (or on the boundary of) the circle. We use the equation for a circle, x2 + y2 = r2, to detrmine whether the distance between the point and the center is less than or equal to the radius. It is OK in C to evaluate such an expression where it is needed, in this case just when the if-statement is evaluated.
 

return status;    
Terminate program
 


Generated: Wednesday, March 29, 2006, 12:33:04
This program dissection page is generated from an XML-in-LAML source file