Thread: beginner multiple function

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    6

    beginner multiple function

    OT: Being a total beginner to programming but having lots of free time I recently started practicing. Having a rough time on functions. Thanks to everyone in advance.

    THE PROBLEM:
    Given 2 integers N and M from standard input, using a multiples function, calculate the integers in [1,N] multiple of M. Print the sets in increasing order.

    MY (WRONG) SOLUTION:

    Code:
    #include <stdio.h>
    
    
    int multipli(int,int);
    int multipli (int x, int y)
    {
        int i;
        for (i=1;i<=x;i++)
        {
            if((i%y)==0)
            {
                return i;
            }
        }
    }
    int main(void)
    {
        int N;
        int M;
        scanf("%d", &N);
        scanf("%d", &M);
        printf("%d", multipli(N,M));
        return 0;
    }
    Thanks again in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of returning a multiple, you could print it in the loop body.

    You might also consider if skipping numbers by the multiple might be a more efficient approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    6
    I'm still getting "output isn't correct" (practicing on an autoevaluator platform) after trying both suggestions.
    I really thought the mistake was inthe printf command.
    Maybe I'll just skip this one.
    But your suggestions are great, I should start thinking about efficiency more.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm still getting "output isn't correct" (practicing on an autoevaluator platform) after trying both suggestions.
    You need to be very specific about what format the auto checker expects.

    For example, not printing newlines would be a killer if you print
    5101520
    and not as expected
    5
    10
    15
    20
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2020
    Posts
    6
    OMG, thanks, that's it! I forgot to notice the commas dividing the numbers in the output. Sorry for making you waste your time on something like this, I hope to be able to give back sooner or later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  2. Replies: 1
    Last Post: 11-13-2010, 11:59 PM
  3. Replies: 3
    Last Post: 11-13-2010, 11:49 PM
  4. beginner function questions
    By drag0n69 in forum C Programming
    Replies: 4
    Last Post: 02-22-2008, 11:36 AM
  5. Beginner Question: Multiple source files
    By ironfistchamp in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2006, 02:19 PM

Tags for this Thread