What should I do?

This is a discussion on What should I do? within the C Programming forums, part of the General Programming Boards category; Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    What should I do?

    Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And which part of your task is it you are struggling with?

    How to mathematically determine if integer A is a multiple of integer B?
    How to write a function in general, or something else?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Well technically all of it. lol But the main part that Im strugglin with is the math. Like what math do i need to do in order to return it true and to return it false.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what do you do if I say "Is 111 a multiple of 3" - you have pen and paper at hand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by matsp View Post
    So, what do you do if I say "Is 111 a multiple of 3" - you have pen and paper at hand.

    --
    Mats
    Well, 1 + 1 + 1 = 3. So of course 111 is a multiple of 3.


    Or is it? dun, dun dun...

    Perhaps a bigger hint than matsp's,
    7 / 2 = 3 remainder 1
    6 / 2 = 3 remainder 0

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Well, 1 + 1 + 1 = 3. So of course 111 is a multiple of 3.


    Or is it, dun, dun dun...
    That is of course one way. That only works for 3 tho', so if I ask if 111 is a multiple of, say, 7, you can't use that method.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Yes it is. So as long as the the digits add up to be a multiple of 3 then its a multiple of 3? Is that right?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jturner38 View Post
    Yes it is. So as long as the the digits add up to be a multiple of 3 then its a multiple of 3? Is that right?
    Yes, 3 is a "magical" number this way:
    21 for example, or 63, or 18 are all multiples of three, and the sum of the numbers is a multiple of three. But it doesn't work for any other "A is multple of B", e.g. 25 does not make a sum of it's numbers that make 5 - but it is a multiple of 5.

    So you need to work out something that works for ALL numbers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Ok I know Im a need a loop to repeat that process. So a if statement should be fine. The only problem is how can i say this without setting anything to a specific value. Or will I have to? For instance how can I say the first integer is a multiple of the second or vice versa without specifying a value?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is a very simple way to check if a value is a multiple of another value, yes, no matter what the different values are.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    use % operator

    if largeno%smallno is 0 then its divisible so its a multiple

  12. #12
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Ok well in the second function after my main function. Would i use a for loop. How would u put it? I have it like this:

    Code:
         for(x % y)
             if(remainder==0)
              return 1(true)
             if(remainder==1)
              return 0(false)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    Quote Originally Posted by jturner38
    Ok well in the second function after my main function. Would i use a for loop. How would u put it? I have it like this:
    Stop. The first part of your requirements asks you to do this: "Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first."

    I want to see your code for this function first. I also want to see you write the main function such that it calls this function with the values 10 and 5 as arguments, and based on the result prints whether 10 is a multiple of 5.

    If you cannot get the above right, there is no point going further. At a glance, you need to grasp some basic syntax first, otherwise you are going to end up with a program that so badly needs to be fixed that you might as well give up and rewrite it from scratch.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I have this so far and i have only 3 errors!

    Code:
    #include <stdio.h>
    
    int multiple;
    
    int main() {
    
    	int x;
    	int y;
    	int a;
    	int m;
    
    	printf("Enter first integer here:");
    	scanf("%d", x);
    
    	printf("Enter second integer here:");
    	scanf("%d", y);
    
    	m = int multiple(a)
    
    	return 0;
    }
    
    int multiple {
    	int a;
    
    	a = x % y;
    
    	if(a==0)
            printf("The second integer is a multiple of the first");
    		return 1;
    	if(a==1)
    		printf("The second integer is not a multiple of the first");
    		return 0;

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by jturner38 View Post

    int multiple;
    this is variable definition
    you want function declaration:

    int multiply(int x, int y);

    scanf("%d", x);
    should be

    scanf("%d", &x);

    m = int multiple(a)
    should be
    m = multiply(x,y);

    and do not forget tp print value of m afterwards


    Code:
    int multiple {
    	int a;
    
    	a = x % y;
    
    	if(a==0)
            printf("The second integer is a multiple of the first");
    		return 1;
    	if(a==1)
    		printf("The second integer is not a multiple of the first");
    		return 0;
    should start as
    Code:
    int multiply(int x, int y) {
    if(a == 1) should be removed - a could be any value from 1 to y-1

    also 2 statements after if(a ==0) should be placed inside
    Code:
    {}
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21