Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)
New Posts  All Forums:Forum Nav:

Programming Challenge (Out-of-Date) - Page 5  

Poll Results: Are you interested in participating in and/or helping organise and post these programming challenges?

 
  • 100% (2)
    I want to participate.
  • 0% (0)
    I want to contribute by helping posting and organise these challenges.
  • 0% (0)
    I'll only take part if other people are willing to participate.
  • 0% (0)
    I can help and participate - I love programming!
  • 0% (0)
    I do not wish to participate or help.
2 Total Votes  
post #41 of 306
What are the parameter bounds? Should it be able to handle amounts in the hundreds? Thousands? Billions?
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #42 of 306
I set it to be under a thousand, so your program should be able to handle any dollar amount smaller then that, but you can go higher.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
post #43 of 306
I just threw something together using a bash shell and ran into a problem. In bash $WHATEVER assumes WHATEVER is a variable (such as HOME or PATH) and expands it. Passing $12.11 expands $1 (to nothing) and passes only the string 2.11. In order to pass "$12.11" it must be in quotes or passed as \\$12.11
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #44 of 306
So if we write this is something like java or c#, would it also be acceptable just to ask for the dollar amount? Maybe to allow someone to type as many as they want in a single line rather than having it stuctured like the run command you posted earlier. I'm going to assume yes while I start on this .
post #45 of 306
Ok, it doesn't matter how someone can enter the amount to the program, just as long as it has a dollar sign($) and a decimal point.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
post #46 of 306
Quote:
Originally Posted by dangerousHobo View Post
Ok, it doesn't matter how someone can enter the amount to the program, just as long as it has a dollar sign($) and a decimal point.
sounds good. Should be fun.
post #47 of 306
Here it is in C (not ideal for the task, but my default language) with the user input method. It does not check for erroneous input

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFF_SIZE 1024

char *toString(int num);
char *makeString(char *buffer);

int main(int argc, char **argv){

enum { file, arguments } mode;  //describes input type
FILE *infile;
int i = -1;//counts argument position
char read_buffer[BUFF_SIZE], *string, *curr_pos, *decimal_ptr;
int dollars, cents;

printf("Enter a string begining with $ to convert: ");
while(fgets(read_buffer, BUFF_SIZE, stdin)){
string = read_buffer;

curr_pos = string;
if(*curr_pos != '$'){
printf("Done converting, exitingn");
exit(1);
}

/* separate decimal from whole number amount */
while(*curr_pos && (*curr_pos != '.')) curr_pos++;
*curr_pos = '';
decimal_ptr = ++curr_pos;
curr_pos = string + 1;

/* get integer values of dollars/cents */
dollars = atoi(curr_pos);
cents = atoi(decimal_ptr);
char *d = (dollars == 1) ? "dollar" : "dollars";
char *c = (cents == 1) ? "cent" : "cents";

printf("%s %s and %s %sn", toString(dollars), d, toString(cents), c);
printf("Enter a string begining with $ to convert: ");
}
}

char *toString(int num){

char *ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
char *teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
char *tens[] = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
char buffer[1024], *buff_ptr = buffer, *ret;
buffer[0] = '';

if(num == 0) return makeString(ones[0]);

int index = num/100;
if(index){
strcpy(buff_ptr, ones[index]);
buff_ptr += strlen(ones[index]);
*buff_ptr++ = ' ';
strcpy(buff_ptr, "hundred");
buff_ptr += strlen("hundred");
*buff_ptr = '';
}
num %= 100;

index = num/10;
if(index){
if(buffer[0])
*buff_ptr++ = ' ';

if(index == 1){
strcpy(buff_ptr, teens[num-10]);
return makeString(buffer);
}

strcpy(buff_ptr, tens[index-2]);
buff_ptr += strlen(tens[index-2]);
*buff_ptr = '';
}
num %= 10;

if(num){
if(buffer[0])
*buff_ptr++ = ' ';
strcpy(buff_ptr, ones[num]);
}

return makeString(buffer);
}

char *makeString(char *buffer){
char *ret;
size_t len = strlen(buffer);
if(len > 1024) return NULL;

ret = malloc(sizeof(char) * len);
strcpy(ret, buffer);
}

Edited by rabidgnome229 - 7/24/08 at 6:53pm
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #48 of 306
Good job man. I haven't even started yet haha.
post #49 of 306
I did want solutions posted so early but I guess it doesn't really hurt.

Edit: looks a little messy if you don't know ruby, but works.

Code:
$ time ruby challenge_1.rb $56.08
"fifty six dollars and eight cents"

real0m0.008s
user0m0.004s
sys0m0.003s
Code:
#!/usr/bin/ruby

d1 = ARGV.shift

ones = { 0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 
         5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine",
         10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen",
         15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen" }

tens = { 0 => "", 1 => "ten", 2 => "twenty", 3 => "thirty", 4 => "fourty", 
         5 => "fifty", 6 => "sixty", 7 => "seventy", 8 => "eighty", 9 => "ninety" }

d1 = d1.split('').map {|x| x.to_i}
d1.shift
d1[-3] = '.'
f = String.new
f = "#{ones[d1.shift]} hundred and " if d1.join.to_f > 100
if d1.join.to_f > 20.00
  f.concat "#{tens[d1.shift]} #{ones[d1.shift]} dollars "
else
  f.concat "#{ones[d1.shift]} #{ones[d1.shift]} dollars "
end
d1.shift
if d1.join.to_i < 20
  f.concat "and #{ones[d1.join.to_i]} cents"
else
  f.concat "and #{tens[d1.shift]} #{ones[d1.shift]} cents"
end

p f

Edited by dangerousHobo - 7/24/08 at 7:43pm
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
post #50 of 306
Here's my code in C++

Code:
#include<iostream>
using namespace std;

//Printing Function
void print(int a)
{

int temp;
temp=a;
int i=0;
int tens=0;
int temp2;


temp2=temp;
i=temp2/100;
if(i==1) cout<<"One";
else if(i==2) cout<<"Two";
else if(i==3) cout<<"Three";
else if(i==4) cout<<"Four";
else if(i==5) cout<<"Five";
else if(i==6) cout<<"Six";
else if(i==7) cout<<"Seven";
else if(i==8) cout<<"Eight";
else if(i==9) cout<<"Nine";
if(i>0 && i<10)
cout<<" Hundred ";


temp2=temp;
i=temp2%100;
i=i/10;
if(i>0)
{
if(i==2) cout<<"Twenty";
else if(i==3) cout<<"Thirty";
else if(i==4) cout<<"Fourty";
else if(i==5) cout<<"Fifty";
else if(i==6) cout<<"Sixty";
else if(i==7) cout<<"Seventy";
else if(i==8) cout<<"Eightty";
else if(i==9) cout<<"Ninety";
tens=i;
cout<<" ";
}

temp2=temp;
i=temp2%10;

if(tens==1)
{
if(i==1) cout<<"Eleven";
else if(i==2) cout<<"Twelve";
else if(i==3) cout<<"Thirteen";
else if(i==4) cout<<"Fourteen";
else if(i==5) cout<<"Fifteen";
else if(i==6) cout<<"Sixteen";
else if(i==7) cout<<"Seventeen";
else if(i==8) cout<<"Eighteen";
else if(i==9) cout<<"Nineteen";
}

else if(i>0)
{
if(i==1) cout<<"One";
else if(i==2) cout<<"Two";
else if(i==3) cout<<"Three";
else if(i==4) cout<<"Four";
else if(i==5) cout<<"Five";
else if(i==6) cout<<"Six";
else if(i==7) cout<<"Seven";
else if(i==8) cout<<"Eight";
else if(i==9) cout<<"Nine";
}

cout<<endl;
}
//end of Function


//Main
int main()
{
int amount=0;
char dollar;

cout<<"Menu"<<endl;


cout<<"Please Enter the amount with the dollar sign first"<<endl;
cin>>dollar>>amount;

while(amount<0 || amount>1000)
{
cout<<"Please Enter the amount with the dollar sign first"<<endl;
cin>>dollar>>amount;
}


print(amount);



int b;
cout<<"Enter anything to Exit"<<endl;
cin>>b;
return 0;
}
After I finished I realized that the program should accepts two numbers after decimal point

Also it only accepts entering the data manually as I wanted to code it fast

I will try to fix them later
My PC
(23 items)
 
Home Server
(12 items)
 
Aorus X7 DT v7
(11 items)
 
CPUMotherboardGraphicsGraphics
Intel Core i7 5820K ASUS X99 Deluxe NVIDIA MSI GTX1080 EK X NVIDIA MSI GTX1080 EK X 
RAMHard DriveHard DriveOptical Drive
32GB GSkill Trident Z DDR4 3000MHz CL14 Samsung 850 EVO 500GB SSD Seagate 4TB LG 14X BluRay Writer 
CoolingCoolingCoolingCooling
EK ASUS X99 Monoblock 2x EK XE480 Radiators Swiftech MCP35X (Dual DDC) Pump EK X3 250 Reservoir 
OSMonitorKeyboardPower
Windows 10 Pro X64 LG C6 55" UHD OLED Smart TV Logitech G710 Corsair RM1000 1000W 
CaseMouseMouse PadAudio
Corsair 900D Logitech G700S Razer ExactMat Pioneer VSX-930-K Receiver 
AudioAudioAudio
Pioneer S-RS77TB Speakers Pioneer S-RS3SW Subwoofer Logitech G35 
CPUMotherboardGraphicsRAM
Intel Core i7 7820HK Aorus HM175 NVIDIA GTX 1080 Notebook 32GB Kingston Hyper X DDR4 2400MHz 
Hard DriveHard DriveOSMonitor
512GB Samsung SM961 SSD Seagate FireCuda 2TB Windows 10 Pro 17.3" Sharp IGZO 4K 
KeyboardPowerCase
Aorus RGB Aorus 255W Power Adapter Aorus X7 v7 
  hide details  
My PC
(23 items)
 
Home Server
(12 items)
 
Aorus X7 DT v7
(11 items)
 
CPUMotherboardGraphicsGraphics
Intel Core i7 5820K ASUS X99 Deluxe NVIDIA MSI GTX1080 EK X NVIDIA MSI GTX1080 EK X 
RAMHard DriveHard DriveOptical Drive
32GB GSkill Trident Z DDR4 3000MHz CL14 Samsung 850 EVO 500GB SSD Seagate 4TB LG 14X BluRay Writer 
CoolingCoolingCoolingCooling
EK ASUS X99 Monoblock 2x EK XE480 Radiators Swiftech MCP35X (Dual DDC) Pump EK X3 250 Reservoir 
OSMonitorKeyboardPower
Windows 10 Pro X64 LG C6 55" UHD OLED Smart TV Logitech G710 Corsair RM1000 1000W 
CaseMouseMouse PadAudio
Corsair 900D Logitech G700S Razer ExactMat Pioneer VSX-930-K Receiver 
AudioAudioAudio
Pioneer S-RS77TB Speakers Pioneer S-RS3SW Subwoofer Logitech G35 
CPUMotherboardGraphicsRAM
Intel Core i7 7820HK Aorus HM175 NVIDIA GTX 1080 Notebook 32GB Kingston Hyper X DDR4 2400MHz 
Hard DriveHard DriveOSMonitor
512GB Samsung SM961 SSD Seagate FireCuda 2TB Windows 10 Pro 17.3" Sharp IGZO 4K 
KeyboardPowerCase
Aorus RGB Aorus 255W Power Adapter Aorus X7 v7 
  hide details  
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
This thread is locked  
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)