I need help implenting a sorted list ADT. I\'m supposed to add numbers to a list
ID: 3732609 • Letter: I
Question
I need help implenting a sorted list ADT.
I'm supposed to add numbers to a list and have it output the list in descending order. It should print out the numbers and ask the user for a new number and then insert that new number into the correct position. It should keep this up until the user inputs a non positive number.
I also need a specific function "getPosition()" which should return the position of where an entry should be (but as a negative int instead)
Example of the program
User inserts 5
The list prints out 5
User inserts 4
List prints out 5 4
User asks for position of "1" and it should return -3
My bad, this is for C++
Explanation / Answer
Hi,
Please find the C++ code for your problem and sample output. Please let me know if you need anything.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void position(int number); //To get position
void sort_t(int add_n,int i); //To sort the numbers
int arr[100]; // Global variable
int main() // Main function
{
int option=0,add_n,i=0; // Variables
while(option == 0) // Getting input
{
cout << "Please Enter the option ";
cout << "1. Enter the number to list ";
cout << "2. Get Position of number will stay ";
cin >> option;
if (option == 1)
{
cout << "1. Enter the number ";
cin >>add_n;
sort_t(add_n,i); // calling sort_t funtion to sort the numbers
i++;
option=0;
}
else if (option == 2)
{
position(5); // calling position function to get the position
option=0;
}
else
{
exit(0); //exiting with incorrect input
}
}
return 0;
}
void position(int number) // positon function defenition
{
//cout << "position";
int count=1;
cout << "Enter the number ";
cin >> number;
int i = sizeof(arr)/sizeof(arr[0]);
for (int j = 0; j <= i; ++j)
{
if (number < arr[j])
{
count++;
}
}
cout <<count*(-1)<<" "; // convert into negative number
}
void sort_t(int add_n,int i) // sorting funtion defenition
{
cout << "sort";
arr[i]=add_n;
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n, greater<int>());
cout << "Array after sorting : ";
for (int j = 0; j <= i; ++j)
cout << arr[j] << " ";
cout << " ";
}
Output:
Please Enter the option
1. Enter the number to list
2. Get Position of number will stay
1
1. Enter the number
1
sortArray after sorting :
1
Please Enter the option
1. Enter the number to list
2. Get Position of number will stay
1
1. Enter the number
5
sortArray after sorting :
5 1
Please Enter the option
1. Enter the number to list
2. Get Position of number will stay
1
1. Enter the number
9
sortArray after sorting :
9 5 1
Please Enter the option
1. Enter the number to list
2. Get Position of number will stay
2
Enter the number
8
-2
Please Enter the option
1. Enter the number to list
2. Get Position of number will stay
2
Enter the number
4
-3
Please Enter the option
1. Enter the number to list
2. Get Position of number will stay
5
program ended
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.