Question
Write a c++ program that does the following
Create an integer array of size 30. Assign -1 to each location in the array indicating that the array is empty. Populate half of the array with random integer values between 100 and 500 inclusive. Use the following formula in order to hash and store each number in its proper position/location. Generated Number Table Size: Should a collision occurs, use linear probing to find next available position location. Use the following probing hashing function. Generated Number 1 Table Size; Display and introduction message followed by the generated array. The generated array should be displayed in 2 lines. Each line contain 15 numbers separated by 2 spaces

media%2Fabe%2Fabe24103-84da-4474-b163-1c
media%2Fa11%2Fa11928bc-59e5-4c6b-a42b-20
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<stdlib.h>

using namespace std;

class Array
{
        int size;
        int *arr;
public:
    Array(int size)
    {
        arr= new int[size];
        this->size=size;
        for(int i=0;i<size;i++)
        {
            arr[i]=-1;
        }
    }
    int populateArray();
    int search(int element);
    void display();
    bool insert(int num,int *collisions);
    bool deleteElement(int num);
};

bool Array::insert(int element,int *collisions)
{
    *collisions=0;

    int generated_index = element%30;

       if(arr[generated_index] == -1)
       {
           arr[generated_index]= element;
           return true;
       }
       else
        {

            int index= generated_index;//to break later
            while(arr[++index]!=-1 )
            {
                *collisions++;
                if(index==29)
                        index=0;
                if(index==generated_index)
                    return false;;
            }
            arr[index]=element;
        }

    return true;
}

void Array::display()
{
    for(int i=0;i<size;i++)
            cout<<arr[i]<<" ";
}

int Array::search(int element)
{
    for(int i=0;i<size;i++)
            if(arr[i]==element)
                return i+1;
}
bool Array::deleteElement(int ele)
{
    for(int i=0;i<size;i++)
        if(arr[i]==ele)
        {
            arr[i]=-1;
            return true;
        }
    return false;
}

int main()
{
    int generatedNo,element,collisions,ch,ele;
    Array arr(30);

    for(int i=0;i<15;i++)
    {
        generatedNo = rand() % 500 + 100 ;
        arr.insert(generatedNo,&collisions);
    }
    do{
        cout<<"1. Add element";
        cout<<"\n2. Display";
        cout<<"\n3. Search an element";
        cout<<"\n4. Delete an element";
        cout<<"\n5. Exit";
        cout<<"\n\nEnter your choice: ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\nEnter an element: ";
                cin>>ele;
                arr.insert(ele,&collisions);
                cout<<"\nNumber of collisions: "<<collisions;
                break;
            case 2:
                cout<<"\nContents of the array are: ";
                arr.display();
                break;
            case 3:
                cout<<"Enter an element to search(100-500): ";
                cin>>ele;
                do{
                    if(ele>=100 && ele<=500)
                        arr.search(ele);
                else
                    cout<<"Please enter a correct number";
                }while(ele>=100 && ele<=500);
                break;
            case 4:
                cout<<"\nEnter an element you want to delete(100-500): ";
                cin>>ele;
                do{
                    if(ele>=100 && ele<=500)
                        arr.deleteElelment(ele);
                    else
                        cout<<"Please enter a correct number";
                }while(ele>=100 && ele<=500);
                break;
            default:
                cout<<"\nExiting...\n";
        }
    }while(ch!=5);
    return 0;
}

Output:

℃:\Users\Mayank Bhardwaj\Desktop\array.exe 5. Exit Enter your choice: 2 Contents of the array are: -1 -1-1-1 -1 305 245 427 5

Add a comment
Know the answer?
Add Answer to:
Write a c++ program that does the following Create an integer array of size 30. Assign...
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
  • Write a C++ program that does the following : 1. Accepts array size from the keyboard....

    Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Create a hash table class/struct.in C++ Define an array that holds 27 elements. Define a function...

    Create a hash table class/struct.in C++ Define an array that holds 27 elements. Define a function called Hash(int) -This function returns the modulo of that int by the size of the table (array). Define an add function that takes an integer. -This function takes the integer, determines the hash of that number by calling the above hash function, then adds it to the table using linear probing for collision resolution. Define a function that looks up a value, it takes...

  • In Java create an integer array named dice1 with a size of 10. Populate each array...

    In Java create an integer array named dice1 with a size of 10. Populate each array location with a roll of a six-sided die (hint: an int value of 1 through 6). Print the array out using an enhanced for loop. Sample output: Question 1 dice1 = 1 1 6 2 3 5 1 5 4 5

  • Write a C++ program to : Create array arr1 of size 10 Assign values to elements...

    Write a C++ program to : Create array arr1 of size 10 Assign values to elements such that - say index = i, arr1[i] = 10 - i. Print arr1 Create arr2 and copy alternate elements from arr1 starting from index 1. Print arr2. Sort arr1 in ascending array and print its elements from indices 3 to 8 Print every output on a different line and separate array elements by spaces.

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • C++: Create a class called "HashTable" This class will implement hashing on an array of integers...

    C++: Create a class called "HashTable" This class will implement hashing on an array of integers Make the table size 11 Implement with linear probing. The hash function should be: h(x) = x % 11 Create search(), insert() and delete() member functions. Search should return the index of where the target is located, insert should allow the user to insert a value, and delete removes the desired value from the table. In main perform the following: 1. Insert: 17 11...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Create a variable named my_student_ID and assign YOUR STUDENT ID to it. Write a program that...

    Create a variable named my_student_ID and assign YOUR STUDENT ID to it. Write a program that generates a random integer between 1 and 10, inclusive; then displays the generated random integer and digits in my_student_ID that are less then or equal to the generated integer as a list. For example, if your student id is 45755245642 and the computer generates 4, then your program should display the below message random integer is 4 list of integer that are less than...

  • Insertthe following numbres into a initially empty hash table of size 10. The functions h(x) =...

    Insertthe following numbres into a initially empty hash table of size 10. The functions h(x) = x % 10. Use linear probing when collision happens. Draw the hash tables and show the intermediate steps. 8,15,6,25,77,39

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
ADVERTISEMENT