473,469 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

make a program using C,C++

hi to everybody,i need help for my project....how to make a program
that accept any integer and convert it binary,octal and hexadecimal.pls
help me...thanks

Jan 17 '06
52 4049
On Tue, 17 Jan 2006, Flash Gordon <sp**@flash-gordon.me.uk> wrote:-
David Bolt wrote:
<Snippety>
Well, there is just one little thing that I can see...
When number is converted to unsigned and number is negative, the
conversion will be done as if UINT_MAX to the value. On a 2's
complement machine (i.e. almost all machines these days) this will just
reinterpret the bits as unsigned. However, on a 1's complement or sign
& magnitude system (which the C standard still allows) it will change
the bit pattern. So, basically, your routine always prints the bit
pattern of the 2's complement representation of the number even if that
is not what the implementation uses.

Personally, for all of my real work (as opposed to picking nits here) I
would not worry about it.
Having never seen a 1's complement machine, this is another thing I
wouldn't have known.
Learning more does appear to be among the things I've been doing today.


With that attitude, I think you will do well in this group. So I'll let
you in to a trade secret...


Ooh, a secret. I like secrets :-)
You can legally download copies of the drafts of the various version of
the C standard for free. This page has the appropriate links
http://clc-wiki.net/wiki/Basics_Of_The_C_Standard
OK, so it's not really a secret. However, it is reference material you
might find useful.
Having quickly skimmed through some of the info, including some of the
info linked from there, I can see it's going to be very useful. It might
even stop me making some stupid mistakes. No guarantees though.

After learning that in a for() post- and pre- apparently do the same
thing, it doesn't appear to matter which is used.


It does matter if you use the value of the expression, i.e. these two
are different:
for (i=0; i<10; j=i++) {
/* j uninitialised fist time round then 1 less than i
on subsequent iterations */
}

for (i=0; i<10; j=++i) {
/* j uninitialised fist time round then the same as i
on subsequent iterations */
}


Okay, this is where I should have qualified the above to say that there
being no difference between post/pre only applied when there was no
other assignment.

<snip>
This is good. Making honest errors on this group is fine, accepting
corrections is and learning from them is excellent.
And what a lot of learning I have to do. And after all the lurking I've
done, and all the things I've read here, I still managed to miss such a
simple error.
Just don't take my word as gospel until you've either verified it or at
least seen that no one else is pointing out that I've got it wrong.


Or, as with my case, watch to see if the person correcting you ends up
being corrected?
Regards,
David Bolt

--
Member of Team Acorn checking nodes at 50 Mnodes/s: http://www.distributed.net/
AMD1800 1Gb WinXP/SUSE 9.3 | AMD2400 256Mb SuSE 9.0 | A3010 4Mb RISCOS 3.11
AMD2400(32) 768Mb SUSE 10.0 | RPC600 129Mb RISCOS 3.6 | Falcon 14Mb TOS 4.02
AMD2600(64) 512Mb SUSE 10.0 | A4000 4Mb RISCOS 3.11 | STE 4Mb TOS 1.62
Jan 19 '06 #51
Thad Smith wrote:
August Karlstrom wrote:
The calculations in the implementation above will easily overflow and
`base < 10 + 'Z' - 'A'' should really be `base <= '9' - '0' + 1 +
'Z' - 'A'' (to be explicit), so my implementation works up to base 36.

You are still off by one in your base check, assuming ASCII. If EBCDIC,
it's worse. I am ignoring the superfluous trailing apostrophe for now.


Yes you are right, but I fixed it in my revised code (as you noticed).
There is no superfluous trailing apostrophe; the entire expression is
quoted by `...' (this is the convention used by TeX Info). Maybe `...`
is better, I don't know.
#define BASE_MAX ('9' - '0' + 1 + 'Z' - 'A' + 1)

I see you fixed it here.

void get_num_in_base(char *res, unsigned num, unsigned base)
{
unsigned rem = num / base, power = 1, k = 0, digit;

assert((base >= 2) && (base <= BASE_MAX));
while (rem > 0) { /* loop invariant: power * base <= num */

Assume you start with num=1. The invariant is initially wrong.


No, when num is one rem is zero, so the loop is never entered.
power *= base;
rem /= base;
}
rem = num;
while (power > 0) {
digit = rem / power;
res[k] = ((digit < 10)? '0': 'A' - 10) + digit;
rem -= digit * power;
power /= base;
k++;
}
res[k] = '\0';
}

The code will now always generate a leading 0, which I would consider an
error.


I don't know what you mean. Do you have an example?
If you attempt to convert UINT_MAX-1, you will get a divide by 0.


Where would that happen? The denominators are `base' and `power', where
`base' is positive and division by `power' is guarded by
`power > 0'. Try and compile and run the complete program below. On my
32-bit machine i get the (expected) output FFFFFFFE.

#include <assert.h>
#include <limits.h>
#include <stdio.h>

#define BASE_MAX ('9' - '0' + 1 + 'Z' - 'A' + 1)

void get_num_in_base(char *res, unsigned num, unsigned base)
{
unsigned rem = num / base, power = 1, k = 0, digit;

assert((base >= 2) && (base <= BASE_MAX));
while (rem > 0) { /* loop invariant: power * base <= num */
power *= base;
rem /= base;
}
rem = num;
while (power > 0) {
digit = rem / power;
res[k] = ((digit < 10)? '0': 'A' - 10) + digit;
rem -= digit * power; /* digit * power <= rem */
power /= base;
k++;
}
res[k] = '\0';
}
int main(int argc, char *argv[])
{
char buf[32];

get_num_in_base(buf, UINT_MAX - 1, 16);
puts(buf);
return 0;
}
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Jan 19 '06 #52
August Karlstrom wrote:
Thad Smith wrote:
August Karlstrom wrote:
void get_num_in_base(char *res, unsigned num, unsigned base)
{
unsigned rem = num / base, power = 1, k = 0, digit;

assert((base >= 2) && (base <= BASE_MAX));
while (rem > 0) { /* loop invariant: power * base <= num */


Assume you start with num=1. The invariant is initially wrong.

No, when num is one rem is zero, so the loop is never entered.


Oops, I somehow read rem = num, not rem = num/base. You are right and I
withdraw my criticisms of your code. ;-)
--
Thad
Jan 20 '06 #53

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: takashi | last post by:
Hi, I have a question. I am learning about how to use c++ language. I have attempted to make my own programs, using the knowledge that I have, but sometimes when I get stuck on writing a code, it...
11
by: bart | last post by:
Hello, I'm so sorry, but i don't understand the concept of the .net environment yet. I made a simple program that retrieves the hostname and ipaddress of the local computer. But when i give...
17
by: UJ | last post by:
Is there any way for a windows service to start a windows program ? I have a service that will need to restart a windows app if it needs to. TIA - Jeff.
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
7
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
8
by: Seeker | last post by:
Hello, In using Solaris Pro Compiler to compile Pro*C code. I am getting this error: make: Fatal error in reader: parser_proc_online.mk, line 26: Badly formed macro assignment Based on other...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
48
by: istillshine | last post by:
When I used gprof to see which function consumed most running time, I identified the following one. sz was less than 5000 on average, but foo had been called about 1,000,000 times. I have tried...
42
by: lorlarz | last post by:
Contrary to what one authority in the JavaScript field says: JavaScript does make errors when dealing with just with integers. This authority (Douglas Crockford.) says: "integer arithmetic in...
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.