Thread: Help with this piece of code to multiply a string by two

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Help with this piece of code to multiply a string by two

    Below is a piece of code in a program I am making. This function takes in a char* and int argument, the char pointing to a string of numbers, and the int representing the length of the array.

    When I run it like this, with the code commented out, and input 1234

    Code:
    int multiplyByTwo(char *fraction, int fraction_length)
    {
    printf("last_Cell = %d",fraction_length);
    printf("\n\nfraction[j] =" );
    int j;
    for(j=fraction_length;j>=0;j--){
            fraction[j+1] = fraction[j];
            }
    
    
    fraction[0] = 0; printf("\n\n");
    for(j=0;j<fraction_length+1;j++)
            printf("%d ",fraction[j]);
    }
    /*
    int i = fraction_length+1;
    for(i; i >=1; i++){
            fraction[i] *= 2;
            
            if(fraction[i] >=10){
                    fraction[i] -= 10;
                    fraction[i-1] += 1;
                    }       
            }       
    int x;
    for(x=0;x<=fraction_length;x++)
    printf("%d ",fraction[x]);      
    }
    */
    the output is:

    f[0] = 1 f[1] = 2 f[2] = 3 f[3] = 4 fractionlength = 4


    0 1 2 3 4


    When I include the commented out code, however, the output becomes:

    f[0] = 1 f[1] = 2 f[2] = 3 f[3] = 4 fractionlength = 4
    last_Cell = 4


    Segmentation fault (core dumped)



    What in the bejeezus is going on!?

    EDIT: Sorry, the top line of output is actually coming from main, those are just tests that I ran on the parameters before calling the function.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Array indexing out of bounds, f[4] is undefined. Simply get rid of the "+1" on your last for loop.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    In particular, why would the last printf statement, which printed the results of my moved array just fine sans the code, now stop working, even if there was something wrong with the code after it.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Wait, what? What loop are you referring to? The last loop before the comments works fine when the commented code is not included. Why would it stop working? Also, the whole point is to move each cell of the array up one block, and leave a 0 in the first cell. It looks like that's exactly what is happening when the code is commented out, but not so much when it is included.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You're accessing arrays out of bounds all over the place. Even the first loop:
    Code:
    int j;
    for(j=fraction_length;j>=0;j--){
            fraction[j+1] = fraction[j];
            }
    You start off by trying to access fraction[4], which is already one index past the array bounds. Then you try to access one past that with the "j + 1" statement. If fraction_length is equal to four, then you can only access 0-3. Not 0-5 like your code is doing.

    Code:
    for(j=0;j<fraction_length+1;j++)
            printf("%d ",fraction[j]);
    }
    You're making the same mistake here, fortunately, it's easier to fix. Simply remove the "+1" and it will not overrun the bounds of the array.


    Quote Originally Posted by Avanish Giri View Post
    In particular, why would the last printf statement, which printed the results of my moved array just fine sans the code, now stop working, even if there was something wrong with the code after it.
    ...uh....what? I don't know what you had before, but it was probably because undefined behavior can sometimes seem to "work", or you hadn't tried to access it out of bounds before printing.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    How could that be it though. I probably should have mentioned this, but I initialized the array to 50 cells. I'm merely trying to access the 4th or 5th or 6th one.

    Just to be sure, I left the code in tact, and deleted the commented portion that I contend is causing some bizarre seg fault. Here is the relevant code. I know I'm probably missing some grave mistake here but if anyone could explain why this is happening I will be very much in your debt.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int multiplyByTwo(char*, int);
    int divideByTwo(char*, int);
    
    
    int main(){
    
    
    char array[256];
    char fraction[50];
    fgets(array,255,stdin);
    int f;
    
    
    int last_cell = strlen(array)-2;
    int j;
    
    
    
    
    for(j=0;j<last_cell;j++){
        if(array[j] == '.')break;
        }
    int decimal_location = j;
    int fraction_length = last_cell - decimal_location;
    for(f=0;f<strlen(array)-1;f++){
        array[f] -= 48;
        }
    int k=0;
    for(j++;j<=last_cell;j++,k++){
        fraction[k] = array[j];
        printf("f[%d] = %d ",k,fraction[k]);
        }
    fraction[k+1] == 0;
    fraction[k+2] == 0;
    printf("fractionlength = %d\n",fraction_length - 1);
    multiplyByTwo(fraction, fraction_length - 1);
    printf("\ndecimal_location = %d\n",decimal_location);
    printf("last_cell == %d\n",last_cell);
    int num = 0;
    int binary_array[50];
    int s;
    printf("Integral divided by two == ");
    divideByTwo(array, decimal_location - 1);
    }
    
    
    int multiplyByTwo(char *fraction, int fraction_length)
    {
    printf("last_Cell = %d",fraction_length);
    int j;
    for(j=fraction_length;j>=0;j--){
        fraction[j+1] = fraction[j];
        }
    
    
    fraction[0] = 0; printf("\n\n");
    for(j=0;j<fraction_length+2;j++)
        printf("%d ",fraction[j]);
    }

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Grr...okay, I'll compile it.

    Wait, no, I won't, because the linker complains at me. But looking at the warnings, you need to get a better compiler, or start listening to it, because you have unused variables, misused "==" symbols, and failure to return values at the ends of functions.

    After deleting the functions that make the linker fail, I can't get it to segfault. What were you putting in?

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Firstly, thank you so much for spending time helping me.

    I added those == in a rush after reading your first reply, saying I was going out of bounds. I thought maybe I should initialize them to 0 since it could be holding a '/0'

    It only Seg faults when I include the block of code in the FIRST post, the one that is Commented out in green. Something in there causes it to seg fault. Any ideas?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well in the commented out loop i starts at fraction_length+1 and then goes up to... well probably something less than 0x7FFFFFFF seeing as how it'll crash well before it gets that high.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    LOL!! That was it... but I'm crying on the inside. God how did I miss that, thanks!

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Avanish Giri View Post
    I added those == in a rush after reading your first reply, saying I was going out of bounds.
    You should still recompile and link your code from scratch, fix any errors or warnings before trying your code and then posting the result if it still doesn't work.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R2 - Piece of code not working.
    By reqonekt in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 11:07 AM
  2. what's wrong with this piece of code
    By studentc in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2006, 07:58 PM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  5. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 07:12 AM