Question

I don't know how to run this code on visual studios and I don't understand why...

I don't know how to run this code on visual studios and I don't understand why the answer is 211111118. Please explain! Thank you!

CONSIDER THE FOLLOWING CODE:
void Question()
{
      string x = "12";
      mystery1(x);
      string str1 = "21";
      mystery2(str1);
      string str2 = "11";
      mystery2(str2);
      string str3 = "31";
      mystery2(str3);
      string str4 = "91";
      mystery2(str4);
      string str5 = "81";
      mystery1(str5);
      x = x + str1 + str2 + str3 + str4 + str5;
      cout << x;
}
////////////////////////////////////////////////////////////
void mystery1(string &a)
{
      int num = a.length();
      string temp;
      for (int i = 0 ; i < num ; i++)
      {
            temp = a.at(i) + temp;
      }
      a = temp;

}
////////////////////////////////////////////////////////////
void mystery2(string &b)
{
      int num = b.length();
      string temp ="";
      for (int i = 0; i < num; i++)
      {
            if(b.at(i) == '1')
            temp = temp + b.at(i);
      }
      b = temp;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

FULL CODE

#include "stdafx.h"

#include <iostream>

using namespace std;

////////////////////////////////////////////////////////////

void mystery1(string &a)

{

int num = a.length();

string temp;

for (int i = 0 ; i < num ; i++)

{

temp = a.at(i) + temp;

}

a = temp;

}

////////////////////////////////////////////////////////////

void mystery2(string &b)

{

int num = b.length();

string temp ="";

for (int i = 0; i < num; i++)

{

if(b.at(i) == '1')

temp = temp + b.at(i);

}

b = temp;

}

void Question()

{

string x = "12";

mystery1(x);

string str1 = "21";

mystery2(str1);

string str2 = "11";

mystery2(str2);

string str3 = "31";

mystery2(str3);

string str4 = "91";

mystery2(str4);

string str5 = "81";

mystery1(str5);

x = x + str1 + str2 + str3 + str4 + str5;

cout << x;

}

int main() {

Question();

}

Explanation:

mystery1() function modifies the string passed to it as the argument to its reverse.

mystery2() function modifies the string passed to it as the argument such that it contains only 1's now.

Hence,

string x = "12";

mystery1(x); ------> x = "21"

string str1 = "21";

mystery2(str1); ------> str1= "1"

string str2 = "11";

mystery2(str2); ----> str2 = "11"

string str3 = "31";

mystery2(str3); ------> str3 = "1"

string str4 = "91";

mystery2(str4); -----> str4 = "1"

string str5 = "81";

mystery1(str5); ------> str5 = "18"

x = x + str1 + str2 + str3 + str4 + str5; -----> x = "21" + "1" + "11" + "1" + "1" + "18" = "211111118"

cout << x;

Add a comment
Know the answer?
Add Answer to:
I don't know how to run this code on visual studios and I don't understand why...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • when i run it is still be "false" i don't know why. could you help me...

    when i run it is still be "false" i don't know why. could you help me fix it and tell me which part is wrong??? specific answer is better thanks!!! Problem 6: (4 pts): Write a function that takes as an input parameter a string. It should return a Boolean value indicating whether the string is a palindrome or not. INPUT: “mom"->true “palindrome”->false “amanaplanpanama"->true 3 #include <iostream> #include <string.h> I using namespace std; string function(string str); int main() { function("palindrome");...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • CONSIDER THE FOLLOWING CODE: double SumString(string x, int n) {       double sum = 0;      ...

    CONSIDER THE FOLLOWING CODE: double SumString(string x, int n) {       double sum = 0;       for (int i = 0; i < n; i += 2)             sum += x.length();       return sum; } ///////////////////////////////////////////////////////// void Question() {       string a = "string";       int num = a.length();       double total = SumString(a, num);       cout << "The total is: " << total << endl; } Select one: a. The total is: 14.0 b. The total is: 20.0...

  • When I run this code use ./main How to fix it when program reminds segmentation fault...

    When I run this code use ./main How to fix it when program reminds segmentation fault (core dumped)? #include <iostream> void set_num(int* i_prt, int num) { *i_prt = num; } int main(int argc, char** argv) { int x; set_num(&x, 13); std::cout << "x: " << x << std::endl; int* y; set_num(y, 4); std::cout << "*y:" << *y << std::endl; }

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT