I need this program in C++ language. Already have everything. Write the followin
ID: 3819496 • Letter: I
Question
I need this program in C++ language. Already have everything.
Write the following. Include a type declaration for each value or function. You should not define a function with one word (because there ha to be an identical function built in) or define a value directly (instead of constructing it with, e.g., a list comprehension) 1. A function "duplicates that takes a list and returns true if any t wo elements in the list are the samc. 2. A list of pairs 'rolls' showing all possible rolls of two six-sided dice. 3. A function "counts that takes a list and returns a list of pairs, with each pair containing an element and the number of times it appears. 4. A list of pairs 'sums', showing how often each sum appears when rolling two six-sided dice.
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Test test = new Test();
//create list having duplicate elements
List<String> list = Arrays.asList(new String[] {"A","B","C","A"});
System.out.println("Check duplicacy in list: ");
System.out.println(test.checkDuplicates(list));
System.out.println("All possible dice rolls are: ");
test.allPossibleDiceRolls("", 2);
System.out.println("Count digit frequency: ");
test.countDigitFrequency(new int[] {1,2,3,1,1,1,2,2,3,4,6,5,5,5});
System.out.println("Number appears in dice");
Map<Integer, Integer> map = test.getDiceMapOfNumberList(new int[] {4,7,8,5});
Set<Integer> keys = map.keySet();
for(int key : keys) {
System.out.println("number " +key+" comes => " + map.get(key) + " times" );
}
}
public <T> boolean checkDuplicates(List<T> list) {
Set<T> set = new HashSet<T>();
for(T data : list)
if(!set.add(data))
return true;
return false;
}
public void allPossibleDiceRolls(String s, int depth) {
if(depth == 0)
System.out.println(s.substring(1));
else
for(int i = 1; i <= 6; i++)
allPossibleDiceRolls(s + "," + i, depth - 1);
}
public void countDigitFrequency(int x[]) {
HashMap<Integer,Integer>digits=new HashMap<Integer,Integer>();
for (int i : x){
if (digits.containsKey(i)){
digits.put(i, digits.get(i)+1);
} else {
digits.put(i, 1);
}
}
for (int key:digits.keySet()){
System.out.println(key+" comes "+digits.get(key) + " times");
}
}
private int numberPossibilitiesInRollingDices(int numberOfDice, int sum) {
if (numberOfDice == sum)
return 1;
else if (numberOfDice == 0 || sum < numberOfDice)
return 0;
else
return numberPossibilitiesInRollingDices(numberOfDice, sum - 1) +
numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 1) -
numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 7);
}
public Map<Integer, Integer> getDiceMapOfNumberList(int[] numbers) {
Map<Integer, Integer> diceMap = new HashMap<Integer, Integer>();
for(int num : numbers) {
diceMap.put(num,numberPossibilitiesInRollingDices(2, num));
}
return diceMap;
}
}
Explanation / Answer
Below is the program you requeted . Hope it helped you.
jav.h
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <iostream>
class Test
{
static void main(std::vector<std::wstring> &args);
public:
template<typename T>
bool checkDuplicates(std::vector<T> &list);
virtual void allPossibleDiceRolls(const std::wstring &s, int depth);
virtual void countDigitFrequency(std::vector<int> &x);
private:
int numberPossibilitiesInRollingDices(int numberOfDice, int sum);
public:
virtual std::unordered_map<int, int> getDiceMapOfNumberList(std::vector<int> &numbers);
};
jav.cpp
#include "jav.h"
void Test::main(std::vector<std::wstring> &args)
{
Test *test = new Test();
//create list having duplicate elements
std::vector<std::wstring> list = Arrays::asList(std::vector<std::wstring> {L"A",L"B",L"C",L"A"});
std::wcout << std::wstring(L"Check duplicacy in list: ") << std::endl;
std::wcout << test->checkDuplicates(list) << std::endl;
std::wcout << std::wstring(L"All possible dice rolls are: ") << std::endl;
test->allPossibleDiceRolls(L"", 2);
std::wcout << std::wstring(L"Count digit frequency: ") << std::endl;
test->countDigitFrequency(std::vector<int> {1,2,3,1,1,1,2,2,3,4,6,5,5,5});
std::wcout << std::wstring(L"Number appears in dice") << std::endl;
std::unordered_map<int, int> map = test->getDiceMapOfNumberList(std::vector<int> {4,7,8,5});
Set<int> *keys = map.keySet();
for (auto key : keys)
{
std::wcout << std::wstring(L"number ") << key << std::wstring(L" comes => ") << map[key] << std::wstring(L" times") << std::endl;
}
}
template<typename T>
bool Test::checkDuplicates(std::vector<T> &list)
{
Set<T> *set = std::unordered_set<T>();
for (auto data : list)
{
if (!set->add(data))
{
return true;
}
}
return false;
}
void Test::allPossibleDiceRolls(const std::wstring &s, int depth)
{
if (depth == 0)
{
std::wcout << s.substr(1) << std::endl;
}
else
{
for (int i = 1; i <= 6; i++)
{
allPossibleDiceRolls(s + std::wstring(L",") + std::to_wstring(i), depth - 1);
}
}
}
void Test::countDigitFrequency(std::vector<int> &x)
{
std::unordered_map<int, int> digits;
for (auto i : x)
{
if (digits.find(i) != digits.end())
{
digits[i] = digits[i] + 1;
}
else
{
digits[i] = 1;
}
}
for (auto key : digits)
{
std::wcout << key.first << std::wstring(L" comes ") << digits[key.first] << std::wstring(L" times") << std::endl;
}
}
int Test::numberPossibilitiesInRollingDices(int numberOfDice, int sum)
{
if (numberOfDice == sum)
{
return 1;
}
else if (numberOfDice == 0 || sum < numberOfDice)
{
return 0;
}
else
{
return numberPossibilitiesInRollingDices(numberOfDice, sum - 1) + numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 1) - numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 7);
}
}
std::unordered_map<int, int> Test::getDiceMapOfNumberList(std::vector<int> &numbers)
{
std::unordered_map<int, int> diceMap;
for (auto num : numbers)
{
diceMap[num] = numberPossibilitiesInRollingDices(2, num);
}
return diceMap;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.