Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge #4
New Posts  All Forums:Forum Nav:

Programming Challenge #4 - Page 5

post #41 of 71
Yeah I know globals are bad, but in this case I'm not getting in any problems with that variable being modified somewhere else biggrin.gif
Atroc.V3.2
(16 items)
 
Home Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm 
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05 
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi 
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb 
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w 
Case
[NZXT] Gamma 
  hide details  
Reply
Atroc.V3.2
(16 items)
 
Home Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm 
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05 
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi 
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb 
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w 
Case
[NZXT] Gamma 
  hide details  
Reply
post #42 of 71
Quote:
Originally Posted by NorCa View Post

Yeah I know globals are bad, but in this case I'm not getting in any problems with that variable being modified somewhere else biggrin.gif

Yeah but you're doing it the easy way. tongue.gif

"When faced with a challenge, look for a way, not a way out." -David Weatherford
Edited by kennyparker1337 - 1/30/12 at 1:46pm
post #43 of 71
Quote:
Originally Posted by kennyparker1337 View Post

Yeah but you're doing it the easy way. tongue.gif
"When faced with a challenge, look for a way, not a way out." -David Weatherford

too many non-functional requirements on this challenge. now, everyone's solution will be using the exact same algorithm rolleyes.gif

anyway, i modified my code to meet the extra requirements. but seriously, stuffing the result in an int didn't make this challenge any harder.

please forgive the rant.
Code:
#include <stdio.h>
#include <stdlib.h>
int reverse(int num, int result){
        if(num) return reverse(num/10, (result*10)+(num%10));
        return result;
}
 
int main(int argc, char **argv){
        printf("%d",reverse(atoi(argv[1]), 0));
        return 0;
}
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #44 of 71
Quote:
Originally Posted by travesty View Post

Quote:
Originally Posted by kennyparker1337 View Post

Yeah but you're doing it the easy way. tongue.gif
"When faced with a challenge, look for a way, not a way out." -David Weatherford

too many non-functional requirements on this challenge. now, everyone's solution will be using the exact same algorithm rolleyes.gif

anyway, i modified my code to meet the extra requirements. but seriously, stuffing the result in an int didn't make this challenge any harder.

please forgive the rant.

Non of the requirements even changed, just more emphasized.
The added requirement of returning an int is just optional.

Using global variables isn't against the rules I was just stating that you shouldn't use them and using to make a problem easier is "looking for a way out, and not a way."
post #45 of 71
Quote:
Originally Posted by {Unregistered} View Post

I'm sorry I couldn't check back on this thread earlier.
3. The meaning of "returning" the integer as to how I used is basically outputting it to the user. I know that, literally, returning means putting the digits together to return the int as a whole but I think it's okay if you can simply "print" out each digit individually to reduce the level of difficulty of the challenge.

But then you're returning a string, which is against the rule 1 headscratch.gif
post #46 of 71
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

But then you're returning a string, which is against the rule 1 headscratch.gif
Code:
System.out.println(666);

This prints out the integer 666.
Code:
System.out.println("666");

This prints out the String "666".

I know what you mean but an integer can still be "printed" out.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #47 of 71
Quote:
Originally Posted by {Unregistered} View Post

Code:
System.out.println(666);
This prints out the integer 666.
Code:
System.out.println("666");
This prints out the String "666".
I know what you mean but an integer can still be "printed" out.

It's effectively the same thing as I'm pretty sure most languages would do a quick string conversion against the int in the print functions. But that's besides the point as that wasn't what I was questioning: I thought you were talking about concatenating the units, tens, hundreds, etc on the fly when printing out. eg:
Code:
cout << units << tens << hundreds << endln;
which you are printing out integers (in the same way you're describing above), but you're printing them out separately and using the screen buffer as a string concatenation. (which is a bit of a cheat really given the context of this challenge).

If this wasn't what you meant before, then I apologise smile.gif
post #48 of 71
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

It's effectively the same thing as I'm pretty sure most languages would do a quick string conversion against the int in the print functions. But that's besides the point as that wasn't what I was questioning: I thought you were talking about concatenating the units, tens, hundreds, etc on the fly when printing out. eg:
Code:
cout << units << tens << hundreds << endln;
which you are printing out integers (in the same way you're describing above), but you're printing them out separately and using the screen buffer as a string concatenation. (which is a bit of a cheat really given the context of this challenge).
If this wasn't what you meant before, then I apologise smile.gif

Yup, I know that it is in fact sort of a cheat but please refer to my conversation with kennyparker1337 further up in this thread and the OP. Before issuing this challenge, I meant for the reversed integer to be returned as an integer rather than be printed but then after I saw people misunderstanding the challenge when I said "return" the integer, I decided to change the requirements.

But I have also mentioned in the first post that if you want more of a complete challenge, you may complete the task in the proper way, as in use only mathematical operators on the integer to reverse it.

I hope this clarifies things.

I think I will change it in the OP anyway.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #49 of 71
OK, so after initially doing the challenge exactly how I wasn't suppose to, I had to go back to the drawing board and learn how recursive functions work. After a fair bit of study I understand them pretty well now and finally have it working. Thanks kennyparker for helping me get on track and showing me the log formula which I used to find the length of the input number. So here is my new and improved code in C++ wink.gif
Code:
#include <iostream>
#include <cmath>

using namespace std;

// Declare functions
int numberReverser(int);
int numberLength(int);

// Main function
int main()
{
    int number;

    cout << "Enter a number and I will reverse it for you: ";   // prompt user to input number
    cin  >> number;     // take input number from user

    cout << "Your number reversed is " << numberReverser(number) << endl;     // print out the reversed number
}


// Function that reverses the digits of the input number
int numberReverser(int number)
{
    int lastDigit, numLength;

    lastDigit = number % 10;                // finds the last digit of th number
    numLength = numberLength(number);       // calls numberLength function to find length of number
    number = (number - lastDigit) / 10;     // removes last digit from number

    if(number==0){
        return lastDigit;   // returns end value when no digits remain
    }else{
        return numberReverser(number) + (lastDigit * pow(10, (numLength - 1)));  // place the digit in proper position for reverse of number
    }
}

// Function that finds the length of the input number
int numberLength(int numLength)
{
    if (numLength == 0){
        return 1;
    }else{
        return (int)log10(numLength) + 1;   // formula to find length of an integer
    }
}
post #50 of 71
Java. Works with positive and negative numbers and returns the entire integer as a unit rather than printing one column at a time. (Click to show)
Code:
public class NumFlipRunner
{
        public static void main(String[] args)
        {
                NumFlip flipper = new NumFlip();
                flipper.run();
        }
}

import java.util.Scanner;

public class NumFlip
{
        public void run()
        {
                int num = getInput();
                if (num < 0)
                        num = (reverse(num*-1,0)*-1);
                else
                        num = reverse(num, 0);
                System.out.println("The reverse is: "+num);
        }
        
        private int getInput()
        {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter an integer to reverse: ");
                return input.nextInt();
        }
        
        private int reverse(int num, int wholeNum)
        {
                if (num < 10)
                {
                        wholeNum *= 10;
                        wholeNum += num;
                        return wholeNum;
                }
                else
                {
                        wholeNum *= 10;
                        wholeNum += num%10;
                        return reverse(num/10, wholeNum);
                }
        }
}
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge #4