My third C program

This is a discussion on My third C program within the C Programming forums, part of the General Programming Boards category; A regular number is a positive integer all of whose prime factors are in the set {2, 3, 5}. (1 ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    25

    My third C program

    A regular number is a positive integer all of whose prime factors are in the set {2, 3, 5}. (1
    is assumed to be regular.) Write a function regular that consumes a positive integer n and
    prints out all regular numbers up to (and possibly including) n, one to a line, in increasing
    order.
    To submit: regular.c, regular-driver.c.

    My regular.c contains
    Code:
    #include <math.h>
    #include <stdio.h>
    
    int regular(int n) {
                int m = n;
                while (n%5) {
                            m = m/5;
                }
                while (m%3) {
                            m = m/3;
                }
                while (m%2) {
                            m = m/2;
                }
                if (m == 1) {
                            printf("%d ", n);
                            return regular(n-1);
                }
    }
    my regular-driver.c contains
    Code:
    #include <stdio.h>
    
    #include "regular.h"
    
    int main() {
                regular(60);
                return 0;
    }
    I don't have an error but once I run regular. Nothing happens no output.

    I believe it is because it never reaches the if statement.
    I know that 60 is a regular number because 5*3*2*2 = 60
    So it should atleast printf that 60 is regular.

    Any help is appreciated.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would definitely throw in printf statements in places like this:
    Code:
    int regular(int n) {
                int m = n;
    printf("regular()...n=%d\n",n); fflush(stdout);
                while (n%5) {
    I didn't bother indenting it because I'm going to remove it soon (hopefully), and it will be easy to find and weed out of my code when I'm done debugging. This will tell you if the function has been called, and what input it recieved (because it could just be the difference between regular.h and regular.c, in which case your function is not being called).

    fflush(stdout) just makes sure that this printf output gets sent to the screen immediately, in case the program is about to crash. printf() is actually the greatest of all debugging tools.
    Last edited by MK27; 02-20-2009 at 11:20 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,741
    I think that you mean to compare with 0, i.e., n%5 should be n%5 == 0. The same goes for 3 and 2.

    There might be more efficient ways to solve the problem though, like generating the numbers instead of trying to find which numbers fit the definition.
    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

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    It works now thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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