1. #1
    Dreadlord Rakeer's Avatar
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    The realm of Torment
    Posts
    904

    Nedd help with a program (c++)

    So I've been trying to complete this stupid programing problem for the past week or so but I can't figure it out.
    the problem is a (seemingly) simple one.

    -take an inputed number
    -convert it to octal (base eight)
    -arrange the digits in the octal number in ascending order
    -subtract the re-arranged number, from the octal. in base eight.
    -do this 5 times, or until the digits in the octal are in ascending order
    -convert back to base 10

    heres my code:
    PHP Code:
    #include <stdAfx.h>
    #include <iostream>
    using namespace std;

    int base_eight(int number);
    int sort(int number);
    int base_ten(int number);

    void main(){
        
    int number;
        
    int sorted;
        
    int counter;
        
    int temp;

        for(
    int ia 0ia 5ia++){
            
    number 0;
            
    sorted 0;
            
    counter 0;
            
    temp 0;
            
    system("cls");
            
    cout << "Enter a number: ";
            
    cin >> number;
            
    temp number;
            
    number base_eight(number);

            while(
    1){
                
    sorted sort(number);
                
    sorted base_ten(sorted);
                
    number base_ten(number);
                if(
    number == sorted)
                    break;
                
    number number sorted;
                
    number base_eight(number);
                
    counter++;
                if(
    counter == 5)
                    break;
            }
            
    number base_ten(number);


            
    cout << number << endl;
            
    system("PAUSE");
        }
    }
    int base_eight(int number){
        
    int place[100];
        
    int first 0;
        
    int counter 0;
        
    int octal 0;
        
    int power;
        while(
    1){
            
    place[counter] = number 8;
            
    counter++;
            
    first number 8;
            
            if(
    first 8)
                break;

            
    number first;
        }

        
    place[counter] = first;

        for(
    int ia 0ia <= counteria++){
            
    power 1;
            for(
    int ib 0ib counter iaib++){
                
    power power 10;
            }
            
    octal octal + (place[counter ia] * power);
        }
        return 
    octal;
    }
    int sort(int number){
        
    //will turn 4154 into 1445
        
    int counter 0;
        
    int digit[101];
        
    int place[101];
        
    int final;
        
        
    //break the number
        
    while(1){
            
    digit[counter] = number 10;
            if(
    number == digit[counter])
                break;
            
    number number digit[counter];
            
    counter++;
            
    number number 10;
        }

        
    //number the places
        
    for(int ia 0ia <= counteria++){
            
    place[ia] = 0;
            for(
    int ib 0ib <= counterib++){
                if(
    ib != ia){
                    if(
    digit[ia] < digit[ib]){
                        
    place[ia]++;
                    }
                }
            }
            for(
    int ic 0ic <= counteric++){
                if(
    ic != ia){
                    if(
    place[ia] == place[ic])
                        
    place[ia]++;
                }
            }
        }

        for(
    int ic 0ic <= counteric++){
            for(
    int id 0id place[ic]; id++){
                
    digit[ic] = digit[ic] * 10;
            }
            final = final + 
    digit[ic];
        }

        return final;
    }
    int base_ten(int number){
        
    int digit[101];
        
    int counter 0;
        
    int final = 0;

        while(
    1){
            
    digit[counter] = number 10;
            if(
    number == digit[counter])
                break;
            
    number number digit[counter];
            
    counter++;
            
    number number 10;
        }

        for(
    int ia 0ia <= counteria++){
            for(
    int ib 0ib <= counter ia 1ib++){
                
    digit[counter ia] = digit[counter ia] * 8;
            }
            final = final + 
    digit[counter ia];
        }
        
        return final;

    I've checked the seperate functions by themselves and they work just fine. its just when i put them together i seem to get junk numbers from no where, or at least I can't find them. If any other programers can assist in pointing out my errors it will be greatly appreciated.

  2. #2
    Your first for & while loop combo is a bit crazy...

    To me,
    Code:
    while(1){
    doesn't particularly make sense to me, while what is 1? D:

    SORT:

    Code:
     for (int j=0; j < index-1; j++) { //checks the value at index 0 + index 1
                //^if it's not larger, move to the next index values (and repeat).
                for (int k=0; k < index-1; k++) { //k is 0, but while it's less than 1 keep adding 1 to k
                    //if k is greater than the number in the next array's index value, then move k up one slot
                    if (intArray[k] > intArray[k+1]) {      
                        temp = intArrayTwo[k]; //temp = whatever value is in the array at (variable called 'k')
                        intArrayTwo[k] = intArrayTwo[k+1]; //the value of k is now +1 of k
                        intArrayTwo[k+1] = temp; //k+1 is now equal to the temporary figure
                                           
                        System.out.println("Swap " + swapValue); //print how many swaps have happened
                        swapValue = swapValue+1; //Add 1 to swapvalue for each time it loops around
                        
                        UserInput.readString(); //Slow process down by requiring the user to hit 'Enter' key.                    
                    }
                }
            }
    Code:
    int base_eight(int number){
        ...
            place[counter] = number % 8;
            counter++;
            first = number / 8;
    This part doesn't look quite right.

    Personally, i'd set it up like this:

    Enter your whole number (let's say 65), then divide the number by 8 (8) then % 65 so you get 1.
    Save these numbers individually
    Index position 0 = 8
    Index position 1 = 1

    Copy array numbers

    Sort the new array

    old array - new array (Index 0 - Index 0, Index 1 - Index 1)

    convert to base 10

    output answer
    ...
    Looking back over it;
    -subtract the re-arranged number, from the octal. in base eight.
    -do this 5 times, or until the digits in the octal are in ascending order
    This doesn't really make much sense. - I can't see why would you want to subtract the re-arranged number 5 times, OR (until you) have the numbers in ascending order..?
    Last edited by Yohassakura; 2011-05-29 at 04:37 AM.
    Computer: Intel I7-3770k @ 4.5GHz | 16GB 1600MHz DDR3 RAM | AMD 7970 GHz @ 1200/1600 | ASUS Z77-V PRO Mobo|

  3. #3
    Dreadlord Rakeer's Avatar
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    The realm of Torment
    Posts
    904
    Quote Originally Posted by Yohassakura View Post
    while what is 1? D:
    while(1){} is just a form of infinite loop. essentially it means while 1 is true, or 1 is equal to one. the "break;" command exits the loop.

    maybe im misunderstanding the ideas your conveying. but the user only inputs a number once per cycle. And you have go through the process 5 times or until numbers are in acsending order cause thats what the damn problem is asking for.

    thanks for the help though, I'll try another crack at this once im not so brain dead from trying.

  4. #4
    Miss Doctor Lady Bear Sunshine's Avatar
    15+ Year Old Account
    Join Date
    Mar 2009
    Location
    San Francisco
    Posts
    15,651
    I don't have somewhere to run code easily at hand right now, but have you tried putting in a bunch of cout statements to try to debug it? Print the input number, print the octal conversion, print the rearranged number, print the result of the subtraction, print when you're exiting the loop. It might help to find what's wrong.

    You can paste it here also; maybe it'll help us see what's going wrong.

  5. #5
    Bloodsail Admiral Dassen's Avatar
    10+ Year Old Account
    Join Date
    Jan 2010
    Location
    Sweden
    Posts
    1,087
    I would check it out thoroughly but it's 5:55am where I live so my mind is working on sub-par performance, I'll check back when I wake up.
    Also, you should check out do while, it's a kind of 'better' version of the while loop (not necessarily better, as for everything in programming it depends on the situation and context).
    (I have no idea if I'm using the code formatting for these forums here)
    PHP Code:
    int a 0;
    do
    {
    cout << a;
    } while (
    != 0);

    Console would print:
    0
    ---------------------------------
    int a 0;
    while (
    != 0)
    {
    cout << a;
    }
    Console would print:
    -
    nothing 
    It does before it checks, whereas a regular while loop checks before it does.

    Edit: I learned how to insert code! Huzzah!
    Edit 2: I learned how to insert code with colors! Double huzzah!
    Edit 3: Gnarl. Now I got a craving to program. While half asleep. This won't go well.
    Edit 4: Edited code for difference clarification
    Last edited by Dassen; 2011-05-29 at 05:23 AM.
    "After dealing with about 10 000 patients over the last 15 years, I would say that over 200 different medical conditions respond favorably to cannabis."
    - The late Dr. Tod Mikuriya, MD, interview in The Union: The business behind getting high
    Former national administrator of the U.S. Government's marijuana research programs
    http://yourlogicalfallacyis.com/home

  6. #6
    Dreadlord Rakeer's Avatar
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    The realm of Torment
    Posts
    904
    Quote Originally Posted by Sunshine View Post
    I don't have somewhere to run code easily at hand right now, but have you tried putting in a bunch of cout statements to try to debug it? Print the input number, print the octal conversion, print the rearranged number, print the result of the subtraction, print when you're exiting the loop. It might help to find what's wrong.

    You can paste it here also; maybe it'll help us see what's going wrong.
    yes i have done that, i've tested it with 3418 and 123. 3418 worked perfectly all throughout (but oddly not without the cout statements >.>) and 123 returned 24 which I'm getting based on paper math is wrong, should be 28. ill run like 5 tests and post it up here in a few mins.

    ---------- Post added 2011-05-28 at 10:19 PM ----------

    PHP Code:
    Enter a number3418

    base eight conversion 1
    6532

    sort 1
    :
    ...
    in base 101262
    number in base ten
    3418
    number
    -sorted2156

    sort 2
    :
    ...
    in base 10805
    number in base ten
    2156
    number
    -sorted1351

    sort 3
    :
    ...
    in base 10175
    number in base ten
    1351
    number
    -sorted1176

    sort 4
    :
    ...
    in base 10147
    number in base ten
    1176
    number
    -sorted1029

    sort 5
    :
    ...
    in base 1021
    number in base ten
    1029
    number
    -sorted1008

    number in base 8
    1760
    final number1008 
    answer is suppose to be 1008, so that one is good for some reason : /

    PHP Code:
    Enter a number123
    base eight conversion 1
    173

    sort 1
    :
    number in base ten123
    number
    -sorted28

    sort 2
    :
    number in base ten28

    number in base 8
    28
    final number24 
    answer is spose to be 28.

    PHP Code:
    Enter a number5049
    base eight conversion 1
    11671

    sort 1
    :
    number in base ten5049
    number
    -sorted322

    sort 2
    :
    number in base ten322
    number
    -sorted301

    sort 3
    :
    number in base ten301

    number in base eight
    301
    final number193 
    answer is suppose to be 301.
    changes inbetween the /*/
    PHP Code:
    void main(){
        
    int number;
        
    int sorted;
        
    int counter;
        
    int temp;

        for(
    int ia 0ia 5ia++){
            
    number 0;
            
    sorted 0;
            
    counter 0;
            
    temp 0;
            
    system("cls");
            
    cout << "Enter a number: ";
            
    cin >> number;
            
    temp number;
            
    number base_eight(number);
            [
    B]cout << "base eight conversion " << counter << ": " << number << endl;[/B]

            while(
    1){
                
    sorted sort(number);
                
    /*/cout << "sort " << counter + 1 << ": " << endl;/*/
                
    sorted base_ten(sorted);
                
    /*/cout << "...in base 10: " << sorted << endl;/*/    
                            
    number base_ten(number);
                
    /*/cout << "number in base ten: " << number << endl;/*/
                
    if(number == sorted)
                    break;
                
    number number sorted;
                
    /*/cout << "number-sorted: " << number << endl;/*/
                
    number base_eight(number);
                
    /*/cout << "...in base 10" << endl;/*/
                
    counter++;
                if(
    counter == 5)
                    break;
            }
            
    /*/cout << "number in base 8: " << number << endl;/*/
            
    number base_ten(number);
            
    cout << "final number: " << number << endl;


            
    cout << number << "." << endl;
            
    system("PAUSE");
        }

    now that i look at it this way i see that i am getting the answers just before the end. bah. ill be trying some more on this tomorrow after i sleep some. thanks in advance for your guys help.
    Last edited by Rakeer; 2011-05-29 at 04:24 AM.

  7. #7
    Sorry, the swap that i mentioned is actually supposed to be more like:

    (Note, index has to be at the last number, for this configuration to work, so i guess that this would be equivalent to your 'counter'.)

    Code:
     for (int j=0; j < index-1; j++) { //checks the value at index 0 + index 1
                //^if it's not larger, move to the next index values (and repeat).
                for (int k=0; k < index-1; k++) { //k is 0, but while it's less than 1 keep adding 1 to k
                    //if k is greater than the number in the next array's index value, then move k up one slot
                    if (intArray[k] > intArray[k+1]) {      
                        temp = intArrayTwo[k]; //temp = whatever value is in the array at (variable called 'k')
                        intArrayTwo[k] = intArrayTwo[k+1]; //the value of k is now +1 of k
                        intArrayTwo[k+1] = temp; //k+1 is now equal to the temporary figure
                                           
                        System.out.println("Swap " + swapValue); //print how many swaps have happened
                        swapValue = swapValue+1; //Add 1 to swapvalue for each time it loops around
                        
                        UserInput.readString(); //Slow process down by requiring the user to hit 'Enter' key.                    
                    }
                }
            }
    I blame the time for my mistake, will edit above post... And will hopefully have the answer soon.

    Edit; Too bad my c++ compiler doesn't seem to actually work. Stupid MS -_-'
    Last edited by Yohassakura; 2011-05-29 at 05:50 AM.
    Computer: Intel I7-3770k @ 4.5GHz | 16GB 1600MHz DDR3 RAM | AMD 7970 GHz @ 1200/1600 | ASUS Z77-V PRO Mobo|

  8. #8
    Miss Doctor Lady Bear Sunshine's Avatar
    15+ Year Old Account
    Join Date
    Mar 2009
    Location
    San Francisco
    Posts
    15,651
    Ah, I see the problem. Notice this:
    Quote Originally Posted by Rakeer View Post
    Code:
    number in base ten: 28
    
    number in base 8: 28
    Plus, the fact that it only happens where you exit the loop early, not the one where you finish 5 iterations.

    Not going to post the full actual answer, since it's still good practice to find that yourself.

  9. #9
    Dreadlord Rakeer's Avatar
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    The realm of Torment
    Posts
    904
    THanks for the help guys, finally got it after sunshine pointed out the loop thing. but I have made on odd thing. I used a bunch of cout statements (as in i was outputting every variable after it was changed even in the most tedial way) and it worked. But if i remove them the program doesn't work lol >.>

  10. #10
    Miss Doctor Lady Bear Sunshine's Avatar
    15+ Year Old Account
    Join Date
    Mar 2009
    Location
    San Francisco
    Posts
    15,651
    Can you post the complete code now with the commented out statements? That sounds pretty weird.

  11. #11
    Dreadlord Rakeer's Avatar
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    The realm of Torment
    Posts
    904
    PHP Code:
    #include <stdAfx.h>
    #include <iostream>
    using namespace std;

    int base_eight(int number);
    int sort(int number);
    int base_ten(int number);
    int calculate(int number);

    void main(){
        
    int number;
        
    int temp;

        for(
    int ia 0ia 5ia++){
            
    cout << "Enter a number: ";
            
    cin >> number;
            
    temp number;

            
    number calculate(number);
            
    system("cls");
            
    cout << "The 'Botchagaloop' of " << temp << " is " << number << "." << endl;
            
    system("PAUSE");
            
    system("cls");
        }
    }
    int base_eight(int number){
        
    int place[100];
        
    int first 0;
        
    int counter 0;
        
    int octal 0;
        
    int power;
        while(
    1){
            
    place[counter] = number 8;
            
    counter++;
            
    first number 8;
            
            if(
    first 8)
                break;

            
    number first;
        }

        
    place[counter] = first;

        for(
    int ia 0ia <= counteria++){
            
    power 1;
            for(
    int ib 0ib counter iaib++){
                
    power power 10;
            }
            
    octal octal + (place[counter ia] * power);
        }
        return 
    octal;
    }
    int sort(int number){
        
    //will turn 4154 into 1445
        
    int counter 0;
        
    int digit[101];
        
    int place[101];
        
    int final;
        
        
    //break the number
        
    while(1){
            
    digit[counter] = number 10;
            if(
    number == digit[counter])
                break;
            
    number number digit[counter];
            
    counter++;
            
    number number 10;
        }

        
    //number the places
        
    for(int ia 0ia <= counteria++){
            
    place[ia] = 0;
            for(
    int ib 0ib <= counterib++){
                if(
    ib != ia){
                    if(
    digit[ia] < digit[ib]){
                        
    place[ia]++;
                    }
                }
            }
            for(
    int ic 0ic <= counteric++){
                if(
    ic != ia){
                    if(
    place[ia] == place[ic])
                        
    place[ia]++;
                }
            }
        }

        for(
    int ic 0ic <= counteric++){
            for(
    int id 0id place[ic]; id++){
                
    digit[ic] = digit[ic] * 10;
            }
            final = final + 
    digit[ic];
        }

        return final;
    }
    int base_ten(int number){
        
    int digit[101];
        
    int counter 0;
        
    int final = 0;

        while(
    1){
            
    digit[counter] = number 10;
            if(
    number == digit[counter])
                break;
            
    number number digit[counter];
            
    counter++;
            
    number number 10;
        }

        for(
    int ia 0ia <= counteria++){
            for(
    int ib 0ib <= counter ia 1ib++){
                
    digit[counter ia] = digit[counter ia] * 8;
            }
            final = final + 
    digit[counter ia];
        }
        
        return final;
    }
    int calculate(int number){
        
    int sorted;
        
    number base_eight(number);
        
    cout << "In base eight: " << number << endl;
        
    sorted sort(number);
        
    cout << "Base eight sorted: " << sorted << endl;

        
    number base_ten(number);
        
    cout << "Original number in base ten: " << number << endl;
        
    sorted base_ten(sorted);
        
    cout << "Sorted number in base ten: " << number << endl;
        
    cout << number;
        
    number number sorted;
        
    cout << "-" << sorted << " = " << number << endl;
        
    system("PAUSE");
        
    system("cls");

        
    number base_eight(number);
        
    cout << "New number in base eight: " << number << endl;
        
    sorted sort(number);
        
    cout << "New number sorted: " << sorted << endl;
        if(
    sorted == number){
            
    number base_ten(number);
            return 
    number;
        }

        
    number base_ten(number);
        
    cout << "New number in base ten: " << number << endl;
        
    sorted base_ten(sorted);
        
    cout << "New sorted in base ten: " << sorted << endl;
        
    cout << number;
        
    number number sorted;
        
    cout << "-" << sorted << " = " << number << endl;
        
    system("PAUSE");
        
    system("cls");

        
    number base_eight(number);
        
    cout << "New number in base eight: " << number << endl;
        
    sorted sort(number);
        
    cout << "New number sorted: " << sorted << endl;
        if(
    sorted == number){
            
    number base_ten(number);
            return 
    number;
        }

        
    number base_ten(number);
        
    cout << "New number in base ten: " << number << endl;
        
    sorted base_ten(sorted);
        
    cout << "New sorted in base ten: " << sorted << endl;
        
    cout << number;
        
    number number sorted;
        
    cout << "-" << sorted << " = " << number << endl;
        
    system("PAUSE");
        
    system("cls");

        
    number base_eight(number);
        
    cout << "New number in base eight: " << number << endl;
        
    sorted sort(number);
        
    cout << "New number sorted: " << sorted << endl;
        if(
    sorted == number){
            
    number base_ten(number);
            return 
    number;
        }

        
    number base_ten(number);
        
    cout << "New number in base ten: " << number << endl;
        
    sorted base_ten(sorted);
        
    cout << "New sorted in base ten: " << sorted << endl;
        
    cout << number;
        
    number number sorted;
        
    cout << "-" << sorted << " = " << number << endl;
        
    system("PAUSE");
        
    system("cls");

        
    number base_eight(number);
        
    cout << "New number in base eight: " << number << endl;
        
    sorted sort(number);
        
    cout << "New number sorted: " << sorted << endl;
        if(
    sorted == number){
            
    number base_ten(number);
            return 
    number;
        }

        
    number base_ten(number);
        
    cout << "New number in base ten: " << number << endl;
        
    sorted base_ten(sorted);
        
    cout << "New sorted in base ten: " << sorted << endl;
        
    cout << number;
        
    number number sorted;
        
    cout << "-" << sorted << " = " << number << endl;
        
    system("PAUSE");

        return 
    number;

    the code is definatly not optimized or elegant, couldn't a loop to work the way i wanted it to so i just did the long way in a new function to retrun values. and when i said cout, i include the system commands.

  12. #12
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define SIZE unsigned int
    #define TIMES_SUB 5
    #define OCTETC ((((sizeof(SIZE) << 3) - 1) / 3) + 1)
    #define SWAP_INT_XOR(x, y) {x^=y; y^=x; x^=y;}
    
    SIZE octal_sort_asc(SIZE val){
    	short i, j, digits[OCTETC];
    	
    	for(i = 0; i < OCTETC; i++)
    		digits[i] = (val >> (3*i)) & 7;
    	
    	for(i = 0; i < OCTETC; i++)
    		for(j = 0; j < OCTETC; j++)
    			if(digits[i] > digits[j]) 
    				SWAP_INT_XOR(digits[i], digits[j]);
    	
    	val = 0;
    	for(i = 0; i < OCTETC; i++)
    		val += digits[i] << (3*i);
    	
    	return val;
    }
    
    int main(int argc, char *argv[]){
    	SIZE val, newval;
    	short i;
    
    	printf("input value: %0*o\n", OCTETC, val = atoi(argv[1]));
    	printf("sorted input value: %0*o\n", OCTETC, newval = octal_sort_asc(val));
    	
    	for(i = 0; i < TIMES_SUB; i++){
    		printf("%0*o - %0*o = %0*o\n", OCTETC, val+newval, OCTETC, newval, OCTETC, val-=newval);
    		if(val == octal_sort_asc(val))
    			break;
    	}
    	
    	return 0;
    }
    i fail to see the point why you make a difference between base8 and base10 arithmetic. because there is none.
    Last edited by fsdfosdgj; 2011-05-30 at 01:36 PM.

  13. #13
    Dreadlord Rakeer's Avatar
    10+ Year Old Account
    Join Date
    Jun 2010
    Location
    The realm of Torment
    Posts
    904
    Quote Originally Posted by fsdfosdgj View Post
    i fail to see the point why you make a difference between base8 and base10 arithmetic. because there is none.
    i did it because subtraction, and all forms of math, differ if the bases differ. as i do not know how to properly express subtraction of octals, i convertered back and forth to get the same number. just a longer road that i understand.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •