Question

In: Computer Science

**CODED IN C LANGUAGE** Case 1: The given snapshot in the assignment instructions checks for the...

**CODED IN C LANGUAGE**

Case 1: The given snapshot in the assignment instructions checks for the following:

  • P to be switched with Q (Once done will remain as it is in all the rows)
  • If the user enters the same P again, the program must not make any changes

For instance, given elements are 0123456789

3 4          0              0124456789

2 5          1              0154456789 (Keeping the change in Row 0 (input for row 1); 2 is switched to 5)

1 6          2              0654456789 (Keeping the change in row1 (input for row 2); 1 has been replaced with 6)

This implies that for every other row, the input array elements get changed to its previous row elements.

Solutions

Expert Solution

// A C++ program to print elements with count more than n/k 
#include<iostream> 
using namespace std; 

// A structure to store an element and its current count 
struct eleCount 
{ 
        int e; // Element 
        int c; // Count 
}; 

// Prints elements with more than n/k occurrences in arr[] of 
// size n. If there are no such elements, then it prints nothing. 
void moreThanNdK(int arr[], int n, int k) 
{ 
        // k must be greater than 1 to get some output 
        if (k < 2) 
        return; 

        /* Step 1: Create a temporary array (contains element 
        and count) of size k-1. Initialize count of all 
        elements as 0 */
        struct eleCount temp[k-1]; 
        for (int i=0; i<k-1; i++) 
                temp[i].c = 0; 

        /* Step 2: Process all elements of input array */
        for (int i = 0; i < n; i++) 
        { 
                int j; 

                /* If arr[i] is already present in 
                the element count array, then increment its count */
                for (j=0; j<k-1; j++) 
                { 
                        if (temp[j].e == arr[i]) 
                        { 
                                temp[j].c += 1; 
                                break; 
                        } 
                } 

                /* If arr[i] is not present in temp[] */
                if (j == k-1) 
                { 
                        int l; 
                        
                        /* If there is position available in temp[], then place 
                        arr[i] in the first available position and set count as 1*/
                        for (l=0; l<k-1; l++) 
                        { 
                                if (temp[l].c == 0) 
                                { 
                                        temp[l].e = arr[i]; 
                                        temp[l].c = 1; 
                                        break; 
                                } 
                        } 

                        /* If all the position in the temp[] are filled, then 
                        decrease count of every element by 1 */
                        if (l == k-1) 
                                for (l=0; l<k; l++) 
                                        temp[l].c -= 1; 
                } 
        } 

        /*Step 3: Check actual counts of potential candidates in temp[]*/
        for (int i=0; i<k-1; i++) 
        { 
                // Calculate actual count of elements 
                int ac = 0; // actual count 
                for (int j=0; j<n; j++) 
                        if (arr[j] == temp[i].e) 
                                ac++; 

                // If actual count is more than n/k, then print it 
                if (ac > n/k) 
                cout << "Number:" << temp[i].e 
                                << " Count:" << ac << endl; 
        } 
} 

/* Driver program to test above function */
int main() 
{ 
        cout << "First Test\n"; 
        int arr1[] = {4, 5, 6, 7, 8, 4, 4}; 
        int size = sizeof(arr1)/sizeof(arr1[0]); 
        int k = 3; 
        moreThanNdK(arr1, size, k); 

        cout << "\nSecond Test\n"; 
        int arr2[] = {4, 2, 2, 7}; 
        size = sizeof(arr2)/sizeof(arr2[0]); 
        k = 3; 
        moreThanNdK(arr2, size, k); 

        cout << "\nThird Test\n"; 
        int arr3[] = {2, 7, 2}; 
        size = sizeof(arr3)/sizeof(arr3[0]); 
        k = 2; 
        moreThanNdK(arr3, size, k); 

        cout << "\nFourth Test\n"; 
        int arr4[] = {2, 3, 3, 2}; 
        size = sizeof(arr4)/sizeof(arr4[0]); 
        k = 3; 
        moreThanNdK(arr4, size, k); 

        return 0; 
} 

Related Solutions

Instructions: The assignment is based on the mini case below. The instructions relating to the assignment...
Instructions: The assignment is based on the mini case below. The instructions relating to the assignment are at the end of the case. Katie Holmes and Sam Wilson are facing an important decision. After having discussed different financial scenarios into the wee hours of the morning, the two computer engineers felt it was time to finalize their cash flow projections and move to the next stage – decide which of two possible projects they should undertake. Both had a bachelor...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
Capital Budgeting Mini Case Instructions: The assignment is based on the mini case below. The instructions...
Capital Budgeting Mini Case Instructions: The assignment is based on the mini case below. The instructions relating to the assignment are at the end of the case. Samantha Groves and Harry Finch are facing an important decision. After having discussed different financial scenarios into the wee hours of the morning, the two computer engineers felt it was time to finalize their cash flow projections and move to the next stage – decide which of two possible projects they should undertake....
1. Convert the machine language instructions into assembly language instructions: 7976C1 06
1. Convert the machine language instructions into assembly language instructions: 7976C1 06
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1)...
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1) Declare a variable named myName equal to your first name //Firstname is Susan // Construct a basic IF statement that prints the variable to the // console IF the length of myName is greater than 1 // 2) Copy your IF statement from above and paste it below // Change the IF statement to check if the length of myName // is greater than...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
3. Capital Budgeting Assignment (15 marks) Instructions: The assignment is based on the case below. The...
3. Capital Budgeting Assignment Instructions: The assignment is based on the case below. The instructions relating to the assignment are at the end of the case. Dan and Susan are facing an important decision. After having discussed different financial scenarios, the two computer engineers felt it was time to finalize their cash flow projections and move to the next stage – decide which of two possible projects they should undertake. Both had a bachelor degree in engineering and had put...
Not sure where to begin on this c++ assignment. The instructions are to enclose the current...
Not sure where to begin on this c++ assignment. The instructions are to enclose the current contents of the print_output function into a try block that throws two different exceptions based on which line catches the bad input (shape or color). You will then print an appropriate message based on the type of exception that was thrown. this is what I have so far: #include <stdlib.h> #include <time.h> #include <stdio.h> #include <iostream> namespace color {        enum color{          red  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT