Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Error excecuting?
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: 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 <iostream>

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.
Sponsor
Sponsor
Sponsor
sponsor
Monstrosity_




PostPosted: 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




PostPosted: 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_




PostPosted: 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




PostPosted: 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 Laughing
wtd




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

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




PostPosted: 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




PostPosted: 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 <iostream>
#include <vector>

std::vector<int> find_factors(int);

int main()
{
   std::vector<int> factors(find_factors(42));

   for (std::vector<int>::iterator factors_iter(factors.begin());
        factors_iter != factors.end();
        factors_iter++)
   {
      std::cout << *factors_iter << std::endl;
   }

   return 0;
}

std::vector<int> find_factors(int input_number)
{
   std::vector<int> factors;

   for (int factor_candidate(2);
        factor_candidate < input_number;
        ++factor_candidate)
   {
      bool multiple_of_previous_factor(false);

      for (std::vector<int>::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;
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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 <iostream>
#include <list>
#include <algorithm>

std::list<int> find_factors(int);
std::list<int> shared_factors(int, int);

int main()
{
   std::list<int> factors(shared_factors(42, 27));

   for (std::list<int>::iterator factors_iter(factors.begin());
        factors_iter != factors.end();
        factors_iter++)
   {
      std::cout << *factors_iter << std::endl;
   }

   return 0;
}

std::list<int> find_factors(int input_number)
{
   std::list<int> factors;

   for (int factor_candidate(2);
        factor_candidate < input_number;
        ++factor_candidate)
   {
      bool multiple_of_previous_factor(false);

      for (std::list<int>::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<int> shared_factors(int number1, int number2)
{
   std::list<int> factors_of_number1(find_factors(number1)),
                    factors_of_number2(find_factors(number2)),
                    output;

   for (std::list<int>::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<int>::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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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]




PostPosted: 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 Smile).

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




PostPosted: 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 Smile).

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 Twisted Evil

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 ^^ Very Happy
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 26 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: