Enable contrast version

Tutor profile: Juan M.

Inactive
Juan M.
Tutor for 2 Years in Intro to Mech E with High Experience in Programming
Tutor Satisfaction Guarantee

Questions

Subject: Java Programming

TutorMe
Question:

Develop a short Trivia Game that utilizes a Trivia Class Array such that it is scalable to contain as many questions and answers as the user desires.

Inactive
Juan M.
Answer:

import java.util.Scanner; public class TriviaGameV2 { private int ARRAY_SIZE = 5; private Trivia[] trivia; private int score; private int questionNum; public TriviaGameV2() { score = 0; questionNum = 0; trivia = new Trivia[ARRAY_SIZE]; trivia[0]=new Trivia("The first Pokemon that Ash recieves from Professor Oak", "pikachu", 1); trivia[1]=new Trivia("Erling Kagge skiied into here alone on January 7, 1933", "south pole", 2); trivia[2]=new Trivia("1997 British band that produced 'Tub Thumper'", "chumbawumba", 2); trivia[3]=new Trivia("Who is the tallest person on record (8ft. 11in) that has lived?", "robert wadlow", 3); trivia[4]=new Trivia("PT Barnum said \"This way to the ________\" to attract people to the exit.", "egress", 1); } public boolean askNextQuestion() { if(questionNum >= ARRAY_SIZE) return false; Scanner kb = new Scanner(System.in); System.out.println("\nQuestion " + (questionNum+1)); System.out.println(trivia[questionNum].getQuestion()); String userAns = kb.nextLine(); //Can use .equalsIgnoreCase() for later string comparisons if(userAns.toUpperCase().equals(trivia[questionNum].getAnswer().toUpperCase())) { System.out.println("That is correct!"); score += trivia[questionNum].getValue(); } else System.out.println("Wrong. The correct answer is " + trivia[questionNum].getAnswer()); questionNum++; return true; } public void showScore() { System.out.println("Your score is " + score); } }

Subject: Computer Science (General)

TutorMe
Question:

If a decision problem E is NP-complete, providing that X is reducible to Y in a polynomial time transformation, is this sufficient to show that Y is NP-Complete? (True or False, explain your reasoning)

Inactive
Juan M.
Answer:

FALSE X is an NP-Complete problem, and it is able to be transformed to Y in polynomial time. This satisfies the 2nd condition of a problem being NP-Complete which is for all other problems in NP, there is a transformation for them to become Y. Since X is in NP and is able to be transformed into Y, all NP problems may be transformed into Y in polynomial time. However, the first condition of being an NP-Complete problem is not met: X is not specifically stated to be an NP problem, that is it is not necessarily a decision problem. Thus, I must conclude that at the least Y is NP-Hard but not NP-Complete because Y is not stated to be a decision problem or to be an NP problem.

Subject: C++ Programming

TutorMe
Question:

Write out a program with 4 algorithms that help to find the smallest value in an array of any given size. Analyze the 4 algorithms to see which is the fastest.

Inactive
Juan M.
Answer:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iostream> #include <chrono> using namespace std; using namespace std::chrono; int MedianOfMedians5(int* A, int size); void printArray(int* A, int size) { for (int i = 0; i < size; i++) { std::cout << A[i] << " "; } std::cout << endl; } void printArray(unsigned int** A, int rows, int cols) { for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) std::cout << A[j][i] << ", "; std::cout << endl; } } void Merge(int* sortMe, int low, int mid, int high) { int sizeOfLeft = mid - low + 1; int sizeOfRight = high - mid; //Prepare memory allocations with their proper sizes int* leftArray = new int[sizeOfLeft]; int* rightArray = new int[sizeOfRight]; //Next, we fill the left and right array for (int i = 0; i < sizeOfLeft; i++) leftArray[i] = sortMe[low + i]; for (int j = 0; j < sizeOfRight; j++) rightArray[j] = sortMe[mid + 1 + j]; int i = 0, j = 0, k = low; //Merge temp arrays to real array, from smallest to biggest while (i < sizeOfLeft && j < sizeOfRight) { if (leftArray[i] <= rightArray[j]) { sortMe[k] = leftArray[i]; i++; } else { sortMe[k] = rightArray[j]; j++; } k++; } //Takes care of any extra elements that were missed, the largest ones while (i < sizeOfLeft) { sortMe[k] = leftArray[i]; i++; k++; } while (j < sizeOfRight) { sortMe[k] = rightArray[j]; j++; k++; } } void MergeSort(int* sortMe, int low, int high) { if (low < high) { //Create the mid for each iteration int mid = low + (high - low) / 2; //Divide the array into single parts MergeSort(sortMe, low, mid); MergeSort(sortMe, mid + 1, high); //Then we merge each part withe the following algorithm Merge(sortMe, low, mid, high); } } int Partition(int* pardonMe, int low, int high, int pivotPosition) { int pivot = pardonMe[low]; int leftCrawler = low; int temp = 0; for (int index = low + 1; index <= high; index++) { if (pardonMe[index] < pivot) { leftCrawler++; //Swap the two items temp = pardonMe[index]; pardonMe[index] = pardonMe[leftCrawler]; pardonMe[leftCrawler] = temp; } //printArray(pardonMe, (high + 1)); } //cout << "pivotPosition = " << pivotPosition << endl; pivotPosition = leftCrawler; //cout << "pivotPosition = " << pivotPosition << endl; //Swap the two items temp = pardonMe[low]; pardonMe[low] = pardonMe[pivotPosition]; pardonMe[pivotPosition] = temp; return pivotPosition; } int IterativeSelectK(int* pardonMe, int size, int key) { int lower = 0; //lower bound number int higher = size - 1; //higher bound number int pivotPosition = 0; //beginning pivot position, which is the first element bool keyNotFound = true; //boolean to control the iterations of the loop while (keyNotFound) { //Call Partition to organize the smallest and largest around the pivotPosition left and right respectively pivotPosition = Partition(pardonMe, lower, higher, 0); if (key == pivotPosition) { keyNotFound = false; return pardonMe[key]; } else if (key < pivotPosition) higher = pivotPosition - 1; else //if(key > pivotPosition) { lower = pivotPosition + 1; //key = key - pivotPosition; } } } int RecursiveSelectK(int* pardonMe, int lower, int higher, int key) { //Call Partition to organize the smallest and largest around the pivotPosition left and right respectively int pivotPosition = Partition(pardonMe, lower, higher, 0); if (key == pivotPosition) { return pardonMe[key]; } else if (key < pivotPosition) return RecursiveSelectK(pardonMe, lower, pivotPosition - 1, key); else //if(key > pivotPosition) return RecursiveSelectK(pardonMe, pivotPosition + 1, higher, key); } int RecurisveSelectMMK(int* pardonMe, int lower, int higher, int key) { if ((higher + 1) <= 5) RecursiveSelectK(pardonMe, lower, higher, key); int pivot = MedianOfMedians5(pardonMe, (higher + 1)); //Call Partition to organize the smallest and largest around the pivotPosition left and right respectively int pivotPosition = Partition(pardonMe, lower, higher, pivot); if (key == pivotPosition) { return pardonMe[key]; } else if (key < pivotPosition) return RecurisveSelectMMK(pardonMe, lower, pivotPosition - 1, key); else //if(key > pivotPosition) return RecurisveSelectMMK(pardonMe, pivotPosition + 1, higher, key); } int MedianOfMedians5(int* A, int size) { int chunkletCount = size / 5; int* Medians = new int[chunkletCount]; int* chunk = new int[5]; //Find the median of each 5 element chunk for (int index = 0; index < chunkletCount; index++) { Medians[index] = RecursiveSelectK(A, 5 * index, 5 * index + 4, (5*index + 2)); } return RecursiveSelectK(Medians, 0, chunkletCount - 1, chunkletCount / 2); } unsigned int** initializeMatrixU(int rows, int cols) { unsigned int** Q = new unsigned int* [rows]; for (int row = 0; row < rows; row++) { Q[row] = new unsigned int[cols]; for (int col = 0; col < cols; col++) { Q[row][col] = 0; } } return Q; } int* initializeArrayRandomly(int size) { int* Q = new int[size]; //Generate random seed srand(time(NULL)); for (int col = 0; col < size; col++) { Q[col] = rand() % 10; } return Q; } float averageTimes(unsigned int** time, int row, int cols) { float sum = 0; for (int col = 0; col < cols; col++) { sum += time[col][row]; } return sum / (float)cols; } int main() { int repetitions = 500; int n = 12; //int numKs = 5; unsigned int** MMTimeTaken = initializeMatrixU(repetitions, n); unsigned int** RecursiveTimeTaken = initializeMatrixU(repetitions, n); unsigned int** IterativeTimeTaken = initializeMatrixU(repetitions, n); unsigned int** MRGTimeTaken = initializeMatrixU(repetitions, (n-2)); int inputs[] = { 3, 5, 9, 17, 33, 65, 129, 257, 513, 1025, 2049, 4097}; int* plsSortMeMM; int* plsSortMeRR; int* plsSortMeII; int* plsSortMeMRG; int key; int findKey; for (int iterations = 0; iterations < repetitions; iterations++) { for (int i = 0; i < n; i++) { key = ((iterations / 100)/4) * (inputs[i] - 1); //if (i < 10) //{ plsSortMeMRG = initializeArrayRandomly(inputs[i]); //} plsSortMeMM = initializeArrayRandomly(inputs[i]); plsSortMeRR = initializeArrayRandomly(inputs[i]); plsSortMeII = initializeArrayRandomly(inputs[i]); //printArray(plsSortMe, size); //key = 0; //cout << "\nFinding Position Key: " << key << endl; auto MMstart = high_resolution_clock::now(); findKey = RecurisveSelectMMK(plsSortMeMM, 0, inputs[i] - 1, key); auto MMstop = high_resolution_clock::now(); //cout << "\nThe element of plsSortMe[" << key << "] = " << findKey << endl; auto MMduration = duration_cast<microseconds>(MMstop - MMstart); MMTimeTaken[iterations][i] = MMduration.count(); //cout << "\nFinding Position Key: " << key << endl; auto RRstart = high_resolution_clock::now(); findKey = RecursiveSelectK(plsSortMeRR, 0, inputs[i] - 1, key); auto RRstop = high_resolution_clock::now(); //cout << "\nThe element of plsSortMe[" << key << "] = " << findKey << endl; auto RRduration = duration_cast<microseconds>(RRstop - RRstart); RecursiveTimeTaken[iterations][i] = RRduration.count(); //cout << "\nFinding Position Key: " << key << endl; auto IIstart = high_resolution_clock::now(); findKey = IterativeSelectK(plsSortMeII, inputs[i], key); auto IIstop = high_resolution_clock::now(); //cout << "\nThe element of plsSortMe[" << key << "] = " << findKey << endl; auto IIduration = duration_cast<microseconds>(IIstop - IIstart); IterativeTimeTaken[iterations][i] = IIduration.count(); //if (i < 10) //{ //cout << "\nFinding Position Key: " << key << endl; auto MRGstart = high_resolution_clock::now(); MergeSort(plsSortMeMRG, 0, inputs[i] - 1); findKey = plsSortMeMRG[key]; auto MRGstop = high_resolution_clock::now(); //cout << "\nThe element of plsSortMe[" << key << "] = " << findKey << endl; auto MRGduration = duration_cast<microseconds>(MRGstop - MRGstart); MRGTimeTaken[iterations][i] = MRGduration.count(); //} } } std::cout << "The average times for input sizes for Recursive MM Partition Select K is: " << endl; for (int i = 0; i < n; i++) { std::cout << averageTimes(MMTimeTaken, i, n) << " "; } std::cout << endl << endl; std::cout << "The average times for input sizes for Recursive Partition Select K is: " << endl; for (int i = 0; i < n; i++) { std::cout << averageTimes(RecursiveTimeTaken, i, n) << " "; } std::cout << endl << endl; std::cout << "The average times for input sizes for Iterative Partition Select K is: " << endl; for (int i = 0; i < n; i++) { std::cout << averageTimes(IterativeTimeTaken, i, n) << " "; } std::cout << endl << endl; std::cout << "The average times for input sizes (minus last 2) for Merge Sort Select K is: " << endl; for (int i = 0; i < n; i++) { std::cout << averageTimes(MRGTimeTaken, i, n) << " "; } std::cout << endl << endl; return 0; }

Contact tutor

Send a message explaining your
needs and Juan will reply soon.
Contact Juan

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.
BEST IN CLASS SINCE 2015
TutorMe homepage
Made in California by Zovio
© 2013 - 2022 TutorMe, LLC
High Contrast Mode
On
Off