Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Continuing to get 0 output on this code, getting near a zero everytime I run it but I know my tests are right, Help?

Continuing to get 0 output on this code, getting near a zero everytime I run it but I know my tests are right, Help?

 

#include
#include
#include

using namespace std;

class Submission
{
public:

/* TODO: Test 1
* ========================
* Given an array of integers, and an integer index,
* return the item at the given index of the array.
*
* Example input:
* - [ 2, 4, 6, 8, 10 ]
* - 3
*
* Example output:
* - 8
*/
static int Test1(int numbers[], int index)
{
 return numbers[index];
}
/* TODO: Test 2
* ========================
* Given an array of integers, and an integer index,
* replace the item at the given index with a 0.
*
* Tips:
* - This method has a void return type. This is
*   because arrays are a reference-type variable,
*   meaning that any changes made to the array within
*   the method will also be made to the original array
*   outside of the method.
*
* Example input:
* - [ 2, 4, 6, 8, 10 ]
* - 3
*
* Example output:
* - [ 2, 4, 6, 0, 10 ]
*/
static void Test2(int numbers[5], int index)
{
 numbers[index] = 0;
}

/* TODO: Test 3
* ========================
* Given a vector of strings, determine whether or
* not the vector contains duplicate strings. If it
* does, return true. Otherwise, return false.
*
* Example input:
* - { "Mario", "Mario", "Miss Fortune", "Echo" }
*
* Example output:
* - true
*/
static bool Test3(vector names) {
 (names.begin(), names.end());
 for (size_t i = 1; i < names.size(); ++i) {
  if (names[i] == names[i - 1]) {
   return true; // Found a duplicate
  }
 }
 return false; // No duplicate found
}


/* TODO: Test 4
* ========================
* Given an vector of integers, and an integer index,
* return the item at the given index of the array using
* the at() method.
*
* Example input:
* - { 2, 4, 6, 8, 10 }
* - 3
*
* Example output:
* - 8
*/
static int Test4(vector numbers, int index)
{
 return numbers.at(index);
}

/* TODO: Test 5
* ========================
* Given 5 integers, place each into an integer vector
* using the push_back() method and return the created
* vector.
*
* Example input:
* - 5
* - 7
* - 9
* - 10
* - 15
*
* Example output:
* - { 5, 7, 8, 10, 15 }
*/
static vector Test5(int number1, int number2, int number3, int number4, int number5)
{
 vector result = { number1, number2, number3, number4, number5 };
 return result;
}

/* TODO: Test 6
* ========================
* Given 4 strings, place each into a string vector
* in order without using the push_back() method and
* return the created vector.
*
* Example input:
* - Mario
* - Master Chief
* - Tracer
* - Ezio Auditore
*
* Example output:
* - { "Tracer", "Mario", "Ezio Auditore", "Master Chief" }
*/
static vector Test6(string word2, string word4, string word1, string word3)
{
 vector result;
 result = { word3, word1, word4, word2 };
 return result;
}

/* TODO: Test 7
* ========================
* Convert a given char array into an integer vector
*
* Example input:
* -  [ t, c, q, v, j ]
*
* Example output:
* - { 116, 99, 113, 118, 106 }
*/
static vector Test7(char letters[])
{
 vector result;
 for (int i = 0; letters[i] != '\0'; ++i) {
  result.push_back(static_cast(letters[i]));
 }
 return result;
}

/* TODO: Test 8
* ========================
* Given an array of capital letters,
* change the array so that every other
* letter is lowercase.
*
* Tips:
* - There are several solutions to this
*   test. For example, one involves subtraction
*   while another requires using the tolower() method.
*   You may use either solution.
*
* References:
* - https://cplusplus.com/reference/locale/tolower/
* - https://www.geeksforgeeks.org/tolower-function-in-cpp/
*
* Example input:
* - [ L, V, F, D, C ]
*
* Example output:
* - [ L, v, F, d, C ]
*/
static void Test8(char letters[])
{
 for (int i = 1; letters[i] != '\0'; i += 2) {
  if (isupper(letters[i])) {
   letters[i] = tolower(letters[i]);
  }
 }
}

/* TODO: Test 9
* ========================
* Given 3 integer vectors, return a single 2D vector.
*
* Example input:
* - [ 4, 0, 9, 7, 6 ]
* - [ 43, 31, 48, 29, 40 ]
* - [ 2, 5, 5, 2, 1 ]
*
* Example output:
* - 4 43 2
*   0 31 5
*   9 48 5
*   7 29 2
*   6 40 1
*/
static vector> Test9(vector mins, vector maxes, vector seeds)
{
 vector> result;
 for (size_t i = 0; i < mins.size(); ++i) {
  vector row = { mins[i], maxes[i], seeds[i] };
  result.push_back(row);
 }

 return result;
}

/* TODO: Test 10
* ========================
* Given a 2D vector int integers,
* return a new 2D vector transposed.
*
* Example input:
* - 5 1 5
*   4 9 6
*
* Example output:
* - 5 4
*   1 9
*   5 6
*/
static vector> Test10(vector> numbers)
{
 if (numbers.empty() || numbers[0].empty()) {
  return {};
 }

 size_t rows = numbers.size();
 size_t cols = numbers[0].size();

 vector> result(cols, vector(rows));

 for (size_t i = 0; i < rows; ++i) {
  for (size_t j = 0; j < cols; ++j) {
   result[j][i] = numbers[i][j];
  }
 }

 return result;
}
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Lets dissect the provided code stepbystep and identify any issues that might be causing you to get a nearzero output consistently Typically nearzero or zero outputs often indicate logical errors misun... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Communication Essentials a skill based approach

Authors: Courtland L. Bovee, John V. Thill

6th edition

978-0132971324

More Books

Students also viewed these Algorithms questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

What is quality of work life ?

Answered: 1 week ago

Question

How does cultural context affect communication?

Answered: 1 week ago