Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Programming challenge


  • Please log in to reply

#1
chickenman

chickenman

    Member

  • Member
  • PipPip
  • 37 posts
Here is the first of my programming challenges :tazz:

Rules
  • Must be made in C++
  • Your program must be completed within 10 hours of starting
  • You must only use the header files that come with your compiler or make you own
  • You may only use the Windows API or GTK+ for a GUI
The challenge:

Make a program that converts decimal numbers to hexadecimal, octal and binary, a GUI is optional.
also if possible make your own algorithm to convert the numbers.

This challenge will end in two days, to enter the challenge post your source file here.

-The chicken man.
  • 0

Advertisements


#2
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Hi chickenman,
Is this a real challenge or are just trying to get help for your homework?
If its help for homework you want, be honest and don't worry we'll help you out.

If its a real challenge, I'm sorry for the above statements and let me be the first to say this is a really good idea. :tazz:
Also you might want to add a rule that people with more than 3 years experience in programming can't enter the contest as it won't be fair for the others. Or maybe you can make the question tougher, because I bet there are many people on this forum who can write this program even in assembely language.

Edited by darth_ash, 12 October 2005 - 05:27 AM.

  • 0

#3
chickenman

chickenman

    Member

  • Topic Starter
  • Member
  • PipPip
  • 37 posts
Yes this is a real challenge, the aim of it is for people to learn new ways of programming things, it's not about winning the challenge it's about learning :)

This is a nice simple thing to start with, but the challenges will get slowly harder :tazz:
  • 0

#4
chickenman

chickenman

    Member

  • Topic Starter
  • Member
  • PipPip
  • 37 posts
Is any one going to try my challenge lol, it's closed in a day !
  • 0

#5
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
chickenman, the C language considers all numbers internally in its implementation to be binary, and if you want to do arithmetic or operations it will happily accept numbers in decimal, octal (add an initial 0), or hex (add an initial 0x), so you don't really need to write a function to convert numbers from one base to another because it will do it for you. For example, suppose you want to print out the hex representation of 200. If you just write:

printf("%x\n", 200);

C will happily print out the hex representation of 200 (I think it's C8). Is this what you meant? If you meant you wanted a program that converted the actual integer type to a hexadecimal type to be used internally as a new data type, that isn't the way C (or C++) works.....
  • 0

#6
chickenman

chickenman

    Member

  • Topic Starter
  • Member
  • PipPip
  • 37 posts
I meant for people to write there own algorithm to convert the decimal numbers to hexadecimal, octal and binary, not the C or C++ one.
  • 0

#7
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
No, but what I am saying is: what does it mean to "convert" a decimal number to another base when C treats them all the same internally, as ints? All an "int" is, is a bunch of bits anyway --- those bits must be interpreted in binary and then re-interpreted in whatever base you want. C does that for you for hex, octal, and decimal.
  • 0

#8
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
chickenman,
SwanDog46 is right.

But sometimes in programming contests, they do ask to do it manually, just to see you ability to develop logic.

For the solution, just use the standerd conversion methods.
I'm too lazy to type the code; but if you insist, I will.
  • 0

#9
MaverickSidewinder

MaverickSidewinder

    Member

  • Member
  • PipPipPip
  • 257 posts
Hey...no one responded to the topic..i was getting interested :tazz:

p.s. sorry for the people that saw the reply on the programmin forum and hoped that i was the one with the source code... :)
  • 0

#10
DeathOutdone

DeathOutdone

    Member

  • Member
  • PipPipPip
  • 241 posts
CAN anyone here program in assembly language? I heard that is a very hard language because you are giving the comp specific instructions on how to carry out the programming language.
  • 0

Advertisements


#11
dsenette

dsenette

    Je suis Napoléon!

  • Community Leader
  • 26,047 posts
  • MVP
i've done it in vb before...sorry i don't know c....but...good idea for a contest
  • 0

#12
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
You want a challenge? OK, here's one: write a command-line routine that accepts three arguments: a file to parse, and two text strings (I don't care if the arguments are given on command-line or from stdin), and performs a simple search-and-replace in the file argument, replacing all instances of string 1 with string 2. (The program is sort of a simpler version of UNIX sed). It must be done in a compiled language --- no scripting, batches, or system calls --- so C, C++, or I guess Java would be OK too. (I like C myself). Post the source here and I'll take a look at it. :tazz:
  • 0

#13
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
SwanDog46,

//Language used: c++
#include<string.h>
#include<fstream.h>
#include<iostream.h>
void main(int argc, char *argv[])
{
	int i_match=0,i_max;
	char chr;
	fstream f;
	char *par1,*par2,*par3;
	par1 = argv[1];			 //get para1, i.e. file-path
	par2 = argv[2];			 //get para2, i.e. string to be replaced
	par3 = argv[3];			 //get para3, i.e. string to replace with
	i_max = strlen(par2);

	f.open(par1,ios::in|ios::out);  //open file(para1) in read\write mode
	while(!f.eof())						//check for EOF   
	{
	f>>chr;					 //read a single char
	if(chr==par2[i_match])		 //check if it matches with the current char in par2
	{
		if(i_match==i_max-1)				 //checks for complete string match
		{
			f.seekp(f.tellg()-i_max);	  //move file-write pointer back to start of string
			f<<par3;							//replace(write) the string with par3 string
			i_match=0;
		}
		else i_match++;
	}
	else i_match=0;
	}
	f.close();
}


I really want this topic to stay alive, so how about a change in rules, the person who gets a qus. correct must post the next qus.
So I have answered the qus.(which you have to approve), I'll be the one to post the next qus.

Edited by darth_ash, 04 November 2005 - 04:36 PM.

  • 0

#14
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Sure, that's a good idea, so the next one is yours then. About your code, a few things...

I tried to compile this using g++ in unix, and I got a few errors, one about how the main routine should return int rather than void (which is not major as most compilers will ignore that), but a more substantive one about this line:

f.seekp(f.tellg()-i_max); //move file-write pointer back to start of string

Apparently ISO C++ forbids implicit casting of int to streampos. I wrote in an explicit cast on i_max, and after that it compiled fine. (I am actually not very familiar with C++ I/O conventions, so I don't know if this is typical, or what was going on.)

In addition, your code will correctly overwrite the matched text, but it will also continue to overwrite any other data as well. For example, suppose I had a file containing this string of text:

hi, nice to see you

and suppose I ran your program with "hi" as par2 and "gotcha" as par3 (I tried it so I know this is true). Then, your program will happily replace "hi", but it will also (unexpectedly) replace some of the characters after hi also, to make room. The final string would look like:

gotchace to see you

See? It would overwrite the comma, and the beginning of the word 'nice'. I was thinking more along the lines of having the program *insert* the replaced text rather than overwriting whatever came after, so that the replaced string should look like:

gotcha, nice to see you

but perhaps I was not clear in my requirements.

Either way, this was fun! What's the next one? :tazz:

Edited by Swandog46, 04 November 2005 - 10:20 PM.

  • 0

#15
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Next Challenge:
Find the Determinant of a nXn matrix, where n will be user inputs.
(For those who don't know what is a determinant, refer: http://www.maths.sur...ption1.html#Det)

Rules:
Use any programming language that supports matrices.
If possible, attach the final exe, as problems may be caused due to difference in compilers.
Do not use a ready-made library function to directly find determinant.
Duration is 1 week.

Edited by darth_ash, 05 November 2005 - 04:23 AM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP