Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Error excecuting?
Author Message
Flikerator

Posted: Fri Nov 04, 2005 9:40 am   Post subject: Error excecuting?

Near like the beggining my program just crapped out and after I did the input it shut down saying the program was succesful. Im not entirely sure why though. I wrote the rest of the program anyways, without being able to test it. Any help would be appreciated.

The program is supposed to find all the factors in both numbers and compare to see how many of those numbers are the same. Then it checks if they have 2 or more numbers with the same factors, and reports it.

 code: #include int main() {     int num1, num2, count,numHold = 0;     std::cout << "Enter the first number: ";     std::cin >> num1;     std::cin.ignore();     std::cout << "Enter the second number: ";     std::cin >> num2;     std::cin.ignore();         if (num1 > num2){        numHold = num1;              }     else{          numHold = num2;     }     int fact[numHold][2];         for (int i=1; i < 2; i = i + 1){         for (int ii=1; i < numHold+1; ii = ii + 1){             fact[ii][i] = 0;            }        }         for (int i = 1; i < num1+1; i = i+1){         if (num1 % i < 1){             fact[i][1] = 1;                }     }     for (int i = 1; i < num1+1; i = i + 1){         std::cout << fact[i][1] << " ";        }     std::cout << std::endl;     for (int i = 1; i < num2+1; i = i+1){         if (num2 % i < 1){             fact[i][2] = 1;                }     }     for (int i = 1; i < num2+1; i = i + 1){         std::cout << fact[i][2] << " ";        }     for (int i = 1; i < numHold+1; i = i + 1){         if (fact[i][1] = 1){            if (fact [i][2] = 1){               count = count + 1;               }                    }     }     if (count > 2){        std::cout << "The two entered numbers are not mutually prime: " << count;              }     else{          std::cout << "The two entered numbers are mutually prime: " << count;          }     std::cout << count;     std::cin.get(); }

Its the longest code I have so far, most of it is the same stuff, and it could probably be reduced. This is the turing version I did back in september;

 code: var num, num2, count : int := 0 put "Enter the first number: " .. get num put "Enter the second number: " .. get num2 var fact : array 1 .. max (num, num2), 1 .. 2 of int    %The highest number has the factors for ii : 1 .. 2     for i : 1 .. upper (fact)  %Gives ALL the arrays a value  of 0         fact (i, ii) := 0     end for end for %2D array, 1..2 is for both numbers. Just practising arrays. Not nessisary =D. for i : 1 .. num     if num mod i = 0 then         fact (i, 1) := 1        %Change this to i for a visual     end if end for for i : 1 .. upper (fact)     put fact (i, 1), " " .. end for put "" for i : 1 .. num2     if num2 mod i = 0 then         fact (i, 2) := 1        %Change this ti i for a visual     end if end for for i : 1 .. upper (fact)     put fact (i, 2), " " .. end for put "" for i : 1 .. upper (fact)     if fact (i, 1) ~= 0 and fact (i, 2) ~= 0 then         count += 1     end if end for if count > 2 then     put "The numbers are not mutually prime (", count, " matching factors)" else     put "The number is mutually prime" end if

Not my best example of my turing skills, but there it is anyway.

Monstrosity_

Posted: Sat Nov 05, 2005 12:42 pm   Post subject: Re: Error excecuting?

Flikerator wrote:
.. I wrote the rest of the program anyways, without being able to test it...

At a quick glance it looks to be all right, but the thing the confuses me is that the code you pasted isnt the code you were having a problem with. Does the code you gave have the same problem as the unfinished one? When it says it finished successfully does it print your message "The two entered numbers are.."?
Flikerator

Posted: Mon Nov 07, 2005 11:15 am   Post subject: Re: Error excecuting?

Monstrosity_ wrote:
Flikerator wrote:
.. I wrote the rest of the program anyways, without being able to test it...

At a quick glance it looks to be all right, but the thing the confuses me is that the code you pasted isnt the code you were having a problem with. Does the code you gave have the same problem as the unfinished one? When it says it finished successfully does it print your message "The two entered numbers are.."?

No it doesn't print out "The two entered numbers are" line...Thats the problem. After it gets the input it just closes the window... = (
Monstrosity_

Posted: Mon Nov 07, 2005 12:27 pm   Post subject: Re: Error excecuting?

Does C++ allow variable length arrays?
And in every loop there, your stepping out of the bounds of your array.
Tony

Posted: Mon Nov 07, 2005 12:54 pm   Post subject: (No subject)

you should be running it in console, not a window

I don't think C++ allows run-time determened array length. You could use linked lists instead
wtd

Posted: Mon Nov 07, 2005 1:02 pm   Post subject: (No subject)

Sure it does, but that should be done via dynamic memory allocation.
wtd

Posted: Mon Nov 07, 2005 1:14 pm   Post subject: (No subject)

Ok... here's what you need to do:

You have first one task: find all of the factors of a given number. That is a function.

Once you can do that for any given number, it's a simple matter to compare the factors of two numbers.
wtd

Posted: Mon Nov 07, 2005 1:30 pm   Post subject: (No subject)

Let's look at a pretty basic function which does just that... finds all of the factors of a number.

 c++: #include #include std::vector find_factors(int); int main() {    std::vector factors(find_factors(42));    for (std::vector::iterator factors_iter(factors.begin());         factors_iter != factors.end();         factors_iter++)    {       std::cout << *factors_iter << std::endl;    }    return 0; } std::vector find_factors(int input_number) {    std::vector factors;    for (int factor_candidate(2);         factor_candidate < input_number;         ++factor_candidate)    {       bool multiple_of_previous_factor(false);       for (std::vector::iterator factors_iter(factors.begin());            factors_iter != factors.end();            factors_iter++)       {                   if (factor_candidate % *factors_iter == 0)                   {                          multiple_of_previous_factor = true;                          break;                   }           }           if (!multiple_of_previous_factor && input_number % factor_candidate == 0)           {                  factors.push_back(factor_candidate);           }    }    return factors; }

wtd

Posted: Mon Nov 07, 2005 1:51 pm   Post subject: (No subject)

Made some modifications for the comparison part. Using "list" instead of "vector" to get "push_front".

 c++: #include #include #include std::list find_factors(int); std::list shared_factors(int, int); int main() {    std::list factors(shared_factors(42, 27));    for (std::list::iterator factors_iter(factors.begin());         factors_iter != factors.end();         factors_iter++)    {       std::cout << *factors_iter << std::endl;    }    return 0; } std::list find_factors(int input_number) {    std::list factors;    for (int factor_candidate(2);         factor_candidate < input_number;         ++factor_candidate)    {       bool multiple_of_previous_factor(false);       for (std::list::iterator factors_iter(factors.begin());            factors_iter != factors.end();            factors_iter++)       {                   if (factor_candidate % *factors_iter == 0)                   {                          multiple_of_previous_factor = true;                          break;                   }           }           if (!multiple_of_previous_factor && input_number % factor_candidate == 0)           {                  factors.push_back(factor_candidate);           }    }    factors.push_front(1);    factors.push_back(input_number);    return factors; } std::list shared_factors(int number1, int number2) {    std::list factors_of_number1(find_factors(number1)),                     factors_of_number2(find_factors(number2)),                     output;    for (std::list::iterator first_number_factors_iter(factors_of_number1.begin());         first_number_factors_iter != factors_of_number1.end();         first_number_factors_iter++)    {           if (std::find(factors_of_number2.begin(),                         factors_of_number2.end(),                         *first_number_factors_iter)               != factors_of_number2.end())           {          output.push_back(*first_number_factors_iter);           }    }    for (std::list::iterator second_number_factors_iter(factors_of_number2.begin());         second_number_factors_iter != factors_of_number2.end();         second_number_factors_iter++)    {           if (std::find(output.begin(), output.end(), *second_number_factors_iter)               == output.end()                   &&                   std::find(factors_of_number1.begin(),                         factors_of_number1.end(),                         *second_number_factors_iter)               != factors_of_number1.end())           {          output.push_back(*second_number_factors_iter);           }    }    return output; }
Flikerator

Posted: Tue Nov 08, 2005 8:41 am   Post subject: (No subject)

Good lots of new stuff to learn. I got vectors, <list>, <algorithm>, functions, ext..

Thanks a lot for the help.
wtd

Posted: Tue Nov 08, 2005 1:09 pm   Post subject: (No subject)

You're quite welcome. There's a lot to learn, but hey... that's C++ for ya, and trying to treat C++ like "Turing with curly braces" means you're going to write code that doesn't take advantage of the advantages C++ does have.
Flikerator

Posted: Wed Nov 09, 2005 9:17 am   Post subject: (No subject)

wtd wrote:
You're quite welcome. There's a lot to learn, but hey... that's C++ for ya, and trying to treat C++ like "Turing with curly braces" means you're going to write code that doesn't take advantage of the advantages C++ does have.

Well right now im doing all the excericises we have in class, and doing them all in C++, because im done them all in turing. So far turing is vastly inferior to C++ and ive just started. Its nice to have a programming language that I can actually use "in the real world".

Anyways does anyone know why my original program for this didn't work?
wtd

Posted: Wed Nov 09, 2005 1:28 pm   Post subject: (No subject)

Run it from an existing command prompt, and remove all of the pause hacks.
[Gandalf]

Posted: Wed Nov 09, 2005 4:19 pm   Post subject: (No subject)

By that wtd means all the cin.ignore()s and the cin.get()s (just in case you did not know ).

Out of curiosity, what are some of the things you have found in C++ to be so far superior to Turing?
Flikerator

Posted: Thu Nov 10, 2005 7:19 pm   Post subject: (No subject)

[Gandalf] wrote:
By that wtd means all the cin.ignore()s and the cin.get()s (just in case you did not know ).

Out of curiosity, what are some of the things you have found in C++ to be so far superior to Turing?

Way faster, built in sort. Libraries full of things, im just starting out though and don't know that much about them. Also can't you access the graphics card with C++? Which makes my plans for a game possible

Note: I don't plan to make a game for a LONG while, untill Im elite at C++ at least. Im not geared toward just things for games in C++ either.

EDIT - Oh I almost forgot, Thanks for the help ^^
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 2  [ 26 Posts ]
Goto page 1, 2  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: