help in transforming into a general case..

This is a discussion on help in transforming into a general case.. within the C Programming forums, part of the General Programming Boards category; i wrote this working code which transforms a string of one number into integer using recursion Code: #include <stdio.h> int ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    help in transforming into a general case..

    i wrote this working code which transforms a string of one number into integer using recursion
    Code:
    
    #include <stdio.h>
     int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    	int res;
        char buf[] ="-000123		";
    	res=one_num(buf,0,0);
        printf("%d",res);
    return 0;
    }
    int one_num(char buf[],int index,int start)
    {
    int y=1,x = 0;
     
    	if(buf[start]=='-')
         {
            y=-1;
    		index++;
          }
    	if(buf[start]=='+')
         {
             index++;
          }
    	x=rid(buf,index,0)*y;
    return x;
    
    }
    
    int rid(char buf[],int index,int x)
    {
    if ((buf[index]<'0')||(buf[index]>'9'))
    {
    return x;
    }
       x *= 10;
       x += buf[index]-'0';
       
    return rid(buf,index+1,x);
    
    }
    i want to make a program which transforms number like "-123 56 8 "
    into numbers of an array
    i wrote a code wich should find the next number and transforms it.
    but its not working
    rid_next should return the index of the next number
    but its not working
    Code:
    #include <stdio.h>
    int rid_next(char buf[],int index,int x);
    void spin(char buf[],int arr[],int index,int num);
    int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    	
    	int arr[2];
        char buf[] ="-000123		143		";
    
    	spin(buf,arr,0,0);
    	printf("%d %d\n",arr[0],arr[1]);
    return 0;
    }
    void spin(char buf[],int arr[],int index,int num)
    {
    
    	int loc;
        
    	if (index=='\0')
    	{
           return;
    	}
    	arr[num]=one_num(buf,index, index);
    	loc=rid_next( buf,index,0);
    	
        spin(buf,arr,loc,num+1);
    }
    
    int one_num(char buf[],int index,int start)
    {
    int y=1,x = 0;
     
    	if(buf[start]=='-')
         {
            y=-1;
    		index++;
          }
    	if(buf[start]=='+')
         {
             index++;
          }
    	x=rid(buf,index,0)*y;
    return x;
    
    }
    
    int rid(char buf[],int index,int x)
    {
    if ((buf[index]<'0')||(buf[index]>'9'))
    {
    return x;
    }
       x *= 10;
       x += buf[index]-'0';
       
    return rid(buf,index+1,x);
    
    }
    
    int rid_next(char buf[],int index,int x)
    {
         if ((buf[index]<'0')||(buf[index]>'9'))
    {
         return index;
    }
       
        return rid(buf,index+1,x);
    
    }
    ??

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i added another function nex_num which comes after rid_next
    which i think i should point to the next number
    but its not working
    ??
    Code:
    #include <stdio.h>
    int nex_num(char buf[],int index);
    int rid_next(char buf[],int index,int x);
    void spin(char buf[],int arr[],int index,int num);
    int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    	
    	int arr[2];
        char buf[] ="-000123		143		";
    
    	spin(buf,arr,0,0);
    	printf("%d %d\n",arr[0],arr[1]);
    return 0;
    }
    void spin(char buf[],int arr[],int index,int num)
    {
    
    	int loc;
        
    	if (index=='\0')
    	{
           return;
    	}
    	arr[num]=one_num(buf,index, index);
    	loc=rid_next( buf,index,0);
    	loc=nex_num(buf,loc);
        spin(buf,arr,loc,num+1);
    }
    
    int one_num(char buf[],int index,int start)
    {
    int y=1,x = 0;
     
    	if(buf[start]=='-')
         {
            y=-1;
    		index++;
          }
    	if(buf[start]=='+')
         {
             index++;
          }
    	x=rid(buf,index,0)*y;
    return x;
    
    }
    
    int rid(char buf[],int index,int x)
    {
    if ((buf[index]<'0')||(buf[index]>'9'))
    {
    return x;
    }
       x *= 10;
       x += buf[index]-'0';
       
    return rid(buf,index+1,x);
    
    }
    
    int nex_num(char buf[],int index)
    {
       if ((buf[index]>='0')&&(buf[index]<='9'))
       {
          return index;
        }
       
    return nex_num(buf,index+1);
    
    }
    int rid_next(char buf[],int index,int x)
    {
         if ((buf[index]<'0')||(buf[index]>'9'))
        {
            return index;
        }
       
         return rid(buf,index+1,x);
    
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    you probably meant rid_next instead of rid... but what's the purpose of x? I would attempt something like:
    Code:
    int rid_next(char buf[],int size,int index) {
      if(index>size) return some_special_failure_value; // ?
      if(isdigit(buf[index])) return index; // !isdigit(buf[index]) is better
      return rid_next(buf,++index);
    }
    Is it still in your specifications to make it recursive?

    i added another function nex_num which comes after rid_next
    which i think i should point to the next number
    but its not working
    so what? you stack bugs to make things more complicated to fix? focus on one problem, solve it and resume with what's left, besides your code is not an example of clarity. You should not 'think' a function does something, be sure it does. Test it.
    Last edited by root4; 01-30-2009 at 03:22 PM.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes only recurtion are allowed
    my function are working in the base case.
    x in that function is useless.
    i called thos function
    so the first will get me to the first char after the number
    and the next one should go till it reaches the start of the number
    but i cant see why its not working??

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    right, I misread your code.

    your rid_next() doesn't work because you want it to return the index following the first token read, and to do that you use rid() which does the job but returns the converted value (token to integer), and not an index, so you simply can't use rid() in rid_next(). Either you recode rid_next() as rid() but returning an index or you modify rid() to keep a track of the last index used.
    Last edited by root4; 01-30-2009 at 03:30 PM.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thats exactly what i have written here
    correct?
    Code:
    int nex_num(char buf[],int index)
    {
       if ((buf[index]>='0')&&(buf[index]<='9'))
       {
          return index;
        }
       
    return nex_num(buf,index+1);
    }

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Yep, i misunderstood your code, this is fixed in my previous post.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I have been thinking, transgalactic2, that your solutions to your prof's specifications are becoming as sadistic as the specs themselves

    Anyway, here's an example of what I meant by using a getchar() based function to process your input. I obeyed all the rules you have given us so far, I think. The function will give you an array of strings, each one containing only numbers, and null terminated. That means you don't have to parse any spaces out!
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int getintray (char **string, int len, int count, const int size) {
    	int ch=getchar();
    	if (ch==' ') {
    		if ((len>0) && (count==size-1)) return len;
    		if (len>0) string[count++][len]='\0'; 
    		return getintray(string,0,count,size);
    	} else if (ch=='\n') { 
    		if ((len>0) && (count==size-1)) return len;
    		else {	
    			printf("More numbers please: ");
    			if (len>0) string[count++][len]='\0';
    			return getintray(string,0,count,size); 
    		}
    	} else if ((ch>=48) && (ch<=57)) {
    		if (len==0) string[count]=malloc(256);
    		string[count][len++]=ch;
    		return getintray(string,len,count,size);
    	}
    	else return getintray(string,len,count,size);
    } 
    	
    
    int main() {
    	int len, i;
    	char *string[4];
    	printf("Enter 4 numbers seperated by spaces: ");
    	len=getintray(string,0,0,4);
    	for (i=0; i<4; i++) {	/* a loop for the demo */
    		printf("%d: \"%s\"\n",i,string[i]);
                    free(string[i]);   /* remember, malloc was used! */
    	}
    	return 0;
    }
    It ignores garbage and won't give up, eg:
    Code:
    Enter 4 numbers seperated by spaces: 45
    More numbers please: 13
    More numbers please: 44 98
    0: "45"
    1: "13"
    2: "44"
    3: "98"
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces: sdfgsa 33     56ads 17          
    More numbers please: 4
    0: "33"
    1: "56"
    2: "17"
    3: "4"
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces:    1    2   124351234621  4   
    0: "1"
    1: "2"
    2: "124351234621"
    3: "4"
    Enter 4 numbers seperated by spaces: 4123 89239 6279 27
    0: "4123"
    1: "89239"
    2: "6279"
    3: "27"
    It doesn't process signs (+/-) -- for that you will have to do some hacking
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use pointers and malloc
    and it doesnt need to ask for more numbers
    if not all numbers were entered it needs to return 0
    if i enter 5 numbers it needs to return 0
    if i enter letter like "hgm sfs" it doesnt return error and stop
    it asks for more numbers instead

    but here in every case it doesnt return an error massage or function return 0.
    how to replace that??
    Last edited by transgalactic2; 01-30-2009 at 09:01 PM.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant see why it stays on the same index
    after it converts one number
    loc=rid_next( buf,index,0); return the index where the number stops
    loc=nex_num(buf,loc); returns the index where the spaces end(start of the next one)
    i debugged it
    and i cant see why i get the same index over and over
    ??

    Code:
    
    #include <stdio.h>
    int nex_num(char buf[],int index);
    int rid_next(char buf[],int index,int x);
    void spin(char buf[],int arr[],int index,int num);
    int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    	
    	int arr[2];
        char buf[] ="-000123		143		";
    
    	spin(buf,arr,0,0);
    	printf("%d %d\n",arr[0],arr[1]);
    return 0;
    }
    void spin(char buf[],int arr[],int index,int num)
    {
    
    	int loc;
        
    	if (buf[index]=='\0')
    	{
           return;
    	}
    	arr[num]=one_num(buf,index, index);
    	loc=rid_next( buf,index,0);
    	loc=nex_num(buf,loc);
        spin(buf,arr,loc,num+1);
    }
    
    int one_num(char buf[],int index,int start)
    {
    int y=1,x = 0;
     
    	if(buf[start]=='-')
         {
            y=-1;
    		index++;
          }
    	if(buf[start]=='+')
         {
             index++;
          }
    	x=rid(buf,index,0)*y;
    return x;
    
    }
    
    int rid(char buf[],int index,int x)
    {
    if ((buf[index]<'0')||(buf[index]>'9'))
    {
    return x;
    }
       x *= 10;
       x += buf[index]-'0';
       
    return rid(buf,index+1,x);
    
    }
    
    int nex_num(char buf[],int index)
    {
       if ((buf[index]>='0')&&(buf[index]<='9'))
       {
          return index;
        }
       
    return nex_num(buf,index+1);
    
    }
    int rid_next(char buf[],int index,int x)
    {
         if ((buf[index]<'0')||(buf[index]>'9'))
        {
            return index;
        }
       
         return rid(buf,index+1,x);
    
    }
    Last edited by transgalactic2; 01-31-2009 at 08:53 AM.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    and i cant see why i get the same index over and over
    I think I explained why in an earlier post:

    your rid_next() doesn't work because you want it to return the index following the first token read, and to do that you use rid() which does the job but returns the converted value (token to integer), and not an index, so you simply can't use rid() in rid_next(). Either you recode rid_next() as rid() but returning an index or you modify rid() to keep a track of the last index used.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i fixed it thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 10:45 PM

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