0% found this document useful (0 votes)
63 views7 pages

Assignment No 1..... DSA

The document discusses various operations on arrays such as clear, copy, get, and update. It provides algorithms and code for each operation. For the clear operation, it describes an algorithm to clear all elements of an array and code to take input, clear elements based on user choice, and output the final array. For copy, it outlines copying one array into another. The get operation retrieves an element at a given index. Finally, update replaces an element at a specified position with a new value.

Uploaded by

Ahmad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views7 pages

Assignment No 1..... DSA

The document discusses various operations on arrays such as clear, copy, get, and update. It provides algorithms and code for each operation. For the clear operation, it describes an algorithm to clear all elements of an array and code to take input, clear elements based on user choice, and output the final array. For copy, it outlines copying one array into another. The get operation retrieves an element at a given index. Finally, update replaces an element at a specified position with a new value.

Uploaded by

Ahmad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment No# 1

Name:
Ahmad Raza
Roll No:
F20-BSCS-5069
Class Section:
BSCS-3rd E B

Subject:
Data Structure & Algorithm

Submitted To:
Ms.Sobia
Topic:
 Clear
 Copy
 Get
 Update
Clear Operation:
Clear a list(remove all elements.

I. Algorithm:
1) Start
2) Initialize array
3) Ask for user to enter array length and elements
4) Enter length and element
5) Loop to visit each element
6) Make a choice for user to clear the list or not.
7) If YES then clear the elements and if NO then print the final
array
8) End
II. Code:
#include<iostream>
using namespace std;
int main()
{
int arrA[100];
int len,n;
cout<<"Enter the length of Array\n";
cin>>len;
cout<<"Enter the Elements of Array \n";
for(int i=0;i<len;i++)
{
cin>>arrA[i];
}
cout<<"Clear the array list\n";
cout<<"Press 1 if You want to Clear the Array\n";
cout<<"Press 2 if Don't want.\n";
cin>>n;
if(n==1)
{
for(int c=0;c<len;c++)
{ arrA[c]=0;
}
cout<<"Now Final Array is :\n";
for(int e=0;e<len;e++)
{
cout<<arrA[e]<<endl;
}
}
else
{
for(int c=0;c<len;c++)
{ cout<<arrA[c]<<endl;
}
}

Output:

Copy Operation:
Set one list to be a copy of another

I. Algorithm:
1) Start
2) Initialize two arrays of same type with same length
3) Ask for user to enter length and array elements.
4) Enter length and element.
5) After entering elements in array one store them into array
second
6) Print the output.
7) End
Code:
#include<iostream>
using namespace std;
int main()
{
int arrA[100],arrB[100];
int len;
cout<<"Enter the Length of Array \n";
cin>>len;
cout<<"Enter the Elements of Array\n";
for(int i=0;i<len;i++)
{
cin>>arrA[i];
}
for(int c=0;c<len;c++)
{
arrB[c]=arrA[c];
}
cout<<"final array is :\n";
for(int e=0;e<len;e++)
{
cout<<arrB[e]<<endl;
}
}

Output:
Get Operation:
Get element at a given position

I. Algorithm:
1) Start
2) Initialize an array and variables Len and value
3) Ask for user to enter array length and elements
4) Enter the length and elements
5) Run a loop to visit each element
6) Enter the index to be searched in array
7) Check the condition if the index value is exciting then print output otherwise stop
the program
8) End
II. Code:
#include<iostream>
using namespace std;
int main()
{
int arr[30]; int len,value;
cout<<"Enter the Length of Array\n";
cin>>len;
cout<<"Enter the Elements of Array\n";
for(int i=0;i<len;i++)
{
cin>>arr[i];
}
cout<<"Enter the index\n";
cin>>value;
for(int c=0;c<len;c++)
{
if(c==value)
{
cout<<"The Value at index "<<value<< " is: "<<arr[c]<<endl;
}}}
Output:

Update Operation:
Replace the element at a given position with x

I. Algorithm:
1) Start
2) Take a user defined array
3) Enter elements and length.
4) A loop to visit each element
5) Asked a user for a key value and index to update.
6) Enter the value
7) Print values after updating arrays elements.
8) End.
II. Code:

#include<iostream>
using namespace std;
int main()
{
int arr[30];
int len , key, index;
cout<<"Enter the Length of Array\n";
cin>>len;
cout<<"Enter the Elements of Array\n";
for (int i=0;i<len; i++)
{
cin>>arr[i];
}
cout<<"You Entered Array elements are
as follows: \n";
for(int c=0;c<len;c++)
{
cout<<"arr["<<c<<"]="<<arr[c]<<endl;
}
cout<<"Enter the value for update\n";
cin>>key;
cout<<"Enter the Index\n";
cin>>index;
for(int c=0;c<len;c++)
{
if(c==index)
{
arr[c]=key;
break;
}
}
cout<<"After the updation of Array
Elements:\n";
for(int e=0;e<len;e++)
{
cout<<"arr["<<e<<"]="<<arr[e]<<endl;
}
}

Output:

You might also like