C program that takes as input two memory addresses and prints the displacement address

Posted by Samath
  1113
June 02, 2015

 

Write a C program that takes as input two memory addresses between 0 and ((2^8) – 1) and prints the displacement address (e.g. the distance between two points).

Solution:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
int address1 = -1; 
int address2 = -1;
int displacement; 

do
{
	printf("Enter first address: ");
	scanf("%d", &address1);
}
while(address1 < 0 || address1 > 255);


do
{
	printf("Enter second address: ");
	scanf("%d", &address2);
}
while(address1 < 0 || address1 > 255);



printf("Address1: %d\n", address1);
printf("Address2: %d\n", address2);

displacement = fabs(address1 - address2); 
printf("Displacement: %d\n", displacement);

system("PAUSE");
return 0;
}