You are on page 1of 6

SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY

NAME : MUNIZA KAMRAN


ROLL NO : 2020F-BIT-012
DEPARTMENT : INFORMATION TECHNOLOGY
SUBJECT : DATA STRUCTURE & ALGORITHUM
SECTION : A
TEACHER : SIR FARAZ
LAB 01

TASK : 1 Code a function in C++ to traverse the elements of an array.


#include <iostream>
using namespace std;

int main()
{
int m, n;

int arr[10] = { 21,34,20,76,39,22,38,7,31,63 }; // Inilizing value of an Array

cout << "Travers Elements of an Array:" << endl;

for (m = 0; m < 10; m++) // for loop to repeat a specific number of times
{
cout << arr[m] << endl; // to print an array
}
return 0;
}

TASK : 2 Write a C++ program to find the sum and average of one dimensional
integer array.
#include<iostream>
using namespace std;

int main()
{
int a[10];
int s = 0;
float avg;
cout << "Enter values:" << endl;
for (int i = 0; i < 10; i++) //for loop to take numbers 10 times
{
cin >> a[i]; //input integer from user
s = s + a[i]; //formula of sum
}
cout << "Sum: " << s << endl;
avg = s / 10; //formula of average
cout << "Average: " << avg;
}

TASK : 3 Write a C++ program to swap first and last element of an integer 1-d
array.
#include <iostream>
using namespace std;

int main()
{
int m, a; // define variable
int num[10] = { 10,12,14,16,18,20,22,24,26,28 }; // Inilizing an Array

cout << "Swap first element with last element: " << endl;

a = num[0]; // taking a equal to num[0] which is 1


num[0] = num[9];
num[9] = a;
for (m = 0; m < 10; m++) // for loop
{
cout << num[m] << endl; // print new array
}
return 0;
}
TASK : 4 Write a C++ program to reverse the element of an integer 1-D array.

#include <iostream>
using namespace std;

int main()
{
int a[5];
int m;

cout << "Enter Array Elements:\n";


for (m = 0; m < 5; m ++)
{
cin >> a[m]; //taking array from user
}

cout << "The Reversed of Array :\n";


for (m = 0; m < 2; m++)
{
a[m] = a[m] + a[4 - m]; //For reverse the array
a[4 - m] = a[m] - a[4 - m];
a[m] = a[m] - a[4 - m];
}

for (m = 0; m < 5; m++)


{
cout << a[m] << endl; //The reverse numbers of an array
}
return 0;
}
TASK : 5 Write a C++ program to find the largest and smallest element of an
array.
#include <iostream>
using namespace std;

int main()
{
int array[5]; //initilizing an array
int m;
int large, small;

cout << "Enter any 5 Numbers:" << endl;


for (m = 0; m< 5; m++)
{
cin >> array[m]; //taking array numbers from user
}
large = array[0]; //initilizing array as 0
small = array[0];

for (m = 0; m < 5; m++)


{
if (large < array[m]) //if statement for getting large element
{
large = array[m];
}
if (small > array[m]) //if statement for getting small element
{
small = array[m];
}
}
cout << "\nLargest element of an array is: " << large << endl;
cout << "Smallest element of an array is: " << small;

return 0;
}
TASK : 6

You might also like