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 <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
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 <iostream> #include <vector>
std::vector<int> find_factors(int);
int main() {
std::vector<int> factors(find_factors(42));
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.