1. Write a C program that prompts the user to enter a number (of
integer
type)
and prints the absolute value of this number. For the purpose of
this activity, please do not use the abs function in the math
library.
(Hint: You might want to describe the algorithm in English before
writing any code. When you have written the code, be sure to hand-trace
the code with sample input values.)
2. (Exercise 5 in chapter 4 of Hanly, Koffman) Write a program that takes the x-y coordinates of a point in the Cartesian plane and prints a message telling either an axis on which the point lies or the quadrant in which it is found.
Sample lines of output:
(-1.0, -2.5) is in quadrant III
(0.0, 4.8) is on the y axis
/* absolute value function */
/* prompts the user to enter an integer value and prints the
* absolute value of the entered integer
*/
#include <stdio.h>
int main (void) {
/* declarations */
int abs_value;
/* prompt user */
printf("Please enter an integer.\n");
scanf("%d", &abs_value);
/* check to see if abs_value is < 0 */
if(abs_value < 0){
abs_value = -abs_value;
}
/* print the absolute value */
printf("Absolute value: %d\n", abs_value);
/* successful return */
return 0;
}
2.
/* Program prints out the axis (or axes) on which a point lies or
the
quadrant in which a point lies. The user is
asked to enter the x
and y coordinates of the point */
#include <stdio.h>
int main (void) {
/* declarations */
double x_coordinate, y_coordinate;
/* Prompt user to enter x-coordinate value */
printf("Please enter the x-coordinate: ");
scanf("%lf", &x_coordinate);
/* Prompt user to enter y-coordinate value */
printf("Please enter the y-coordinate: ");
scanf("%lf", &y_coordinate);
/* check for point on x-axis */
if(y_coordinate == 0.0){
printf("(%f, %f) is
on the x axis\n", x_coordinate, y_coordinate);
}
/* check for point on y-axis */
if(x_coordinate == 0.0){
printf("(%f, %f) is
on the y axis\n", x_coordinate, y_coordinate);
}
/* check for quadrant I */
else if(x_coordinate > 0.0 && y_coordinate
> 0.0){
printf("(%f, %f) is
in quadrant I\n", x_coordinate, y_coordinate);
}
/* check for quadrant II */
else if(x_coordinate < 0.0 && y_coordinate
> 0.0){
printf("(%f, %f) is
in quadrant II\n", x_coordinate, y_coordinate);
}
/* check for quadrant III */
else if(x_coordinate < 0.0 && y_coordinate
< 0.0){
printf("(%f, %f) is
in quadrant III\n", x_coordinate, y_coordinate);
}
/* check for quadrant IV */
else{
printf("(%f, %f) is
in quadrand IV\n", x_coordinate, y_coordinate);
}
/* all possibilities covered by now */
/* terminate successfully */
return 0;
}