My Second C Program

This is a discussion on My Second C Program within the C Programming forums, part of the General Programming Boards category; Alright Question to is as follows. "Write a function sumsqr that consumes a non-negative integer n and prints all pairs ...

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

    My Second C Program

    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.
    (Question one was to make a function that found the highest integer for the squareroot of a number eg. isqrt(5) = 2, isqrt(10) = 3)

    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~

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    errors

    a few errors which i found

    1.a '{' is missing
    2. when u are using recursion there sud be no int
    Code:
                if (a == max) {
                            a = 0;
                            b = b+1;
                            return sumsqr(n);

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    Ok I changed what you asked and a few other things I noticed.

    sumsqr-driver.c looks like
    Code:
    #include <stdio.h>
    
    #include "sumsqr.h"
    
    int main() {
                  sumsqr(50);
                  return 0;
    }
    (basically got rid of the printf since thats what sumsqr does)

    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(n);
                } else {
                for (int i=1; i <= max; i=i+1) {
                if (a*a + b*b == n) {
                            printf("%d %d", a, b);
                            a = 0;
                            b = b+1;
                            return sumsqr(n);
                } else {
                            a = a+1;
                            return sumsqr(int n);
                }
                }
                }
    }
    (basically removed the ints and changed a situation that would have been a silly loop)

    and sumsqr.h contains which was given to us in a different part of the question

    Code:
    void sumsqr(int n);
    (changed nothing)

    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 error
    /cygdrive/c/Users/user/AppData/Local/Temp/ccMNyAjQ.o:sumsqr.c.text+0x20):unde
    fined reference to '_isqrt'
    collect2: ld returned 1 exit status.

    Ya I don't understand what this error is saying. Is it because I haven't compiled isqrt yet?

    ~Stormweaver1~

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to compile isqrt.c with everything else, since that is where isqrt is defined.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    Alright that seemed to work. Until I typed

    ./sumsqr

    I got a Microsoft Windows error

    "sumsqr.exe has stopped working" I'm going to assume this means I have an infinite loop.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes. (Note that when you use recursion, you must call the function with a different number each time, and there must be a number that, when you get that number, you stop.)

    Edit: Note also that recursion is neither necessary nor advisable for this problem.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    hmmm...when i reach the line

    return sumsqr(n);

    do all my integers get redefined back to 0?

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    ooo i should just do 2 while loops.

    where the first 1 counts up for the x value and the second one counts up for the y value.

    and if they match then print it.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    25
    Yay it worked thank you very much .

    It was also alot simpler. But that's a good way to learn...to screw up big time lol

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