0% found this document useful (0 votes)
2 views

Lab No 2

Uploaded by

atiq.rehman1105
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab No 2

Uploaded by

atiq.rehman1105
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab no 2

Task no 1:
#include <iostream>
using namespace std;
int main()
{ int arr[5];
cout << "enter elements of array:" << endl;
for (int i=0; i<5; i++)
{
cin >> arr[i];
}
for (int i=0; i<5; i++)
{
cout << "square of element " << i << " : " << arr[i] * arr[i] << endl;
}
return 0; }
Task no 2:
#include <iostream>
using namespace std;

int check ( int arr1[], int s, int k)


{
for (int i=0; i<3; i++)
{
if(arr1[i] == k)
{
return 1;
}
else
{
return -1;
}

}
}
int main()
{
int arr[3];
cout << "enter elements of array: " << endl;
for (int i=0; i<3; i++)
{
cin >> arr[i];
}
int key;
cout << " enter key: " << endl;
cin >> key;
cout << check(arr, 3 , 5);

return 0;
}
Task no 3:
#include <iostream>
using namespace std;

void swapfunc(int arr1[], int s)


{
int start = 0;
int end = s -1;
int temp;
while (start < end)
{
temp=arr1[start] ;
arr1[start] = arr1[end];
arr1[end]= temp;
start++;
end--;
}
for (int i=0; i<s; i++)
{
cout << arr1[i];
}
}
int main()
{
int size;
cout << "enter size: " << endl;
cin >> size;
int arr[size];
int sizeofarr = sizeof(arr)/sizeof(arr[0]);
cout << "enter elements of array: " << endl;
for (int i=0; i<sizeofarr; i++)
{
cin >> arr[i];
}
swapfunc(arr, sizeofarr);
return 0;
}

Task no 4:
#include <iostream>
using namespace std;

void swapfunc(int arr1[], int s, int d)


{
int start = s + d;
int end = s + d + 1;
int temp;
for (int i=0; i <s; i++)
{
temp=arr1[start] ;
arr1[start] = arr1[end];
arr1[end]= temp;
start++;
end++;
}
for (int i=0; i<s; i++)
{
cout << arr1[i];
}
}
int main()
{
int size;
cout << "enter size: " << endl;
cin >> size;
int arr[size];
int sizeofarr = sizeof(arr)/sizeof(arr[0]);
int D;
cout << "enter D steps: " << endl;
cin >> D;
cout << "enter elements of array: " << endl;
for (int i=0; i<sizeofarr; i++)
{
cin >> arr[i];
}
swapfunc(arr, sizeofarr, D);

return 0;
}

You might also like