You are on page 1of 4

Cs112 lab 2

Second weeks lab


Question 1 )

#include <iostream>
#include<math.h>
using namespace std;
void points (double x1, double yl, double x2, double y2, double&d, double&xm,
double&ym)
{
d=sqrt((x1-x2) * (x1-x2) + (yl-y2) * (yl-y2)) ;
xm= (x1+x2) /2;
ym= (yl+y2) /2;
}
int main(){
double d, xm, ym;
points(1, 1, 2, 2, d, xm, ym);
cout << "distance between (1,1) and (2,2) = " << d << endl;
cout << "midpoint between (1,1) and (2,2) = ";
cout << '(' << xm << ',' << ym << ')' << endl;
}
Question 2 )

#include <iostream>
#include<math.h>
using namespace std;
void getminmax (int a[], int sz, int &min, int &max, int &imin, int &imax)
{
min=max=a[0]; imin = imax = 0;
for (int i =1; i<sz; i++)
{
if (a[i]< min)
{
min=a[i];
imin=i;
}
if (a[i]>max)
{
max=a[i];
imax=i;
}
}
}
int main()
{
int a[] = {12, 44, 28, 23, 8, 34, 22, 18, 11};
int size=9;
int min, max, imin, imax;
getminmax(a, size, min, max, imin, imax);
cout << "Min value = " << min << " - found at: " << imin << endl;
cout << "Max value = " << max << " - found at: " << imax << endl;
return 0;
}
Question 3 )

#include <iostream>
#include<math.h>
using namespace std;
void delkey (int a[],int &sz, int k)
{
int loc =0;
for (int i=0; i<sz;i++)
if (a[i]==k)
loc = i;
for (int i=loc; i <sz-1;i++)
a[i]=a[i+1];
sz--;
}
int main() {
int a[] = {12, 44, 28, 23, 8, 34, 22, 18, 11};
int size=9;
for (int i=0; i<size; i++)
cout << " " << a[i];
cout << endl;
delkey(a, size, 34);
for (int i=0; i<size; i++)
cout << " " << a[i];
cout << endl;
return 0;

You might also like