Thread: First C program

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Stormweaver1 View Post
    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.
    isqrt.h contains a prototype ONLY. If you don't know what the word prototype means, look it up or ask. But there should be NO code, just a prototype.

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    no i don't know what a prototype is . I just saved isqrt.c as isqrt.h

    ok i'll google it.

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    alright I googled it and searched for it on these forums clicked a few links and I still don't understand what you mean by a prototype.

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Clue: You've seen one before
    Code:
    void what_a_function(int X, char *Y);
    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

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Stormweaver1 View Post
    alright I googled it and searched for it on these forums clicked a few links and I still don't understand what you mean by a prototype.
    The first link from Google is http://en.wikipedia.org/wiki/Function_prototype which seems amazingly straightforward to me.

  6. #21
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    A function prototype is needed when you create your own function. For example:

    Code:
    #include <stdio.h>
    
    void msg(void);    /* This is the prototype */
    
    int main()
    {
            /* Displays Hello, World */
            msg();
            return(0);
    }
    
    void msg(void)
    {
            printf("Hello, World!");
    }
    Don't feel bad, I'm pretty new to C myself.

    __________________________________________________ _______

    Need money fast? Try this. http://cashcrate.com/1132657

  7. #22
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    So i changed isqrt.h to a "prototype"

    Code:
    int isqrt(int n);
    and it appears to work! Thank you very much for your help. I have learned more from you then I have from a lecture.

    Now onto question #2 :P

  8. #23
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Sure. What question do you have?

  9. #24
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    Alright Question to is as follows.

    "Write a function sumsqr that consumes a non-negative integer n and prints all pairs (x, y)
    such that x and y are nonnegative integers, x >= y, and x^2 + y^2 = n. Each pair is printed
    on a separate line without the parentheses or comma. You may either print them in order of
    increasing x or decreasing x. For example, the call sumsqr(50) would print either:
    5 5
    7 1
    or
    7 1
    5 5
    Your program should be able to list all 18 ways to write 446265625 as the sum of two
    squares.
    To submit: sumsqr.c, sumsqr-driver.c."

    Now after looking at the question I realized that the highest possible number will be the square root of the number. So we're going to be implementing question #1.

    sumsqr-driver.c looks like
    Code:
    #include <stdio.h>
    
    #include "sumsqr.h"
    
    int main() {
                  printf("%d", sumsqr(50));
                  return 0;
    }
    sumsqr.c is
    Code:
    #include <math.h>
    #include <stdio.h>
    
    #include "isqrt.h"
    
    int sumsqr(int n) {
                int a = 0;
                int b = 0;
                int max = isqrt(n);
                if (a == max) {
                            a = 0;
                            b = b+1;
                            return sumsqr(int n);
                } else {
                for (int i=1; i <= max; i=i+1) {
                if (a*a + b*b == n) {
                            printf("%d %d", a, b);
                            a = a+1;
                            return sumsqr(int n);
                } else {
                            a = a+1;
                            return sumsqr(int n);
                }
                }
    }
    and sumsqr.h contains which was given to us in a different part of the question

    Code:
    void sumsqr(int n);
    And I'm kind of new to the whole prototype thing. But correct me if I'm wrong.
    It is saying. It consumes a number and produces a void and in this case void is a set of printfs.

    when i try to compile it using the line

    Code:
    gcc -std=c99 -O -o sumsqr sumsqr-driver.c sumsqr.c -lm
    I get the errors

    sumsqr-driver.c: In function 'main':
    sumsqr-driver.c:6: error: invalid use of void expression
    sumsqr.c: In function 'sumsqr':
    sumsqr.c:13: error: parse error before "int"
    sumsqr.c:19: error: parse error before "int"
    sumsqr.c:22: error: parse error before "int"
    sumsqr.c:25: error: pares error at end of input

    ~Stormweaver1~(I'm going for supper but I should be back in 20)

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    return sumsqr(int n);
    sumsqr returns void
    you cannot return value of function returning void

    also when calling function - type should not be passed - only var name
    do it as (if you really need a recursion here):
    Code:
    sumsqr(n);
    return;
    Last edited by vart; 02-21-2009 at 01:06 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 01: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, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM