Thread: implementation question

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    39

    implementation question

    well I managed to pull this one off on my own, but I was wondering if for my conversion function a switch might be a better way of doing it.

    The program takes a text file of 20x30 digits and converts it to an ascii picture.
    What do you guys think?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #define MAX 100
    #define ROWS 20
    #define COLS 30
    char convert(int scan);
    
    int main(void)
    {
    	int i, j, orig[ROWS][COLS];
    	char picture[ROWS][COLS+1], read[MAX+1], mover[2];
    	FILE *fp;
    
    	if ((fp = fopen("/Users/philip/pict.txt", "r")) == NULL)
    		{
    		printf("Can't open pict filename\n");
    		exit(1);
    		}
    	
    	i = 0;
    	while(fgets(read, 61, fp) != NULL)
    		{
    //		fputs(read, stdout);
    		for(j=0; j < COLS; j++)
    			{
    			strncpy(mover, read + (j*2), 2);
    //			puts(mover);
    			orig[i][j] = atoi(mover);
    			}
    		i++;
    		}
    
    /*	puts("\n");
    	for(i=0; i < ROWS; i++)
    		{
    		for(j=0; j < COLS; j++)
    			printf("%d", orig[i][j]);
    		puts("");
    		}
    */	
    	for(i=0; i < ROWS; i++)
    		{
    		for(j=0; j < COLS; j++)
    			picture[i][j] = convert(orig[i][j]);
    		picture[i][COLS] = '\0';
    		}
    
    	puts("\n");	
    	for(i=0; i < ROWS; i++)
    		puts(picture[i]);
    		
    	fclose(fp);
    	return 0;
    }
    
    char convert(int scan)
    	{
    	if (scan == 0)
    		return ' ';
    	if (scan == 1)
    		return '.';
    	if (scan == 2)
    		return '\'';
    	if (scan == 3)
    		return '\"';
    	if (scan == 4)
    		return '-';
    	if (scan == 5)
    		return '*';
    	if (scan == 6)
    		return '=';
    	if (scan == 7)
    		return '%';
    	if (scan == 8)
    		return '&';
    	if (scan == 9)
    		return '#';
    	
    	return '?';
    	}

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Of course.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    Quote Originally Posted by MacGyver View Post
    Of course.
    with a switch though will return break or will I need return than break. I know return is supposed to exit the function but I haven't used switches in functions yet so I just wanted to make sure.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Breaking from a switch just places control at the end of the switch statement. Why would you need to formally break from a switch?

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    that's what I mean, does the return halt the switch and pull the program out of the function?

    switch(1)
    return 1;
    switch(2)
    return 2;

    will that work is what I'm double checking, I'll find out soon enough when I'm done adding more functions to the program I suppose but I was just wondering before I try it

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The answer to my question is, "You don't." At least if you're returning from a function because returning from a function does just that. It passes control from the function it's currently in to whatever function called it. The break statement in a switch just passes control to the end of the function.

    You don't need to "halt the switch". There's nothing formal about a break vs a return. I think you're thinking about switches in a way that should be reserved for something like functions.

    Since I'm feeling in a very good mood, I'll translate this for you:

    Code:
    char convert(int scan)
    {
    	switch(scan)
    	{
    		case 0:
    			return ' ';
    		case 1:
    			return '.';
    		case 2:
    			return '\'';
    		case 3:
    			return '\"';
    		case 4:
    			return '-';
    		case 5:
    			return '*';
    		case 6:
    			return '=';
    		case 7:
    			return '&#37;';
    		case 8:
    			return '&';
    		case 9:
    			return '#';
    		default:
    			return '?';
    	}
    }
    Now here's an even better (but untested) way to do it:

    Code:
    char convert(int scan)
    {
    	char *szAlphabet = " .\'\"\-*=%&#";
    	char ret = '?';
    
    	if((scan >= 0) && (scan <= 9))
    	{
    		ret = szAlphabet[scan];
    	}
    
    	return ret;
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Return exits the function immediately [1] . break exits the switch/for/while that it's in.

    Func1 and Func2 are equivalent ways to write code:
    Code:
    void func1(void) {
        if (x)  goto end;
        .... 
        ....
    end:
    }
    
    void func2(void) {
       if (x) return;
       ...
       ...
    }
    Chances are, indeed, that the compiler generates exactly the same code for both of these functions.

    --
    Mats

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    char convert(int scan)
    {
        static const chars[] = " .\'\"-*=&#37;&#";
        if(scan < 0 || scan > 9) return '?';
        return chars[scan];
    }
    Work smarter not harder.

    EDIT: MacGyver got there first.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    ah thanks guys dunno why I didn't think to just make an array for the returns, but now I'm having a little problem, I'm trying to add a fix or de-"glitching" function that compares a spot versus its neighbors. It works fine except along the edges I'm trying to figure a way to handle those values. Obviously I could easily just set up one loop for passing everything inside the box then make seperate loops for the edges and handle them seperately, but I want to get the function able to handle it, what I've made is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #define MAX 100
    #define ROWS 20
    #define COLS 30
    char convert(int scan);
    int fix(int center, int up, int down, int left, int right);
    
    int main(void)
    {
    	int i, j, orig[ROWS][COLS];
    	char picture[ROWS][COLS+1], read[MAX+1], mover[2];
    	FILE *fp;
    
    	if ((fp = fopen("/Users/philip/pict.txt", "r")) == NULL)
    		{
    		printf("Can't open pict filename\n");
    		exit(1);
    		}
    	
    	i = 0;
    	while(fgets(read, 61, fp) != NULL)
    		{
    //		fputs(read, stdout);
    		for(j=0; j < COLS; j++)
    			{
    			strncpy(mover, read + (j*2), 2);
    //			puts(mover);
    			orig[i][j] = atoi(mover);
    			}
    		i++;
    		}
    
    	for(i=0; i < ROWS; i++)
    		for(j=0; j < COLS; j++)
    			orig[i][j] = fix(orig[i][j], orig[i+1][j], orig[i-1][j], orig[i][j-1], orig[i][j+1]);
    	
    /*	puts("\n");
    	for(i=0; i < ROWS; i++)
    		{
    		for(j=0; j < COLS; j++)
    			printf("&#37;d", orig[i][j]);
    		puts("");
    		}
    */	
    	for(i=0; i < ROWS; i++)
    		{
    		for(j=0; j < COLS; j++)
    			picture[i][j] = convert(orig[i][j]);
    		picture[i][COLS] = '\0';
    		}
    
    	puts("\n");	
    	for(i=0; i < ROWS; i++)
    		puts(picture[i]);
    		
    	fclose(fp);
    	return 0;
    }
    
    int fix(int center, int up, int down, int left, int right)
    	{
    	int check = 0, a = 0;
    	
    	if(up != NULL)
    		if ((center - up) > 1 || (center - up) < -1)
    			{
    			check++;
    			a++;
    			}
    	if(down != NULL)
    		if ((center - down) > 1 || (center - down) < -1)
    			{
    			check++;
    			a++;
    			}
    	if(left != NULL)
    		if ((center - left) > 1 || (center - left) < -1)
    			{
    			check++;
    			a++;
    			}
    	if(right != NULL)
    		if ((center - right) > 1 || (center - right) < -1)
    			{
    			check++;
    			a++;
    			}
    	
    	if(check == 4)
    		return (up+down+left+right)/a;
    	else
    		return center;
    	}
    
    char convert(int scan)
    	{
    	if (scan == 0)
    		return ' ';
    	if (scan == 1)
    		return '.';
    	if (scan == 2)
    		return '\'';
    	if (scan == 3)
    		return '\"';
    	if (scan == 4)
    		return '-';
    	if (scan == 5)
    		return '*';
    	if (scan == 6)
    		return '=';
    	if (scan == 7)
    		return '%';
    	if (scan == 8)
    		return '&';
    	if (scan == 9)
    		return '#';
    	
    	return '?';
    	}
    and obviously NULL is just saying "0" to it when its working with int so I'm not sure how I can check after the values are passed (though I could pass i and j and have if statements inside the function deciding if it's on an edge or not, but that would seem just as over complicated as setting up seperate for loops for the edges.

    Also I swapped in brewbuck's shortened switch made and it replaces most higher numbers in the pict with ) or ( so I'll have to mess around and see why that's not working right. Though MacGyver's works fine.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Think I made a mistake in mine and put a few too many backslashes btw... Brewbucks appears more correct actually. If you are having weird things happen, you might be corrupting memory somewhere.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    even with your slash (which I removed after running when I noticed it) didn't seem to throw anything off since it took it as an escape command and so \- only took up one space in the alphabet array instead of two)

    But no tips on how to sense edges inside the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM