Beginners Task #2

Online C++ programming contests.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo

Postby merlinfire » Thu Jun 03, 2004 7:05 pm

I never really got any useful comments except splitting hairs :(


TEACH ME! IT'S YOUR JOB!

Wait.....
merlinfire
 
Posts: 28
Joined: Mon May 10, 2004 7:07 pm
Location: Fayetteville, OH

Postby Guest » Thu Jun 03, 2004 7:20 pm

merlinfire wrote:I never really got any useful comments except splitting hairs :(


TEACH ME! IT'S YOUR JOB!

Wait.....

I do not get it?
Guest
 

Postby merlinfire » Thu Jun 03, 2004 7:31 pm

Anonymous wrote:
merlinfire wrote:I never really got any useful comments except splitting hairs :(


TEACH ME! IT'S YOUR JOB!

Wait.....

I do not get it?



I meant comments about my solution to this problem.
merlinfire
 
Posts: 28
Joined: Mon May 10, 2004 7:07 pm
Location: Fayetteville, OH

Postby Guest » Thu Jun 03, 2004 7:59 pm

merlinfire wrote:I meant comments about my solution to this problem.

oh, no one really got any comments.

I did tested your code and if I put in a letter it loops forever and if 1 is entered the same. Not sure why but it also does not seem to end it just sits like is doing some thing but is not and it is not the system pause and I am not a fan of system pause.
Guest
 

Postby Guest » Thu Jun 03, 2004 8:24 pm

merlinfire wrote:I meant comments about my solution to this problem.

Code: Select all
  if ((y==x-1 || y==x) && prime !=1)
     {   
         cout << " " << x << endl;
         prime=1;
         // <-- missing statement here.
          }   

You forgot a statement in this block.
Guest
 

Postby merlinfire » Fri Jun 04, 2004 7:41 am

what did I miss? what should be there?
merlinfire
 
Posts: 28
Joined: Mon May 10, 2004 7:07 pm
Location: Fayetteville, OH

Postby merlinfire » Fri Jun 04, 2004 7:51 am

Oh, I see what I missed. Except for the non-number inputs, I added another control structure with no way to break out in the code -->
while (factorfest==0) . That has been fixed. I'm not sure how to check whether the input is a number or not...

Code: Select all
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    int oneleft=0;
    int x=0;
    int y=0;
    cout << "Factorial Prog 1.01." << endl;
    cout << "Enter your number to be decomposed for factors." << endl;
    cin >> x;
    cout << endl;
    int prime=0;
    int factorfest=0;
    while (prime==0)
       {       
        factorfest=0;
        y=1;
   while (factorfest==0)
        {     
         
       
        if (x==0 || x==1)
      {
      cout << "Your number is a " << x <<".  It cannot be factored.  What a ch00b."<< endl;
      prime =1;
      factorfest=1;
      }
 
       y++;
     if (x%y==0 && prime!=1)
     {
         cout << y <<" x ";
          x=x/y;
           factorfest=1;
}   
         
  if ((y==x-1 || y==x) && prime !=1)
     {   
         cout << " " << x << endl;
         prime=1;
          factorfest=1;
          }   
          }   
          } 
system("PAUSE");
  return 0;
}
merlinfire
 
Posts: 28
Joined: Mon May 10, 2004 7:07 pm
Location: Fayetteville, OH

Postby tomcant » Fri Jun 04, 2004 7:58 am

merlinfire wrote:I'm not sure how to check whether the input is a number or not...


isdigit(num);
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby merlinfire » Fri Jun 04, 2004 11:40 am

tomcant wrote:
merlinfire wrote:I'm not sure how to check whether the input is a number or not...


isdigit(num);


Thanks! Is there a particular library I must include for that function?
merlinfire
 
Posts: 28
Joined: Mon May 10, 2004 7:07 pm
Location: Fayetteville, OH

Postby Guest » Fri Jun 04, 2004 12:11 pm

merlinfire wrote:Oh, I see what I missed. Except for the non-number inputs, I added another control structure with no way to break out in the code -->
while (factorfest==0) . That has been fixed. I'm not sure how to check whether the input is a number or not...


You are welcome.
Guest
 

Postby tomcant » Fri Jun 04, 2004 12:34 pm

merlinfire wrote:
tomcant wrote:
merlinfire wrote:I'm not sure how to check whether the input is a number or not...


isdigit(num);


Thanks! Is there a particular library I must include for that function?


#include <cctype>
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Guest » Fri Jun 04, 2004 12:45 pm

tomcant did you post your solution in another thread because I can not seem to find yours? :lol:
Guest
 

Postby tomcant » Fri Jun 04, 2004 1:30 pm

I think, since I'm the host, I am not supposed to? But if you wanna see my version, sure...

Code: Select all
#include <iostream>

using namespace std;

int main()
{
   cout << "Enter a number to factor: ";
   
   int first = 2, val;
   cin >> val;   
   
   cout << "Prime factors = ";
   for(; val >= first*first; ++first)
   {
      for(; val % first == 0;)
      {
         cout << first;
         if(val > first) cout << " x ";
         val /= first;         
      }      
   }

   if(val > 1) cout << val;

   cin.ignore();
   cin.get();
   return(0);
}


Please comment :)
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby tomcant » Fri Jun 04, 2004 1:31 pm

w00ps.. forgot to add support for numbers lower than 4.... here we go:

Code: Select all
#include <iostream>

using namespace std;

int main()
{
   cout << "Enter a number to factor: ";
   
   int first = 2, val;
   cin >> val;   
   
   if(val <= 3)
   {
      cout << "Prime factors = " << val << endl;
      cin.ignore();
      cin.get();
      return(0);
   }
   
   cout << "Prime factors = ";
   for(; val >= first*first; ++first)
   {
      for(; val % first == 0;)
      {
         cout << first;
         if(val > first) cout << " x ";
         val /= first;         
      }      
   }

   if(val > 1) cout << val;

   cin.ignore();
   cin.get();
   return(0);
}
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Guest » Fri Jun 04, 2004 2:31 pm

tomcant wrote:I think, since I'm the host, I am not supposed to? But if you wanna see my version, sure...

Please comment :)


Oh, I thought that is what happened. I am sure it is alright as Johnny T posted his solution.

Very Nice solution. :D

If you care I did enter a letter in you solution and got this:
Enter a number to factor: k
Prime factors = -858993460
Guest
 

PreviousNext

Return to Contests

Who is online

Users browsing this forum: No registered users and 1 guest