Computer Hope
Software => Computer programming => Topic started by: ultimatum on February 10, 2009, 06:59:49 AM
-
Hey guys,
I need help regarding this small application I have to do as an assignment in school. I have to write a program that will show all factors of a number after the input. Ex. if the user enters 8 the program will output 4 and 2.
The only way (I thought of) to go around this would be to take the user input and start dividing from 1 and up, if the result is type int then it's a factor if double then it's not. I would use arrays to run the numbers in a loop and use arrays to save them too.
My question is what command in C# actually checks a stored variable or input for type, like int, double, char, string? I don't want the answer because I have to figure this out on my own, but a little help with this command would be appreciated. Thanks in advance.
-
I don't know about C#- but you won't need to check the type.
VB6 has a function called "int". C++ and C have one called "Floor", what it will do is strip off the fractional portion of a number. (if the number is negative one should use ceil()).
So what you would do is compare the result for each division with the Int (or ceil or floor) of that same number, and if they are equal the number is a factor.
Also: I'd imagine it would be in the Framework "Math" assembly.
-
Thanks. The idea I wanted to use is possible but is used in a different way. I was looking for a way to return an array from a method and stumbled on http://www.java2s.com/Code/CSharp/Language-Basics/Returnanarray.htm (http://www.java2s.com/Code/CSharp/Language-Basics/Returnanarray.htm) site which has the solution for my program. It uses % (division) to detect if the number after decimal is a 0 or something else. If a 0 it's a factor if not then it's not.
This is the line: if( (num%i)==0 )
The last problem was to find a way to use only two methods instead of classes to do this task and I think I got it covered. Thanks a lot though.
-
% is modulus, I wasn't sure the operator for C# for modulus so I didn't mention it :)
Not really division but remainder after integer division.
-
Yes,you use modulus.
If the language does not have a modulus operator you can create something that will do . You divide an integer by a smaller integer. Next you take the answer and multiply it to get back to the larger number. If you do not get the original large number, then the modulus was not zero. Integer arithmetic is much faster. Check to see how your language handles integer division. Some will round up a number. No No.
But, before you do that, create an array of integers with the first 3500 prime numbers. Don't bother using non-prime numbers. Wares of time. What does you professor expect? You should factor the numbers down to primes. For example, if a number is divisible by 3 and 6, you need to reduce the 6 down to 2 and 3 so the factors would be 3, 3, 2 which are primes.
Does this help any? ;)
-
Actually, any processor past the Pentium with MMX will do floating point at the same speed as Integer arithmetic. Of course a speed difference occurs between small integers and large Floating point numbers regardless, since an Integer, for example, is 2 bytes whereas a Double is 8 Bytes (the Integer only cycle on a 32-bit processor, the Double taking two)...
But since this is an academic exercise, Speed is a non-issue; it's more a test of algorithm development.
An interesting exercise is developing a Modulus routine that accepts and returns a floating point result- most Modulus operators in languages only deal in integral values. As such I created a routine for those instances where I need to determine the exact remainder even for floating point division:
Function Modulo(ByVal Num As Double, ByVal divisor As Double) As Double
Dim Temp As Double, Initial As Double
Dim Remainder As Double, oldval As Double
Initial = divisor
If divisor > Num Then Modulo = 0: Exit Function
Do Until divisor >= Num
oldval = divisor
divisor = divisor + Initial
Loop
Modulo = Int(Abs(oldval - Num))
End Function
-
The assignment is not to find prime factors, as the OP's first post makes clear.
I have to write a program that will show all factors of a number after the input. Ex. if the user enters 8 the program will output 4 and 2.
I didn't know 4 was prime.
Here's a method, crude I know. It mimics what you might do if asked to do this with a pencil and paper, often a good starting point for finding an algorithm. Which you can later refine or discard.
Given an input number N, start at 2 (since 1 is not desired in the results) and multiply 2 by all numbers between itself and N-1, storing in an array all those where the product is N. Then do the same with 3,4,5, etc until N-1 (since N is not desired in the results) is reached.
Here it is in BASIC, I whipped this up in a hurry... I used longints in case somebody input a big number, although you could wait a long time for the answer... 9978 took 6 seconds on my 3 GHz Pentium 4. I'm still waiting for the answer to 98763, which started 6 minutes ago... But I have to go to work... Compiled in FreeBasic.
[Edit]
S:\Test\Basic\factors>factors4
number ?98763
started at 08:02:17
ended at 08:11:26
3 7 21 4703 14109 32921
There are various ways you could make this more efficient...
dim N as longint
dim i as longint
dim j as longint
dim product as longint
input "number ?", N
dim array(1 to N) as longint
for element=1 to N
array(element)=0
next element
for i = 2 to N-1
for j = 2 to N-1
product=j*i
if product=N then
array(j)=1
array(i)=1
endif
next j
next i
for element=1 to N
if array(element)=1 then
print element;" ";
endif
next element
print
S:\Test\Basic\factors>factors3
number ?12
2 3 4 6
S:\Test\Basic\factors>factors3
number ?72
2 3 4 6 8 9 12 18 24 36
S:\Test\Basic\factors>factors3
number ?365
5 73
S:\Test\Basic\factors>factors3
number ?9978
2 3 6 1663 3326 4989
S:\Test\Basic\factors>
-
I was actually thinking of getting into freeBASIC a little more, merely so I could make console apps with basic... but all my existing codebase is in VB6, and freeBASIC is a nard-crusher when it comes to using COM components. And I have of way of creating console apps in VB6, which a little LINK command line work after compilation, the subsystem is CONSOLE! hooray!
Great for QuickBASIC like programs, where you really don't want to bother with all the hassle of project files. (or if you want the extra types)
-
hey... guyz.. i'm new here... I'm a comsci student...
Our prof told us to do program that determine the max & min value of the 5 input numbers.. I finish it but my program quite not good because sometimes it gives a wrong answer... can someone help me..???
-
1. Start your own thread; do not hijack somebody elses
2. Post your code that you have done already.
3. You do not say what language you are using.
-
hey.. guyz.. can someone know where to download.. ms visual basic..???
-
hey.. guyz.. can someone know where to download.. ms visual basic..???
::)
1. Start your own thread; do not hijack somebody elses
2. Post your code that you have done already.
3. You do not say what language you are using.
4. read the forum rules.
a VB.NET download for the express edition is available free from MS. earlier versions (1 through 6) are still under copyright by microsoft and thus any information as to their acquisition is against forum rules.
-
sorry sir.... I don't mean that..
by the way I'm making a program now... I want to ask...
how can i use the do while statement in case statement...
I'll show you what I want to do...
______________________________
the output
Press A for Action Press B for Comedy
Enter your choice:
______________________________
if the user type wrong choice the program will repeat... help me plz...
???
-
1. Start your own thread; do not hijack somebody elses
-
do not hijack somebody elses
And BC, you never do that?
-
do not hijack somebody elses
And BC, you never do that?
er... That's different, I "ease" the thread innocently in a different direction. ;D
And I usually have accomplices.
-
hello guyz... i need your help??? what is wrong in this program>>>
when I press N it will go to choice but I can't input any letters because it will terminate
help.. me plz....
#include<stdio.h>
#include<conio.h>
main()
{
int a,d,c,s,h,ls,car,scifi,ans;
char ch;
#define p printf
#define g gotoxy
#define c cprintf
choice:
{clrscr();
g(17,2);p(" >>()> JESSAR_AMA VIDEO RENTALS <()<< ");
g(25,3);p("CONTACT No.09092957632");
g(15,7);p("PRESS[");textcolor(WHITE+BLINK);c("A");p("]for ACTION:");
g(15,9);p("PRESS[");textcolor(WHITE+BLINK);c("D");p("]for DRAMA:");
g(15,11);p("PRESS[");textcolor(WHITE+BLINK);c("C");p("]for COMEDY");
g(15,13);p("PRESS[");textcolor(WHITE+BLINK);c("S");p("] for SUSPENSE:");
g(15,15);p("PRESS [");textcolor(WHITE+BLINK);c("L");p("] for LOVE STORY:");
g(15,17);p("PRESS [");textcolor(WHITE+BLINK);c("H");p("] for HORROR:");
g(15,19);p("PRESS [");textcolor(WHITE+BLINK);c("F");p("] for FICTION\n");
p("\n Enter your choice:_");gotoxy(33,21);
scanf("%c",& ch);}
switch(ch)
{
case 'A': clrscr();
g(29,1);textcolor(BLUE+BLINK);c("***************");
g(29,3);textcolor(BLUE+BLINK);c("***************");
g(43,2);textcolor(BLUE+BLINK);c("*");
g(29,2);textcolor(BLUE+BLINK);c("*");
g(30,2);textcolor(WHITE);c("ACTION MOVIES");
g(30,5);p("Are you sure?[Y/N]");
scanf("%s",&ans);
if (ans=='N')
{
goto choice;
}
break;
case 'D': clrscr();
g(29,1);textcolor(RED+BLINK);c("**************");
g(29,3);textcolor(RED+BLINK);c("**************");
g(42,2);textcolor(RED+BLINK);c("*");
g(29,2);textcolor(RED+BLINK);c("*");
g(30,2);textcolor(WHITE);c("DRAMA MOVIES");break;
case 'C': clrscr();
g(29,1);textcolor(YELLOW+BLINK);c("***************");
g(29,3);textcolor(YELLOW+BLINK);c("***************");
g(43,2);textcolor(YELLOW+BLINK);c("*");
g(29,2);textcolor(YELLOW+BLINK);c("*");
g(30,2);textcolor(WHITE);c("COMEDY MOVIES");break;
case 'S': clrscr();
g(29,1);textcolor(GREEN+BLINK);c("******************");
g(29,3);textcolor(GREEN+BLINK);c("******************");
g(45,2);textcolor(GREEN+BLINK);c("*");
g(29,2);textcolor(GREEN+BLINK);c("*");
g(30,2);textcolor(WHITE);c("SUSPENSE MOVIES");break;
case'L':clrscr();
g(29,1);textcolor(RED+BLINK);c("*******************");
g(29,3);textcolor(RED+BLINK);c("*******************");
g(47,2);textcolor(RED+BLINK);c("*");
g(29,2);textcolor(RED+BLINK);c("*");
g(30,2);textcolor(WHITE);c("LOVE STORY MOVIES");break;
case 'H': clrscr();
g(29,1);textcolor(YELLOW+BLINK);c("***************");
g(29,3);textcolor(YELLOW+BLINK);c("***************");
g(43,2);textcolor(YELLOW+BLINK);c("*");
g(29,2);textcolor(YELLOW+BLINK);c("*");
g(30,2);textcolor(WHITE);c("HORROR MOVIES");break;
case 'F': clrscr();
g(29,1);textcolor(GREEN+BLINK);c("****************");
g(29,3);textcolor(GREEN+BLINK);c("****************");
g(44,2);textcolor(GREEN+BLINK);c("*");
g(29,2);textcolor(GREEN+BLINK);c("*");
g(30,2);textcolor(WHITE);c("FICTION MOVIES");break;
default:gotoxy(17,40);textcolor(RED+BLINK);cprintf(" SORRY THE MOVIE TYPE YOU WANT IS NOT AVAILABLE!!!");
}
getch();}
-
I'm not sure what language this is, but why are you entering 'N', the menu should initiate right away and be waiting for your input.
Why do you have this line "int a,d,c,s,h,ls,car,scifi,ans;"? Why are these letters type of int? or is that a continuation of the program?
-
If you want other people to help you, try to avoid this sort of thing:
#define p printf
#define g gotoxy
#define c cprintf
If makes things hard to read and you can make a very bad error.
Aldo, it helps if you use a High-Level approach for something that is not going to be used to launch rockets .
Make a case block as clean and neat as you can. avoid details.
Something like this:
case 'A' : dofirst;(); break;
case 'D' : dosecond(); break;
case 'E' : dothird(); break ;
case 'N' : goto choice;
Using inline code makes it hard to debug. Start out with void functions that are 'stubs', they don't do anything, but they are place holders where you will expand the code later as you work out the logic flaws. The concept is to HIDE details the are not relevant to the logic of what you are doing.
This way you separate the logic from the decoration.