Computer Science Canada

Error excecuting?

Author:  Flikerator [ 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.

Author:  Monstrosity_ [ 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.."?

Author:  Flikerator [ 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... = (

Author:  Monstrosity_ [ 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.

Author:  Tony [ Mon Nov 07, 2005 12:54 pm ]
Post 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

Author:  wtd [ Mon Nov 07, 2005 1:02 pm ]
Post subject: 

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

Author:  wtd [ Mon Nov 07, 2005 1:14 pm ]
Post 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.

Author:  wtd [ Mon Nov 07, 2005 1:30 pm ]
Post 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;
}

Author:  wtd [ Mon Nov 07, 2005 1:51 pm ]
Post 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;
}

Author:  Flikerator [ Tue Nov 08, 2005 8:41 am ]
Post subject: 

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

Thanks a lot for the help.

Author:  wtd [ Tue Nov 08, 2005 1:09 pm ]
Post 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.

Author:  Flikerator [ Wed Nov 09, 2005 9:17 am ]
Post 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?

Author:  wtd [ Wed Nov 09, 2005 1:28 pm ]
Post subject: 

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

Author:  [Gandalf] [ Wed Nov 09, 2005 4:19 pm ]
Post 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?

Author:  Flikerator [ Thu Nov 10, 2005 7:19 pm ]
Post 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

Author:  wtd [ Thu Nov 10, 2005 7:26 pm ]
Post subject: 

Flikerator wrote:
Also can't you access the graphics card with C++?


Theoretically. Do you think you can write better code than the people who wrote the drivers for the card?

Author:  md [ Thu Nov 10, 2005 8:38 pm ]
Post subject: 

wtd wrote:
Flikerator wrote:
Also can't you access the graphics card with C++?


Theoretically. Do you think you can write better code than the people who wrote the drivers for the card?


In practicality your program runs in it's own virtual memory, and no matter how hard you try you simply can't write to the real video memory without a lot of work (ei. implementing your own driver). It's by far simpler to use a graphics library to do any outputting.

Also Flikerator, although there is a sort function in the standard libraries it is not by any means part of the language; language and library are a key distinction.

Author:  Flikerator [ Fri Nov 11, 2005 4:57 pm ]
Post subject: 

Im a little confused. Whats the point of a graphics card if you can't use it to run games??? I thought you could write code that draws on your graphics card to handle the pictures, 3D models, ext instead of your RAM doing it Question

Author:  wtd [ Fri Nov 11, 2005 5:18 pm ]
Post subject: 

Flikerator wrote:
Im a little confused. Whats the point of a graphics card if you can't use it to run games??? I thought you could write code that draws on your graphics card to handle the pictures, 3D models, ext instead of your RAM doing it Question


And so you can.

By calling functions from libraries that do that for you.

Here's the real question: why should you be concerned about where the execution takes place? Shouldn't you just write the logic of your program and let the libraries decide where the execution should take place?

Author:  rizzix [ Fri Nov 11, 2005 6:31 pm ]
Post subject: 

Flikerator wrote:
Im a little confused. Whats the point of a graphics card if you can't use it to run games??? I thought you could write code that draws on your graphics card to handle the pictures, 3D models, ext instead of your RAM doing it Question


OpenGL / directx libraries usually automatically interact with your graphics card.. through the installed vendor-drivers

Author:  Flikerator [ Fri Nov 11, 2005 7:00 pm ]
Post subject: 

Well yah thats what I meant by using the graphics card, through the libraries in Dev. Thats why I was a little confused Confused

Author:  wtd [ Fri Nov 11, 2005 7:07 pm ]
Post subject: 

Flikerator wrote:
Well yah thats what I meant by using the graphics card, through the libraries in Dev. Thats why I was a little confused Confused


You'll have to forgive us. When you said that C and C++ could access the graphics card and treated it like something special, we naturally thought you meant something veyr low-level, since OpenGL programs can be written in many languages.

Author:  md [ Fri Nov 11, 2005 10:03 pm ]
Post subject: 

wtd wrote:
Flikerator wrote:
Well yah thats what I meant by using the graphics card, through the libraries in Dev. Thats why I was a little confused Confused


You'll have to forgive us. When you said that C and C++ could access the graphics card and treated it like something special, we naturally thought you meant something veyr low-level, since OpenGL programs can be written in many languages.

In the good old days of DOS in order to take advantage of different video cards you really did have to write you own drivers, and one for every card you wanted to support too. Great fun! Now thanks to OpenGL and DirectX things are actually mostly painless and fairly standardized over all graphics cards.

If you're really interested in how video cards render things in 3d, C++ and C are both really good for writing your own renderer in (thanks to the ease of pointer manipulation); the only hard part is that you need to use other libraries to draw the final image in the end.

Author:  wtd [ Fri Nov 11, 2005 10:07 pm ]
Post subject: 

Pointers aren't necessary. Such things can be done in languages which don't have user-accessible pointers.

Author:  md [ Fri Nov 11, 2005 10:59 pm ]
Post subject: 

Yes, I didn't say you couldn't do it without pointers... only that pointers make certain things really nice and quick. Dynamically allocated arrays are also nice (although I'm sure they are available in these other languages too)

Author:  Flikerator [ Sat Nov 12, 2005 10:46 am ]
Post subject: 

Thats one of the reasons I switched from turing because it couldn't use the graphics card. Im still a long way from that though, I still havn't used vectors, or pointers ext..[/b]


: