Thread: Function problems

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by matsp View Post
    In some respects, I think the same about (void) in function the function argument list in C++.
    There's something I just have to hate. Void does not need to be used in argument lists in C++. It just tends to look ugly. Knowing that I have to use "..." to create variables argument lists, I really think void can be left out.
    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.

  2. #17
    Registered User
    Join Date
    Feb 2008
    Location
    Tophet
    Posts
    4
    Are there any suggestions as to how I could write the function(s) that draw the figures? I've fixed all the formatting stuff you guys didn't like, but that doesn't help the problem.

    I gave all the information I was given for the assignment. I know several classmates have finished, and may end up asking them for help. This is the only assignment so far that has stumped me so much. What really bugs me is that a professor who has used my book for years at Hawaii posted his concept of the answers online to all of them BUT this one. I wish he had posted this one so that I could figure out how it works.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by DrendDragonspaw View Post
    Are there any suggestions as to how I could write the function(s) that draw the figures? I've fixed all the formatting stuff you guys didn't like, but that doesn't help the problem.

    I gave all the information I was given for the assignment. I know several classmates have finished, and may end up asking them for help. This is the only assignment so far that has stumped me so much. What really bugs me is that a professor who has used my book for years at Hawaii posted his concept of the answers online to all of them BUT this one. I wish he had posted this one so that I could figure out how it works.
    I still think you should go with the seven segment display:
    Code:
    *******
    *     *
    *     *
    *******
    *     *
    *     *
    *******
    The size of the box tells you where each of the segments goes (see above for 7x7); the digit tells you which segments you need to draw and which ones you don't. (You could either do switches, or a lookup table, or a whole heck of a lot of if statements, or probably some other things I'm not thinking of right now to decide which segments are on, based on the digit.)

  4. #19
    Registered User
    Join Date
    Feb 2008
    Location
    Tophet
    Posts
    4
    I think I have what I need to do. It will just be a whole freaking bunch of if and for statements. I will post it when I am done so you can see what I ended up doing. It will be a total pain in the ass, and a really long piece of code to do such a trivial task, but it will give me some points.

    Thanks

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    So how would that work in a 5X5 or 6X5 grid? Surely there is a minimum grid and even perhaps a maximum grid.

    A grid of 1 or 2 is impossible. A grid of 3 is only good for a single character, so also no good.

    It's not until you get a grid of 7X7 that a 5X5 digit will fit, which is probably the first one where all digits could be recognized, and that's with no space between it and the grid.

    ... thinking... drawing....

    Hold the phone - I've figured this out.

    If you map a 5X5 character, you can then apply enough rules to scale them all. Look at this picture of 3. The (complicated) rules would apply to all numbers. Basically, it works with the concept of filler positions. Row1, 3 and 5 need to be respected, and col1, col3 and col5 need to be respected. Everything else can be repeated. It should work for all digits. You get to define them and structure the program to do the rest.

    And, the code to get you started... (you're welcome)
    Code:
    #include <stdio.h> 
    
    void print_digit(char c[5][5], int newgrid_dim ) ; 
    
    int main(void) { 
    
    char grid3[5][5] = {   // the character "3" 
    	{ '*','*', '*','*', '*' } , 
    
    	{ ' ',' ', ' ',' ', '*' } , 
    
    	{ '*','*', '*','*', '*' } , 
    
    	{ ' ',' ', ' ',' ', '*' } , 
    
    	{ '*','*', '*','*', '*' } }  ; 
    	
    	int newgrid_dim = 9 ; 
    
    	int newgrid_center = (newgrid_dim+1) / 2 ; 
    
    	int i , j ;  // dimension of new grid (row, col) 
    	int a , b ;  // dimension of grid3 
    
    //	char * gridptr ; 
    //	gridptr = grid3 ; 
    
    	print_digit(grid3, 9) ; 
    }
    
    void print_digit(char grid[5][5] , int newgrid_dim) {  
    
    	int newgrid_center = (newgrid_dim+1) / 2 ; 
    
    	int i , j ;  // dimension of new grid (row, col) 
    	int a , b ;  // dimension of grid3 
    
    	for (i = 1 ; i <= newgrid_dim ; ++i ) { 
    		for (j = 1 ; j <= newgrid_dim ; ++j ) { 
    			if (i==1) a = 1 ; 
    			else if (i < newgrid_center) a = 2 ;   // filler position
    			else if (i == newgrid_center) a = 3 ; 
    			else if (i < newgrid_dim) a = 4 ;  // filler position 
    			else a = 5 ; 	
    
    			if (j==1) b = 1 ; 
    			else if (j < newgrid_center) b = 2 ;   // filler position
    			else if (j == newgrid_center) b = 3 ; 
    			else if (j < newgrid_dim) b = 4 ;  // filler position 
    			else b = 5 ; 
    
    			// a & b are now set to point to the proper character to write 
    			// at i,j 
    			printf("%c", grid[a-1][b-1] ) ; 
    		}
    		printf("\n") ; 
    	} 
    }
    Mainframe assembler programmer by trade. C coder when I can.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Code:
    #include <stdio.h> 
    
    void print_digit(char c[5][5], int newgrid_dim ) ; 
    
    int main(void) { 
    	char grid3[5][5] = {   // the character "3" 
    		{ '*','*', '*','*', '*' } , 
    
    		{ ' ',' ', ' ',' ', '*' } , 
    
    		{ '*','*', '*','*', '*' } , 
    
    		{ ' ',' ', ' ',' ', '*' } , 
    
    		{ '*','*', '*','*', '*' } }  ; 
    	
    	int newgrid_dim = 9 ; 
    
    	int newgrid_center = (newgrid_dim+1) / 2 ; 
    
    	int i , j ;  // dimension of new grid (row, col) 
    	int a , b ;  // dimension of grid3 
    
    //	char * gridptr ; 
    //	gridptr = grid3 ; 
    
    	print_digit(grid3, 9) ; 
    }
    
    void print_digit(char grid[5][5] , int newgrid_dim) {  
    
    	int newgrid_center = (newgrid_dim+1) / 2 ; 
    
    	int i , j ;  // dimension of new grid (row, col) 
    	int a , b ;  // dimension of grid3 
    
    	for (i = 1 ; i <= newgrid_dim ; ++i ) { 
    		for (j = 1 ; j <= newgrid_dim ; ++j ) { 
    			if (i==1) a = 1 ; 
    			else if (i < newgrid_center) a = 2 ;   // filler position
    			else if (i == newgrid_center) a = 3 ; 
    			else if (i < newgrid_dim) a = 4 ;  // filler position 
    			else a = 5 ; 	
    
    			if (j==1) b = 1 ; 
    			else if (j < newgrid_center) b = 2 ;   // filler position
    			else if (j == newgrid_center) b = 3 ; 
    			else if (j < newgrid_dim) b = 4 ;  // filler position 
    			else b = 5 ; 
    
    			// a & b are now set to point to the proper character to write 
    			// at i,j 
    			printf("%c", grid[a-1][b-1] ) ; 
    		}
    		printf("\n") ; 
    	} 
    }
    You should know that everything inside a function should be indented. It makes it confusing otherwise...
    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. #22
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    Yes, I missed that one. No comments about the cleverness of the algorithm??
    Mainframe assembler programmer by trade. C coder when I can.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    I'm not one for algorithms
    Difficult to understand most of the times...
    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. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    There's something I just have to hate. Void does not need to be used in argument lists in C++. It just tends to look ugly. Knowing that I have to use "..." to create variables argument lists, I really think void can be left out.
    What is the connection between vargs and void arg lists?

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    No void in C meant variable arguments. Void in C++ is not necessary, but may be added to explicitly declare there should be no variable argument list, in response to mats's reply.
    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.

  11. #26
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Quote Originally Posted by Elysia View Post
    No void in C meant variable arguments. Void in C++ is not necessary, but may be added to explicitly declare there should be no variable argument list, in response to mats's reply.
    empty parameter list in C means variable arguments.
    That's why void as a parameter list in C is required to specify the function with no arguments.

    empty parameter list in C++ means no arguments, that's why void in parameters list is fully equivalent to empty parameters list and could be fully avoided in C++.

    It is used only to make code in headers look like the C-code
    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

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    OP:

    Quote Originally Posted by Elysia View Post
    No void in C meant variable arguments. Void in C++ is not necessary, but may be added to explicitly declare there should be no variable argument list, in response to mats's reply.
    There is a world of difference in what she's saying and what is true. Actually, in C, empty parens is an undefined argument list in the sense that you can pass anything and the compiler ignores that. Variable argument lists can be passed "anything" too, but the arguments during he call are contained in a va_list. The prototypes for these functions is obvious. That allows you to take some advantage of the arguments you pass. Using an undefined argument list you can't make an assumption what arguments are named or their types, and there is nothing like va_list to maintain them.

    In C++, empty parens is an empty argument list, but only because Stroustrup designed things this way. Ugly or not, void will still be necessary in code that "crosses the aisle" to C.
    Last edited by whiteflags; 02-08-2008 at 02:03 AM. Reason: I've been foiled!

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by citizen View Post
    In C++, empty parens is an empty argument list, but only because Stroustrup designed things this way. Ugly or not, void will still be necessary in code that "crosses the aisle" to C.
    C++ is not C.
    Void is not necessary in argument list so long as you don't compile it as C.
    Which is my point. It's ugly and undeserving.
    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.

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yeah what citizen was saying is what I was getting at -- an empty argument list does not necessarily denote a variable argument list.

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