practice your programming skills.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 1726 Views - Last Post: 13 April 2010 - 10:46 AM

#1 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

practice your programming skills.

Posted 11 April 2010 - 09:34 PM

I have a question it's nice to practice your programming skills.
Content question is:How to convert a decimal number to binary, hexadecimal,oct number... which uses only function "printf" , recursive and other common operations such as: / .
-which Code is shortest is good
-I've got the right answer to this question. I want to challenge you to help train programming skills in C / C + +
-I will announce the answer after 2 days.Goodluck to you.

This post has been edited by tauit_dnmd: 11 April 2010 - 10:14 PM

Is This A Good Question/Topic? 0
  • +

Replies To: practice your programming skills.

#2 Galois  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 27-March 10

Re: practice your programming skills.

Posted 11 April 2010 - 09:37 PM

No loops, and no if-statements?
Was This Post Helpful? 0
  • +
  • -

#3 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: practice your programming skills.

Posted 11 April 2010 - 09:39 PM

You said you had a question?
Was This Post Helpful? 0
  • +
  • -

#4 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: practice your programming skills.

Posted 11 April 2010 - 09:41 PM

View PostGalois, on 11 April 2010 - 08:37 PM, said:

No loops, and no if-statements?

-->Yé.No loop.But can use "if"
Was This Post Helpful? 0
  • +
  • -

#5 Galois  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 27-March 10

Re: practice your programming skills.

Posted 11 April 2010 - 09:49 PM

If-statements can be turned into loops. This assignment is a joke.
Was This Post Helpful? 0
  • +
  • -

#6 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: practice your programming skills.

Posted 11 April 2010 - 09:50 PM

From the snippets section on DIC: http://www.dreaminco.../snippet882.htm
Was This Post Helpful? 0
  • +
  • -

#7 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: practice your programming skills.

Posted 11 April 2010 - 10:08 PM

View PostSkaggles, on 11 April 2010 - 08:50 PM, said:

From the snippets section on DIC: http://www.dreaminco.../snippet882.htm

---->Great.But i dont want this way.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: practice your programming skills.

Posted 11 April 2010 - 11:03 PM

I used c++ IO cause thats what i know, but did i break the rules?
#include <iostream>
#include <vector>

using namespace std;

void to_binary(int &num, vector<int> &binary)
{
	if(num == 0) return; 
	binary.insert(binary.begin(),num%2);
	num/=2;
	to_binary(num, binary);
}
	
int main()
{
	int x = 10;
	vector<int>binary;
	to_binary(x,binary);

	for(int i=0; i<binary.size(); i++)
		cout<<binary.at(i);

	cin.ignore();
	cin.get();
	return 0;
}


This post has been edited by ImaSexy: 11 April 2010 - 11:09 PM

Was This Post Helpful? 0
  • +
  • -

#9 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: practice your programming skills.

Posted 11 April 2010 - 11:10 PM

View PostImaSexy, on 11 April 2010 - 10:03 PM, said:

I used c++ IO cause thats what i know, but did i break the rules?
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void to_binary(int &num, vector<int> &binary)
{
	if(num == 0){ reverse(binary.begin(),binary.end()); return; }
	binary.push_back(num%2);
	num/=2;
	to_binary(num, binary);
}
	
int main()
{
	int x = 10;
	vector<int>binary;
	to_binary(x,binary);

	for(int i=0; i<binary.size(); i++)
		cout<<binary.at(i);

	cin.ignore();
	cin.get();
	return 0;
}




--->Thanks you!But you break the rules.
And i can see your code is longer than mine.And your code only convert dec to binary.
And dont use any data structure.
And thanhk you very much again.

This post has been edited by tauit_dnmd: 12 April 2010 - 12:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: practice your programming skills.

Posted 12 April 2010 - 01:07 AM

View Posttauit_dnmd, on 12 April 2010 - 01:34 PM, said:

I have a question it's nice to practice your programming skills.
Content question is:How to convert a decimal number to binary, hexadecimal,oct number... which uses only function "printf" , recursive and other common operations such as: / .
-which Code is shortest is good
-I've got the right answer to this question. I want to challenge you to help train programming skills in C / C + +
-I will announce the answer after 2 days.Goodluck to you.

Don't tell me, your last post changed the goal posts and you're trying to get us to do your home work.
Was This Post Helpful? 0
  • +
  • -

#11 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: practice your programming skills.

Posted 12 April 2010 - 04:32 AM

View PostgregoryH, on 12 April 2010 - 12:07 AM, said:

View Posttauit_dnmd, on 12 April 2010 - 01:34 PM, said:

I have a question it's nice to practice your programming skills.
Content question is:How to convert a decimal number to binary, hexadecimal,oct number... which uses only function "printf" , recursive and other common operations such as: / .
-which Code is shortest is good
-I've got the right answer to this question. I want to challenge you to help train programming skills in C / C + +
-I will announce the answer after 2 days.Goodluck to you.

Don't tell me, your last post changed the goal posts and you're trying to get us to do your home work.

--->No ,i dont think so.
I dont have any homework.I just want develop programming skills with this question for you.
My programming skill is enough to do this question with a data structure such as: stack(use template...)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<ctype.h>   
#include <dos.h>
#include <stdlib.h>
template <class T,int n>
class stack
     {
     private :
	int top;    //dinh cua stack
	T a[n];     //phan tu cua stack
     public :
	stack();  //ham khoi tao stack
	void push (T);  // ham them phan tu vao stack
	T pop();   //ham loai bo 1 phan tu ra khoi stack
	T isempty();  // ham kiem tra stack co rong hay ko
	T isfull();  //ham kiem tra stack day
	T stacktop(); // ham truy xuat nut tai dinh stack?
	T stacksize(); //ham xac dinh kich thuoc stack?

     } ;
// cac ham thao tac


int lc;
template <class T, int max>
stack <T,max> :: stack()
	{
	top=-1;
	}
template <class T, int max>
T stack <T,max> :: isempty()
	{
	return (top==-1 ? 1:0) ;
	}
template <class T,int max >
T stack <T, max> :: pop()
	{
	T i;
	if (isempty())
	cout <<"stack rong !" <<endl;
	else
		{
		i=a[top];
		top--;
		}
	return i;
	}
template <class T,int max>
T stack <T,max> :: stacktop ()
	{
	T size;
	if (isempty())
	cout <<"stack rong !"<<endl;
	else
		{
		size=top+1;
		}
	return size;
	}
template <class T,int max>
void stack <T,max> :: push (T i)
	{
	if (top >=max-1)
	cout <<"stack tran !" << endl;
	else
		{
		top++;
		a[top]=i;
		}
	}
template <class T,int max>
T stack <T,max> :: stacktop()
	{

	if (isempty())	cout <<"stacktop!"<<endl;
	else
	return (a[top]);
	}



-->So i dont have any reason to do thing you said

This post has been edited by tauit_dnmd: 12 April 2010 - 04:41 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: practice your programming skills.

Posted 12 April 2010 - 06:00 AM

Moved to C++ Programmers.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: practice your programming skills.

Posted 12 April 2010 - 08:00 AM

I suspect there is an ulterior motive, but what the hell.

Assuming you meant to say "convert a decimal integer ...":

/* usage: enter executable name followed by integer to convert */
#include <stdio.h>

void dec2bin(int dec) {
  if(dec>>1) dec2bin( dec >> 1 );
  printf( "%d", dec&1 );
}

void dec2oct(int dec) {
  if(dec>>3) dec2oct( dec >> 3 );
  printf( "%d", dec&7 );
}

void dec2hex(int dec) {
  char hexes[] = "0123456789abcdef";
  if(dec>>4) dec2hex( dec >> 4 );
  printf( "%c", hexes[dec&15] );
}

int main( int argc, char ** argv) {
  int dec = atoi( argv[1]);
  printf( "decimal %d = binary ", dec );
  dec2bin( dec );
  printf( " = octal " );
  dec2oct( dec );
  printf( " = hex " );
  dec2hex( dec );
  printf( "\n" );
}


Was This Post Helpful? 0
  • +
  • -

#14 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: practice your programming skills.

Posted 12 April 2010 - 10:22 AM

i'm sorry.I get a problem when post new reply

This post has been edited by tauit_dnmd: 12 April 2010 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#15 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: practice your programming skills.

Posted 12 April 2010 - 10:40 AM

View Postr.stiltskin, on 12 April 2010 - 07:00 AM, said:

I suspect there is an ulterior motive, but what the hell.

Assuming you meant to say "convert a decimal integer ...":

/* usage: enter executable name followed by integer to convert */
#include <stdio.h>

void dec2bin(int dec) {
  if(dec>>1) dec2bin( dec >> 1 );
  printf( "%d", dec&1 );
}

void dec2oct(int dec) {
  if(dec>>3) dec2oct( dec >> 3 );
  printf( "%d", dec&7 );
}

void dec2hex(int dec) {
  char hexes[] = "0123456789abcdef";
  if(dec>>4) dec2hex( dec >> 4 );
  printf( "%c", hexes[dec&15] );
}

int main( int argc, char ** argv) {
  int dec = atoi( argv[1]);
  printf( "decimal %d = binary ", dec );
  dec2bin( dec );
  printf( " = octal " );
  dec2oct( dec );
  printf( " = hex " );
  dec2hex( dec );
  printf( "\n" );
}



==>Nothing shady here.
I do not fool anyone help me do my homework. Because I always do the exercises by the end of my ability. I just want to talk with you to find better ways my ways. But I did not find out.
see that there is no perfect answer any more of my answers.
you use too many functions.
For me just use a single function.
And below is my answer.
But I am sure there will be an Good answer that we have not found.
My code:
#include <stdio.h>
#include <conio.h>

void convert(int so,int he)
    {
        if (so>=he) convert(so/he,he);
        printf("%c",(so%he)["0123456789ABCDEF"]);        
    }

void main(void)
    {
        int n,a;
        clrscr();
        printf("Input a integer number : ");
        scanf("%d",&n);
        printf("You want to convert to[dec:10,hec:16,bin:2,oct:8]: ");
        scanf("%d",&a);
        convert(n,a);
        getch();
    } 

-->I will try to contribute more interesting questions.
Hope you will discuss

This post has been edited by tauit_dnmd: 12 April 2010 - 10:45 AM

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3