First C program

This is a discussion on First C program within the C Programming forums, part of the General Programming Boards category; So the question is Write a function isqrt that consumes a non-negative integer n and produces the largest integer less ...

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

    First C program

    So the question is

    Write a function isqrt that consumes a non-negative integer n and produces the largest
    integer less than or equal to the square root of n (that is, the integer square root of n).
    To submit: isqrt.c, isqrt-driver.c.

    and I've been working on this for about 2 days and getting nowhere.

    for my isqrt.c I have

    Code:
    int isqrt(int n) {
       int a = sqrt(n);
       int b = 0
       for (int i = 1; i <= a; i = i+1)
              b = b +1;
       return b;
    }
    for my isqrt-driver.c I have

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    #include "isqrt.c"
    int main()
    {
            printf("%d", isqrt(5));
            assert(isqrt(5) == 2); /*no effect */
            return 0;
    }
    I know this is my first program and all but I just don't understand what's wrong with what I'm doing...
    Then I run the gcc -std=99 -O -o isqrt isqrt-driver.c isqrt.c

    and it gives me a lot of error messages and

    then run ./isqrt

    and it says its not in the directory which makes sense cause it was never compiled.

    Any help would be appreciated. Don't just give me code cause that is cheating and what not >.<. I'd ask my TA's but it is reading week so I don't have access to them

  2. #2
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    If errors exist then no executable created.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    What are the errors you're receiving?

    First off, *never* include a .c/.cpp file. Includes are for headers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    well we were given startup code for isqrt-driver.c

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    #include "isqrt.h"
    
    int main() {
        assert(isqrt(5) == 2); /* no effect */
        return 0;
    }
    and so you pointed out that I should never include a .c file...

    so on the fourth line i had
    #incldue "isqrt.c" but i changed that to an h.

    but still nothing works, my errors are

    isqrt-driver.c:4:19: isqrt.h: No such file or directory (which is why I changed the include to c in the first place)
    isqrt-driver.c: In function 'main':
    isqrt-driver.c:7: warting implicit declaration of function 'isqrt'
    isqrt.c: In function 'isqrt':
    isqrt.c:2: warning: implicit declaration of function 'sqrt'
    isqrt.c:4: error: parse error before "for"
    isqrt.c:4: error: 'i' undeclared (first use in this function)
    isqrt.c.4: error: (Each undeclared identifier is reported only once
    isqrt.c:4: error: for each function it appears in.)
    isqrt.c:4: error: parse error before ')' token

    anyways...these don't make much sense to me. I've read over the lecture slides and we never even went over a situation where a "driver" called on a program. But it makes sense to me cause then it becomes easier to debug when you're working with bigger programs.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you put functions in a separate source file, then you need to:
    1) Put the prototypes of those functions in yet another file, a header (.h) file;
    2) Make sure every file that calls those functions includes the header file written in 1 above;
    3) Compile both .c files together at the same time.

    Also, using sqrt requires including <math.h>.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    1) .....I don't understand. Do you mean I have to compile isqrt.c all on its own as opposed to compiling them at the same time? Or do I have to save isqrt.c as isqrt.h somewhere?

    2) I think I did this correctly? Or did I have to write #include "isqrt.h" one line above?

    3) Ya I think I did this since I used the line
    "Then I run the gcc -std=99 -O -o isqrt isqrt-driver.c isqrt.c"

    I need to use #include <math.h> .....well we definitely never learned that

    Thanks though I'll throw in the include math, the other 3 points I'm not to sure as to what you're trying to say. I've never worked with C so this is kind of confusing :P

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Stormweaver1 View Post

    I need to use #include <math.h> .....well we definitely never learned that
    And with gcc you need -lm as well (for -l linker m math objects, I guess)
    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

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    so i included the math line

    Code:
    #include <math.h>
    
    int isqrt(int n) {
       int a = sqrt(n);
       int b = 0
       for (int i = 1; i <= a; i = i+1)
              b = b +1;
       return b;
    }
    and then i tried compiling with the -lm command so it looks like
    Code:
    gcc -std=c99 -O -o -lm isqrt isqrt-driver.c isqrt.c
    and so now a few of the error messages went away.


    isqrt.c: In function 'isqrt':

    isqrt.c:4: error: parse error before "for"
    isqrt.c:4: error: 'i' undeclared (first use in this function)
    isqrt.c.4: error: (Each undeclared identifier is reported only once
    isqrt.c:4: error: for each function it appears in.)
    isqrt.c:4: error: parse error before ')' token

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Stormweaver1 View Post
    1) .....I don't understand. Do you mean I have to compile isqrt.c all on its own as opposed to compiling them at the same time?
    Since I explicitly said exactly the opposite ("compile both .c files together at the same time") it seems unlikely that that's what I would mean. You need an additional file, isqrt.h, in addition to main.c and isqrt.c. That file needs to contain the prototype of your functions in isqrt.c

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Stormweaver1 View Post
    so i included the math line

    Code:
    #include <math.h>
    
    int isqrt(int n) {
       int a = sqrt(n);
       int b = 0
       for (int i = 1; i <= a; i = i+1)
              b = b +1;
       return b;
    }
    and then i tried compiling with the -lm command so it looks like
    Code:
    gcc -std=c99 -O -o -lm isqrt isqrt-driver.c isqrt.c
    and so now a few of the error messages went away.


    isqrt.c: In function 'isqrt':

    isqrt.c:4: error: parse error before "for"
    isqrt.c:4: error: 'i' undeclared (first use in this function)
    isqrt.c.4: error: (Each undeclared identifier is reported only once
    isqrt.c:4: error: for each function it appears in.)
    isqrt.c:4: error: parse error before ')' token
    You have to put -lm at the end, not in the middle. And since there was an error before for, look directly before for and see what you see.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    :O there's no little ";" semi-colon thing.

    I'll give that a try, Thanks a bunch

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    alright all of the errors went away now i have a new one. So I think I'm close.

    /cygdrive/c/Users/user/AppData/Local/Temp/ccwWTWvi.o:isqrt.c.text+0x0):multip
    le definition of '_isqrt'
    /cygdrive/c/Users/user/AppData/Local/Temp/ccEc13G4.o:isqrt-driver.c.text+0x0):
    collect2: ld returned 1 exit status

    So I'm going to guess I have to delete a temporary file or something.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, what it means is that you should delete one of the two definitions of isqrt() that you have.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    o...but i have 2 because i thought you needed one for isqrt.c and one for isqrt.h

    but I guess I'll delete the isqrt.h one.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    so i deleted the isqrt.h but now its complaining that isqrt.h doesn't exist again.

    I'm very confused. I'll keep trying different things though.

Page 1 of 2 12 LastLast
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