Thread: help me trace this code

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    help me trace this code

    Code:
     #include<stdio.h>
     #include<string.h>
     char r[6];
     unsigned result[156];
     unsigned temp[156];
     int len()
     {
     	int i;
     	for(i=0;i<=156;i++)
     	{
     		if(result[i]!=0)
     			break;
     	}
     	return 156-i;
     }
     void multiplication()
     {
     	int i,j;
     
     	memset(temp,0,sizeof(temp));
     
     	for(i=5;i>=1;i--)
     	{
     		for(j=155;j>155-len();j--)
     		{
     			temp[j+i-5]+=r[i]*result[j];
     		}
     	}
     	
     	for(i=155;i>=0;i--)
     	{
     		if(temp[i]>=10)
     		{
     			temp[i-1]+=temp[i]/10;
     			temp[i]%=10;
     		}
     	}
     
     	for(i=0;i<156;i++)
     	{
     		result[i]=temp[i];
     	}
      }
    int main()
     {
     	int n;
     	int m;
     	int j;
     	int i;
     	int endPoint=156;
     
     	while(scanf("%s%d",r,&n)==2)
     	{
     		int bStartOutput=0;
     		int bInteger=1;
     		for(j=2;j>0;j--)
     		{
     			if(r[j]=='.')
     				break;
     		}
     		m=5-j;
     		m=m*n;
     
     		for(i=(j-1);i>=0;i--)
     		{
     			r[i+1]=r[i];
     		}
     		r[0]='0';
    
     		for(i=0;i<6;i++)
     		{
     			r[i]-='0';
     		}
            
     		memset(result,0,sizeof(result));
     
     		n--;
     		for(i=1;i<6;i++)
     		{
     			result[i+150]=r[i];
     		}
    		
     		while(n--)
     		{
     			multiplication();
     		}
     
     		for(i=1;i<156-m;i++)
     		{
     			result[i-1]=result[i];
     		}
    
     		result[156-m-1]=100;
    
     		for(i=156-m;i<156;i++)
     		{
     			if(result[i])
     			{
     				bInteger=0;
     				break;
     			}
     		}
    
     		if(bInteger)
     		{
     			for(i=0;i<156;i++)
     			{
     				if(bStartOutput)
     				{
     					if(result[i]==100)
     						break;
     					else 
     						printf("%d",result[i]);
     				}
     				else if(result[i])
     				{
     					printf("%d",result[i]);
     					bStartOutput=1;
     				}
     			}
     		}
     		else
     		{
     			for(i=155;i>=0;i--)
     			{
     				if(result[i]==0)
     					endPoint=i;
     				else
     					break;
     			}
     			for(i=0;i<endPoint;i++)
     			{
     				if(bStartOutput)
     				{
     					if(result[i]!=100)
     						printf("%d",result[i]);
     					else
     						printf(".");
     				}
     				else if(result[i])
     				{
     					bStartOutput=1;
     					if(result[i]!=100)
     						printf("%d",result[i]);
     					else
     						printf(".");
     				}
     			}
     		}
     		
     	}
     }
    my question in this program is in the function multiplication()
    the line:
    Code:
    temp[j+i-5]+=r[i]*result[j];
    how did that happen that the string array become integer without any conversion
    and how that string of characters involved in multiplication with correct value....

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Each character in the character array is really a one byte number. It will be treated as a one byte integer for the multiplication even though that's probably not what you want the program to do. What do you want the program to do?
    Last edited by Zlatko; 08-15-2009 at 09:53 AM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Code:
     int len()
     {
     	int i;
     	for(i=0;i<=156;i++)
     	{
     		if(result[i]!=0)
     			break;
     	}
     	return 156-i;
     }
    i < 156

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea what your point is. Does the following help illustrate the difference?

    Code:
    #include <stdio.h>
    
    int main() {
        char number[10] = "123456789";
    
        printf("%c %c %c\n", number[2], number[5], number[8]); /*print the characters as characters*/
        printf("%d %d %d\n", number[2], number[5], number[8]); /*print the value of each character*/
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    Code:
    1 void multiplication()
    2  {
    3 	int i,j;
    4 
    5 	memset(temp,0,sizeof(temp));
    6 
    7 	for(i=5;i>=1;i--)
    8 	{
    9 		for(j=155;j>155-len();j--)
    10 		{
    11 			temp[j+i-5]+=r[i]*result[j];
    12 		}
    13 	}
    14 	
    15 	for(i=155;i>=0;i--)
    16 	{
    17 		if(temp[i]>=10)
    18 		{
    19 			temp[i-1]+=temp[i]/10;
    20 			temp[i]%=10;
    21 		}
    22 	}
    23 
    24 	for(i=0;i<156;i++)
    25 	{
    26 		result[i]=temp[i];
    27 	}
    28 }
    if you would scan that function multiplication... you would notice in line
    11 temp[j+i-5]+=r[i]*result[j]; // the r[i] is use if you would scan the entire program
    that is a character array and you would not get the right value because even if you
    enter a character '1' in the array the value would be different because the equivalent
    value of in ascii is 49...but if you trace that program...it is correct you check that
    difference by putting this code after line 11 printf("%d",r[i]); //this would print the exact
    character you enter...if you enter '1' then if you use that conversion (%d) it would print
    1 instead of 49....umm pls. run the code so that you could understand me....

  6. #6
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    The digits of 'r' are converted from their ascii representation to the actual number of the digit in the main program at
    Code:
            for(i=0;i<6;i++)
            {
                r[i]-='0';
            }
    I don't understand why the first digit is always reset to '0' just before the conversion. See the line r[0]='0'; just before the conversion loop.

    Does that answer your question?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    thanks zlatko...i missed that part

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM