View the step-by-step solution to:

Question

Hello, trying to write program that uses a stack to print prime factors of a positive integer in descending order.

It has to run on Visual studios but every help I have gotten it only works on other IDE's. The requirements are to make it work only using VS debug. not in command prompt or anything else. I have been having difficulty figuring out how to use stack in my current code. So how would I add stack to my code and it run in visual studios?



#include <iostream>

#include <vector>

#include <stack>


using namespace std;


vector<int> factors(int n) {


   vector<int> result;

   int a = 2;


   while (a * a <= n)

   {

      if (n % a == 0)

      {

         result.push_back(a);

         n = (n / a);

      }

      else

      {

         a++;

      }

   }

   if (n > 1)

      result.push_back(a);

   return result;

}


int main()

{


   int x;


   cout << "Input positive integer greater than 1: ";


   cin >> x;


   vector<int> result_factors = factors(x);

   cout << "The result: ";

   for (int i : result_factors)

      cout << "i ";

   cout << endl;


   return 0;

}

Top Answer

View the full answer
Screenshot (59).png

File
Edit
View Project
Build Debug
Test
Analyze
Tools Extensions Window Help
Search Visual Studio (Ctrl+Q)
ConsoleApplication1
MM
X
Debug
X86
Local Windows Debugger
Live Share
Server Explorer...

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online