Data Structure and Algorithm Lab Task 02, Assignments of Data Structures and Algorithms

Data Structure and Algorithm Lab Task 02

Typology: Assignments

2020/2021

Uploaded on 03/16/2021

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB # 02 Data Structure & Algorithms Lab
1
Task 2.1: Declare and initialize an array of size taken from user as input, display array values on screen
And find sum of array elements and finally display the summation result.
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
int array[n];
cout<<"Enter the Array size: ";
cin>>n;
for(int i=0; i<n; i++){
cout<<"Enter element"<<i+1<<" : "; cin>>array[i];
}
cout<<"\n{ ";
for(int i=0; i<n; i++){
cout<<array[i]<<",";
}
cout<<" }\n";
for(int i=0; i<n; i++)
{
sum=sum+array[i];
}
cout<<endl;
cout<<"The sum of all element : "<<sum<<endl;
}
pf3
pf4
pf5

Partial preview of the text

Download Data Structure and Algorithm Lab Task 02 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Task 2.1: Declare and initialize an array of size taken from user as input, display array values on screen And find sum of array elements and finally display the summation result. #include using namespace std; int main() { int n,sum=0; int array[n]; cout<<"Enter the Array size: "; cin>>n;

for(int i=0; i<n; i++){ cout<<"Enter element"<<i+1<<" : "; cin>>array[i]; }

cout<<"\n{ "; for(int i=0; i<n; i++){ cout<<array[i]<<","; } cout<<" }\n"; for(int i=0; i<n; i++) { sum=sum+array[i]; } cout<<endl; cout<<"The sum of all element : "<<sum<<endl; }

Task 2.2: Declare an array of size 8. Receive all the elements from the user as input using for- loop. Display the entered elements from user as input in reverse order. #include using namespace std; int main() { system("color f0"); int array[8];

cout<<"Enter 8 Element of array"<<endl; for(int i=0; i<8; i++) { cin>>array[i]; } cout<<"Array elemnt in Reverse Order: "; for(int i=0; i<8; i++) {cout<<array[i]<<",";} }

Task 2.3: Use an int pointer and an int variable and displays the variables address.

#include using namespace std; int main() { system("color f0"); int variable; int *ptr; cout<<"Address of Variable: "<<&variable<<endl; cout<<"Address of Pointer : "<<&ptr<<endl; }

Task 2.6: Create a class named “Car” with attributes xPosition, yPosition, speed. The class should have methods such as “accelerate”, “decelerate” to increment and decrement the speed of the car while for change in xPosition & yPosition, there should be methods such as turn Left, turnRight. A n extra method“currState” should display all the data members of the object. #include using namespace std; class Car { private: float xPosition, yPosition, speed; public: Car(float x,float y,float sp) { xPosition=x; yPosition=y; speed=sp; } float accelarate() { ++speed; } float deaccelarate() { --speed; } float moveX(float a) {xPosition+=a;} float turnLeft() {moveX(-1.0);} float turnRight() {moveX(1.0); } void currState() { std::cout << "I am in xPosition " << xPosition << " and yPosition " << yPosition << " with velocity " << speed << std::endl; } }; int main() { Car c(1.7,3.3,100.3); c.accelarate(); c.deaccelarate(); c.moveX(1.0); c.turnLeft(); c.turnRight(); c.currState(); return 0; }

Task 2.7: Create an array of 10 cars, randomly assigning values to xpos,ypos. Using a pointer of type car in a for loop, display the state of all the cars. #include using namespace std;

int main() { system("color f0"); int xpos=10,ypos=20; int *car[10];

car[0] = &xpos; car[1] = &ypos; car[2] = &xpos; car[3] = &xpos; car[4] = &ypos; car[5] = &xpos; car[6] = &xpos; car[7] = &ypos; car[8] = &xpos; car[9] = &xpos; for(int a=0;a<10;a++) { cout << *car[a]<<", "; } }