non-integers from created function?

This is a discussion on non-integers from created function? within the C Programming forums, part of the General Programming Boards category; I had to write a program for class that gives first 10 perfect squares, then prints them backwords, then takes ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Question non-integers from created function?

    I had to write a program for class that gives first 10 perfect squares, then prints them backwords, then takes a number inputed and gives perfect squares up to that number, then take those and pass them to a function written in another file that is called in main. Every time 100 is printed also print jackpot. these two files need to be compiled using a makefile that combines the seperate function and main.
    i have two problems:
    1. a 0 prints after the first ten squares. actually its printing before the ten are printed backwords because when i isolate the first part the 0 doesn't print.
    2. the part that divides the squares by 9 is rounding like its outputing integers when a rational number is the answer but i am using float i thought in all the right places. also i even tried changin main and all arrays and variables to float and its still wrong. just .0000 is added to the end the correct rational number is not printing.

    the code for main, div function and the output are following. please don't be too critical i'm sure there is a much better way to do the parts that work but this is not a C class it's actually a unix class. we have very linited knowledge of C itself. also i'm not sure how to comment out the code for the forum so sorry if thats messed up. its my first post with actual code.
    PS: also if someone could tell me the syntax to print every element in an array without a loop that would be great also.

    CODE FOR MAIN:
    Code:
    float main()
    {
    
    int number, i, square[32676];
    float y[32676];
    
    printf("enter a number to find its square\n");
    scanf("%d",&number);
    
    i=1;
    while(i<=10)
    {
    square[i]=i*i;
     {
    if(square[i]==100)
      printf("jackpot!!!");
     }
    printf("%d\n",square[i]);
    i++;
    }
    while(i>=1)
    {
     {
      if(square[i]==100)
      printf("jackpot!!!");
     }
    printf("%d\n",square[i]);
    i--;
    }
    i++;
    while(i<=number)
    {
    square[i]=i*i;
     {
      if(square[i]==100)
      printf("jackpot!!!");
     }
    printf("%d\n",square[i]);
    y[i]=div9(square[i]);
    i++;
    }
    for(i=1;i<=number;i++)
    {
    if(y[i]==100)
    printf("jackpot!!!");
     {
     printf("%f\n",y[i]);
     }
    }
    return 0;
    }
    CODE FOR FUNCTION:
    Code:
    div9 (int x)
    {
     (x/9);
     return (x/9);
    }
    OUTPUT:
    enter a number to find its square:20
    1
    4
    9
    16
    25
    36
    49
    64
    81
    jackpot!!!100
    0
    jackpot!!!100
    81
    64
    49
    36
    25
    16
    9
    4
    1
    1
    4
    9
    16
    25
    36
    49
    64
    81
    jackpot!!!100
    121
    144
    169
    196
    225
    256
    289
    324
    361
    400
    0.000000
    0.000000
    1.000000
    1.000000
    2.000000
    4.000000
    5.000000
    7.000000
    9.000000
    11.000000
    13.000000
    16.000000
    18.000000
    21.000000
    25.000000
    28.000000
    32.000000
    36.000000
    40.000000
    44.000000
    Last edited by minty33; 10-26-2012 at 06:28 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    float main()?!
    It needs to be
    int main(void)

    PS: also if someone could tell me the syntax to print every element in an array without a loop that would be great also.
    The only other way is to use recursion, but that's definitely not what you want.

    I don't have time to look through more of your post at the moment.
    Last edited by iMalc; 10-26-2012 at 07:16 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    thanks for the array info. as for the the int main (void) i actually had that before but tried the float thing as an attempt to get it to work. anyway i did change it back to int main (void) and thats not it. if you end up with time later let me know. as i said in my post for problem number 1 i narrowed it down to the second while loop but nothing looks wrong. as for the division output i'm lost.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Okay, I'm trying to take a look at the code, but the awful indentation is making it too unnecessarily difficult.
    I'm quite surprised that nobody else has posted to help you in the mean time, but I suspect others are equally put off by the bad indentation and have just moved on.
    When you want help around here, you have to make it as easy as possible for others to help you, afterall, we do so for free, and if it's unnecessarily difficult then most of us just wont bother.
    Here's a small section of your code indented properly:
    Code:
    while(i<=10) {
        square[i]=i*i;
        { // This pair of braces is unnecessary, but as you have it here, the next line down must be further out.
            if(square[i]==100)
                printf("jackpot!!!");
        }
        printf("%d\n",square[i]);
        i++;
    }
    Please make the whole thing look like that and repost it. Then I'm sure lots of people will come and help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    If you want floatin point division, at least one of the operands to / must be of a floating point type; otherwise integer division (round down) will be performed.


    For fun, try this C99 program (variable defined inside the for; implicit return 0)
    Code:
    #include <stdio.h>
    int main(void) {
        for (int i = 1; i < 10; i++) {
            printf("%d %d %f\n", i, i/3, i/3.0);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  2. Replies: 4
    Last Post: 06-20-2007, 06:33 AM
  3. A data structure i created with a function
    By Darkinyuasha1 in forum C Programming
    Replies: 23
    Last Post: 11-14-2006, 09:03 PM
  4. Using DrawDoll(); function (self created)
    By bluehead in forum Windows Programming
    Replies: 8
    Last Post: 11-03-2002, 03:49 PM
  5. Replies: 13
    Last Post: 04-29-2002, 11:06 PM

Tags for this Thread


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