theSJace
8/30/2018 - 2:40 AM

## Practice Lab Qns - Lab 3

``````#include <stdio.h>
#include <math.h>
void inputXY(double *x1, double *y1, double *x2, double *y2);
void outputResult(double dist);
double calDistance1(double x1, double y1, double x2, double y2);
void calDistance2(double x1, double y1, double x2, double y2, double *dist);
int main()
{
double x1, y1, x2, y2, distance=-1;

inputXY(&x1, &y1, &x2, &y2); // call by reference
distance = calDistance1(x1, y1, x2, y2); // call by value
printf("calDistance1(): \n");
outputResult(distance);
calDistance2(x1, y1, x2, y2, &distance); // call by reference
printf("calDistance2(): \n");
outputResult(distance); // call by value
return 0;
}
void inputXY(double *x1, double *y1, double *x2, double *y2)
{
/* Write your code here */
//Prompt user and then read in the values
printf("Enter the x1 y1 x2 y2: \n");
//this data *x1 is already referencing to the memory of the original x1
// because this is a pointer need to dereference
scanf("%lf %lf %lf %lf", x1, y1, x2, y2);
/* OR scanf("%lf %lf %lf %lf", &*x1, &*y1, &*x2, &*y2);

*/

}
void outputResult(double dist)
{
/* Write your code here */
//Write a print function
//Print double in 2 decimal places
printf("%.2f\n", dist);
}
//Expecting a return of the data tyoe
double calDistance1(double x1, double y1, double x2, double y2)
{
/* Write your code here */

double a, b, c, d;
a = pow((x2 - x1), 2);
b = pow((y2 - y1), 2);
d = a+b;
c = sqrt(d);
return c;
}
void calDistance2(double x1, double y1, double x2, double y2, double
*dist)
{
/* Write your code here */
//pass by reference

double a, b, c, d;
a = pow((x2 - x1), 2);
b = pow((y2 - y1), 2);
d = a+b;
c = sqrt(d);
*dist = c;
}
``````
``````#include <stdio.h>
#include <math.h>
void inputXY(double *x1, double *y1, double *x2, double *y2);
void outputResult(double dist);
double calDistance1(double x1, double y1, double x2, double y2);
void calDistance2(double x1, double y1, double x2, double y2, double *dist);
int main()
{
double x1, y1, x2, y2, distance=-1;

inputXY(&x1, &y1, &x2, &y2); // call by reference
distance = calDistance1(x1, y1, x2, y2); // call by value
printf("calDistance1(): \n");
outputResult(distance);
calDistance2(x1, y1, x2, y2, &distance); // call by reference
printf("calDistance2(): \n");
outputResult(distance); // call by value
return 0;
}
void inputXY(double *x1, double *y1, double *x2, double *y2)
{
/* Write your code here */
//Prompt user and then read in the values
printf("Enter the number: \n");
//this data *x1 is already referencing to the memory of the original x1
scanf("%lf %lf %lf %lf", &*x1, &*y1, &*x2, &*y2); // because this is a pointer need to dereference
printf("%lf %lf %lf %lf\n", *x1, *y1, *x2, *y2);
}
void outputResult(double dist)
{
/* Write your code here */
//Write a print function
printf("%lf\n", dist);
}
//Expecting a return of the data tyoe
double calDistance1(double x1, double y1, double x2, double y2)
{
/* Write your code here */

double a, b, c, d;
a = pow((x2 - x1), 2);
b = pow((y2 - y1), 2);
d = a+b;
c = sqrt(d);
return c;
}
void calDistance2(double x1, double y1, double x2, double y2, double
*dist)
{
/* Write your code here */
//pass by reference

double a, b, c, d;
a = pow((x2 - x1), 2);
b = pow((y2 - y1), 2);
d = a+b;
c = sqrt(d);
*dist = c;
}
``````
``````#include <stdio.h>
#include <math.h>
void inputXY(double *x1, double *y1, double *x2, double *y2);
void outputResult(double dist);
double calDistance1(double x1, double y1, double x2, double y2);
void calDistance2(double x1, double y1, double x2, double y2, double *dist);
int main()
{
double x1, y1, x2, y2, distance=-1;

inputXY(&x1, &y1, &x2, &y2); // call by reference
distance = calDistance1(x1, y1, x2, y2); // call by value
printf("calDistance1(): \n");
outputResult(distance);
calDistance2(x1, y1, x2, y2, &distance); // call by reference
printf("calDistance2(): \n");
outputResult(distance); // call by value
return 0;
}
void inputXY(double *x1, double *y1, double *x2, double *y2)
{
/* Write your code here */
}
void outputResult(double dist)
{
/* Write your code here */
}
double calDistance1(double x1, double y1, double x2, double y2)
{
/* Write your code here */
}
void calDistance2(double x1, double y1, double x2, double y2, double
*dist)
{
/* Write your code here */
}
``````
``````#include <stdio.h>
int square1(int num);
void square2(int num, int *result);
int main()
{
int number, result=0;
printf("Enter the number: \n");
scanf("%d", &number);
printf("square1(): %d\n", square1(number));
square2(number, &result);
printf("square2(): %d\n", result);
return 0;
}
int square1(int num)
{
/* Write your program code here */
int i, Sum = 0;
int sqrt= num * num;
if(num != 0)
{
for(i = 0; i <= sqrt; i++)
{
if ( i%2 != 0 && Sum < sqrt)
{
Sum = Sum + i;

}

}

return Sum;
}
else
return 0;
}
void square2(int num, int *result)
{
/* Write your program code here */
int i, Sum = 0;
int sqrt= num * num;

if(num != 0)
{
for(i = 0; i <= sqrt; i++)
{
if ( i%2 != 0 && Sum < sqrt)
{
Sum = Sum + i;

}
}

*result = Sum;
}
else
*result = 0;

}``````
``````#include <stdio.h>
int digitPos1(int num, int digit);
void digitPos2(int num, int digit, int *result);
int main()
{
int number, digit, result=0;
printf("Enter the number: \n");
scanf("%d", &number);
printf("Enter the digit: \n");
scanf("%d", &digit);
printf("digitPos1(): %d\n", digitPos1(number, digit));
digitPos2(number, digit, &result);
printf("digitPos2(): %d\n", result);
return 0;
}
int digitPos1(int num, int digit)
{
int totalDigits=1;
/* Write your program code here */
while(num!=0){

if ((num%10) ==digit)
{
}
else
{
num = num/10;
}
totalDigits ++;
}
return 0;
}
void digitPos2(int num, int digit, int *result)
{
/* Write your program code here */
int totalDigits=1;
/* Write your program code here */
while(num!=0){

if ((num%10) == digit)
{
*result = totalDigits;
break;
}
else
{
num = num/10;

}
totalDigits ++;
}
if (num==0)
{
*result = 0;
}
}``````
``````#include <stdio.h>
int numDigits1(int num);
void numDigits2(int num, int *result);
int main()
{
int number, result=0;

printf("Enter the number: \n");
scanf("%d", &number);
printf("numDigits1(): %d\n", numDigits1(number));
numDigits2(number, &result);
printf("numDigits2(): %d\n", result);
return 0;
}
int numDigits1(int num)
{
int totalDigits=0;

while(num!=0){
num = num/10;
totalDigits ++;
}