Question

Q3. (15 marks)using c/c++ Write a COMPLETE Console Application that allows user to work with a...

Q3. (15 marks)using c/c++

Write a COMPLETE Console Application that allows user to work with a database of 20 numbers. The program should:

  • Creates an initial array of size 20.
    • At the start, all of the locations in the array are considered empty (available)
    • Hint: Have a variable that shows how many numbers are in the array. It will be zero at first, and it changes based on the operations.
  • Show a menu to user with these options:
    • List the existing numbers
      • Do not show empty available locations
    • Add a number to the array
      • This number goes to the first available location. Nothing will be added if array is full.
    • Insert a number at a given location in the array.
      • All numbers after the given location will be shifted towards the end. For example, [1 3 6 8 7], by inserting a 0 at location 2 will be [1 3 6 0 8]. The last number will be lost.
      • If the location to insert is more than the first available location, then the number will be inserted at the first available location. For example, if the array is [1 3 6 x x] and x are empty locations, inserting a 5 at location 4 will result in [1 3 6 5 x] instead of [1 3 6 x 5]
    • Delete the number at the given location.
      • This will remove the number and shift all the numbers after it down. For example, deleting 5 from this array [1 3 5 7 9] will result in [1 3 7 9 x]. Note that if we shift down the last member, it will become empty/available.
    • Exit the program
  • Perform the actions
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>
using namespace std;
int main()
{
int arr[20];
int count=0;
while(true)
{
cout<<endl;
cout<<"1: List the existing numbers"<<endl;
cout<<"2: Add a number to the array"<<endl;
cout<<"3: Insert a number at a given location in the array: "<<endl;
cout<<"4: Delete the number at the given location"<<endl;
cout<<"5: Exit the program"<<endl;
cout<<"Enter choice: ";
int choice;
cin>>choice;
if(choice==1)
{
for(int i=0;i<count;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
else if(choice==2)
{
if(count!=20)
{
int i;
cout<<"Enter number: ";
cin>>i;
arr[count++]=i;
}
}
else if(choice==3)
{
int loc,i;
cout<<"Enter location: ";
cin>>loc;
cout<<"Enter number: ";
cin>>i;
if(loc>=count)
arr[count++]=i;
else
{
for(int j=count-1;j>=loc;j--)
arr[j+1]=arr[j];
arr[loc]=i;
count++;
}
}
else if(choice==4)
{
int loc,i;
cout<<"Enter location: ";
cin>>loc;
for(int i=loc;i<count;i++)
arr[i]=arr[i+1];
count--;
}
else
break;
}

}
Output:

Add a comment
Know the answer?
Add Answer to:
Q3. (15 marks)using c/c++ Write a COMPLETE Console Application that allows user to work with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT