Question

In: Computer Science

(Duplicate Elimination) Use a one dimensional array to slove the following problem. Read 20 numbers, each...

(Duplicate Elimination) Use a one dimensional array to slove the following problem. Read 20 numbers, each of which is 10 and 100, inclusive. As each number is read, print it only if it's not a duplicate of a number already read. Provide for the "worst case" in which all 20 numbers are different. use the smallest possible array to slove this problem.

Add the following functionality to your solution. Sort the array in ascending order and print the elements of the sorted array.

if you could please explain each line of the code that would be wonderful.

Solutions

Expert Solution

I think you are saying input number should be between 10 and 100, inclusive.

I have Written code is in C++, as you have not specified any specific programming language.

  • Our program 20 integers input one by one
  • If any entered integer is already exist, it prints "recently entered Number is duplicate."
  • else if it is not duplicate, it prints recently entered integer.
  • It uses sort() function in c++ to sort our array.
  • after sorting it prints sorted array.
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    //array with size 20
    int arr[20];
    cout<<"input 20 integers one-by-one : ";
    for(int i = 0; i < 20; i++)
    {
        cin>>arr[i];
        int flag = 0;
        //This loop checks if given entered element is already exist in array or not
        for(int j = 0; j < i; j++)
        {
            //if it already exist then set flag to 1
            if(arr[i] == arr[j])
                flag  = 1;
        }
        if(flag == 0)
        {
            cout<<arr[i]<<"\n\n";
        }
        else cout<<"recently entered Number is duplicate."<<"\n\n";
    }
    
    //sort is method to sort array in c++
    sort(arr, arr + 20);
    
    //print sorted array
    for(int i = 0; i < 20; i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
}

Output for a sample testcse:

Try running this code on your own system.

Like, if this helped :)


Related Solutions

(duplicate Elimination) use a one-dimensional array to slove the following program. read 20 numbers, each of...
(duplicate Elimination) use a one-dimensional array to slove the following program. read 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print if only if its not a duplicate of a number already read. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to slove this problem. sort the array in ascending order and print the elements of the sorted array. This much be done...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. Your solution must include a function called isUnique() that returns 1 (true) if the number input is...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following problem: Write an application that inputs ten numbers, each between 10 and 100, both inclusive. Save each number that was read in an array that was initialized to a value of -1 for all elements. Assume a value of -1 indicates an array element is empty. You are then to process the array, and remove duplicate elements from the array containing the numbers you...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
In JAVA Use a two-dimensional array to solve the following problem: A company has four salespeople...
In JAVA Use a two-dimensional array to solve the following problem: A company has four salespeople - Sales Person 1, Sales Person 2, Sales Person 3, and Sales Person 4. The company sells 5 products - Product 1, Product 2, Product 3, Product 4, and Product 5. Each day, a sales person hands in a slip with the following information: Sales Person Number (1,2,3, or 4), Product Number (1,2,3,4, or 5), and dollar value of that product sold. This dollar...
Write a program in Visual Basics that does the following: Read in 20 numbers, each of...
Write a program in Visual Basics that does the following: Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT