Thread: Function problems

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Tophet
    Posts
    4

    Function problems

    This is my first time on this board, and I already like it. I have been having trouble making functions work for the last several months, and this function confuses me to no ends.

    The goal of the project is to force the computer to print a digit out of asterisks, something like this:

    Code:
      
      *******
            *
            *
      *******
            *
            *
      *******
    In case you are like me, and couldn't see it at first, this is a 3. The project is supposed to build on two previously made functions, printBox, and printStars, but I do not see how it does. I will also include the program I built for those functions. It is supposed to be called printDigit. Here is the code I have so far. I think I have the call and headers correct, but do not know how I should make this function work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void printDigit(int, int);
    main(void)
    {
     int a,b;
     printf("Input 2 numbers. The first number is the number you wish to form.\n
                 The second number will determine the size of the digit printed.");
     scanf("%d %d",&a,&b);
     void printDigit(int a, int b);
    }
    
    void printDigit(int a, int b)
    {
    Code:
    #include <stdio.h>
    
    main()
    {
      void printBox(int length, int height);
    
      printf("A 3x5 box:\n");
      printBox(5, 3);
      printf("\n");
      printf("A 5x10 box:\n");
      printBox(10, 5);
      return 0;
      system("pause");
    }
    
    void printAsterisks(int n)
    {
      int i;
    
      for (i=0;i<n;i++)
        printf("*");
    }
    
    void printBlanks(int n)
    {
      int i;
    
      for (i=0;i<n;i++)
        printf(" ");
    }
    
    void printBox(int length, int height)
    {
      void printAsterisks(int n);
      void printBlanks(int n);
    
      int i;
      if (height >= 1)
       printAsterisks(length);
       printf("\n");
        for (i=1;i<height-1;i++)
        {
          if (length >= 1)
           printf("*");
    	  if (length >= 2)
          printBlanks(length - 2);
          printf("*");
    	}
         printf("\n");      
         if (height >= 2)
         printAsterisks(length);  
         printf("\n");
    }
    Any help would be appreciated, as this project was due yesterday.
    Last edited by DrendDragonspaw; 02-06-2008 at 06:15 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    OK, so a few notes:
    1) Main returns int.
    2) Prototypes should be in a separate header file or at the start of your source file, bot inside functions.
    3) You might need to indent a little better: http://cpwiki.sf.net/User:Elysia/Indentation.
    4) 1 space for indentation which you use in some places is hardly enough.
    5) It might be better to use getchar() instead of system("pause") due to being more portable.

    And you never actually mentioned what the problem was?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    OK, so a few notes:
    1) Main returns int.
    To elaborate: cpwiki.sf.net/Void_main
    2) Prototypes should be in a separate header file or at the start of your source file, [n]ot inside functions.
    Well, they don't have to be -- and I wouldn't even go so far to say that they "should" be. I'd just state that by convention, prototypes are declared globally, often at the beginning of the source file; after #includes but before any function bodies. But at least one book that I've seen used the local-prototype style, and it's perfectly acceptable. At least prototypes are being used.

    I know we've had this discussion before . . . .

    3) You might need to indent a little better: http://cpwiki.sf.net/User:Elysia/Indentation.
    Those two pages really ought to be merged . . . .

    5) It might be better to use getchar() instead of system("pause") due to being more portable.
    cpwiki.sf.net/Pause_console

    Anyway.

    I'm not sure exactly what you need to do to get this
    Code:
      *******
            *
            *
      *******
            *
            *
      *******
    but you could just consider it a series of
    Code:
      *******
    and
    Code:
            *
    I'm sure that's occured to you. Perhaps it would be better to consider it to consist of two semi-boxes:
    Code:
      *******
            *
            *
    and one filled-in line:
    Code:
      *******
    (Are those spaces at the beginning of the lines intentional?)

    Or maybe even one big box, minus the left side, which is intersected in the middle.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by dwks View Post
    But this is implicit main, not void main...

    Well, they don't have to be -- and I wouldn't even go so far to say that they "should" be. I'd just state that by convention, prototypes are declared globally, often at the beginning of the source file; after #includes but before any function bodies. But at least one book that I've seen used the local-prototype style, and it's perfectly acceptable. At least prototypes are being used.

    I know we've had this discussion before . . . .
    Indeed. Local function declarations are stupid because it clutters up the code and it creates duplicates of function prototypes and it means the functions can't be used in other source files, unless you duplicate them.
    I consider it bad practice to put them locally.

    Those two pages really ought to be merged . . . .
    You said you were going to try it. What happened?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    But this is implicit main, not void main...
    Oh, you're right -- I thought implicit main was explained there as well.

    You said you were going to try it. What happened?
    I think you're getting a PM on the subject in a little while . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by dwks View Post
    Oh, you're right -- I thought implicit main was explained there as well.
    Well, it mentions it should say "int main()" but nothing really apart from that. It only covers void main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Location
    Tophet
    Posts
    4
    My professor throws a hissy fit if we declare anything globally. The problem is that I have no idea how to get the function to draw the figures. I considered making several functions to return each integer from 0-9 based on the input, but a) I don't think it would work the way I want it to, b) that my professor would accept it, and c) how it would work with the size modifier.

    The actual textbook problem is this: "Write a function, printDigit, that takes a number from 0-9 and a size n, and displays it an n X n box. For example, the call printDigit(3, 7) would display the number 3 in a 7 X 7 box," like above.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    OK well, prototypes aren't variables and should be on file scope level. It's good practice. In real applications, you're going to use headers. And then the prototypes are typically always global.
    Your professor doesn't have a problem with function prototypes, or...?
    Well, suppose you could try and if your professor throws a fit, make it local but remember the little advice when you graduate.

    The textbox sums it up very well. You need to think about how to you would do it in reality.
    Try cluttering on a paper or something. Find a logical solution to the problem. That's the first step. Then you write the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    This is not a trivial assignment, unless I'm missing the obvious. Either that or all the constraints weren't put forth. Per my understanding, this program has to be able to draw the character-art representation of numbers 0-9 to an undefined scale.

    Therefore, it doesn't make sense to hard-code the representation of the numbers, and that leaves coming up with algorithms, for each digit, to be able to scale it with a random grid.

    What am I missing?
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Todd Burch View Post
    This is not a trivial assignment, unless I'm missing the obvious. Either that or all the constraints weren't put forth. Per my understanding, this program has to be able to draw the character-art representation of numbers 0-9 to an undefined scale.

    Therefore, it doesn't make sense to hard-code the representation of the numbers, and that leaves coming up with algorithms, for each digit, to be able to scale it with a random grid.

    What am I missing?
    I think we're (the royal we) supposed to be emulating a seven-segment display, like an alarm clock or whatever. So if the digit is 0, or 2, or 3, or 5, or 7, or 8, or 9, we have to draw the top crossbar, etc. It's still a bit technical coding-up, but I don't think we're really trying to be artistic.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    OK, so a few notes:
    1) Main returns int.
    Having no specified return type is the same as specifying "int."

    5) It might be better to use getchar() instead of system("pause") due to being more portable.
    getchar() isn't a portable function either...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    getchar() isn't a portable function either...
    hmm... are you trying to say that nothing is absolutely portable? Elysia noted that it was "more portable", and this seems reasonable since getchar() is part of the C standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    hmm... are you trying to say that nothing is absolutely portable? Elysia noted that it was "more portable", and this seems reasonable since getchar() is part of the C standard library.
    Whoopsie. I read that as "getch()."

    Yes, getchar() is fine

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by brewbuck View Post
    Having no specified return type is the same as specifying "int."
    Irrelevant. All functions should specify a return type. Leaving it out is just bad practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Irrelevant. All functions should specify a return type. Leaving it out is just bad practice.
    Indeed - if you leave it out, it is very hard to know if it was left out by mistake or if it was left out on purpose. If it's there, there is no confusion about what it is. In some respects, I think the same about (void) in function the function argument list in C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM