Question

In: Computer Science

(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...

  1. (8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions

If the current number is divisible by 2, then print CSU

If the current number is divisible by 5, then print SB

If the current number is divisible by both 2 and 5, then print CSUSB

If the number is neither divisible by 2 nor 5, then print the number

Example: 1 CSU 3 CSU SB CSU 7 CSU 9 CSUSB 11 CSU 13 CSU SB CSU 17 CSU 19 CSUSB …

  1. (4%) Implement a synchronized multithreaded version of CSUSB with four threads. The same instance of CSUSB (array of 1 to 50) will be passed to four different threads:
  • Thread 1 should call csu() to check if divisible by 2 then print CSU.
  • Thread 2 should call sb() to check if divisible by 5 then print SB.
  • Thread 3 should call csusb() to check if divisible by 2 and 5 then output CSUSB.
  • Thread 4 should call number() which should only print the numbers.
  • Submit your code for both parts as separate files (C, Java or CPP) and submit your reasoning on your results (MS word document) that should include brief descriptions of both parts. For multithreaded part, please include your reasoning on the code portion where synchronization is implemented and why. For help, please refer to the lectures and codes where we implemented Dining philosophers, and readers and writers problems using semaphores and pthread_cond_wait variables. You are given the choice between semaphores and pthread conditional wait variables whichever seems easier or convenient to you.

Solutions

Expert Solution

Here is the code for the above problem with the output snippet

#include <iostream>

using namespace std;

int main()
{
    int arr[50];
    int n;
    
    // ask the user to enter the size of the array
    cout<<"Enter the size of the array\n";
   
    // size of the array as input
    cin>>n;
    

    // loop to get the elements
    for(int i=0; i<n; i++)
    {
        cout<<"Enter element "<<i+1<<endl;
        cin>>arr[i];
    }
    
    
    // for loop to check for the conditions
    for(int i=0; i<n; i++)
    {

        // if the number is divisible by 2 check if it is also
        // divisible by 5

        if(arr[i]%2 == 0)
        {
            if(arr[i] %5 ==0)
            cout<<"CSUSB"<<endl;
            
            else
            cout<<"CSU"<<endl;
        }
        
        // ceck if the number is divible by 5
        else if(arr[i]%5 == 0)
        cout<<"SB"<<endl;
        
        // else statement if the number is not divisible by any of them
        else
        cout<<arr[i]<<endl;
    }

    return 0;
}

Here is the output snippet

I have given the solution for pnly first part as you have highlighted that only

Tell in the comments if you need solution for second part also


Related Solutions

C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT