



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data Structure and Algorithm Lab Task 02
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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
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
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
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
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
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]<<", "; } }