Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > OK. LETS START REAL PROGRAMMING IN C FOR PROBLEMS!!!

Reply
Thread Tools

OK. LETS START REAL PROGRAMMING IN C FOR PROBLEMS!!!

 
 
Kleuske
Guest
Posts: n/a
 
      11-24-2011
On Tue, 22 Nov 2011 13:21:01 -0800, Keith Thompson saw fit to publish the
following:

> Kleuskes & Moos <(E-Mail Removed)> writes:
>> On Tue, 22 Nov 2011 00:01:37 +0200, Phil Carmody wrote:

> [...]
>>> Oh - do you not think that a 15-line long .sig is a bit excessive
>>> attached to a post with only two lines of original text?

>>
>> Provided for your amusement only. The argument of conserving bandwidth
>> may have been valid 20 years ago, nowadays it's hardly valid.

>
> Bandwidth isn't the only issue. It takes up space on everyone's screen
> that could be used to show more useful information. (And frankly they
> aren't all that amusing.)
>
> At least use a proper signature delimiter (a line consisting of "-- ").


Aye, aye. Sir!

--
The idea of male and female are universal constants.
-- Kirk, "Metamorphosis", stardate 3219.8
 
Reply With Quote
 
 
 
 
Markus Wichmann
Guest
Posts: n/a
 
      11-24-2011
On 24.11.2011 19:08, io_x wrote:
> "Markus Wichmann" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
>> Euler's number is actually
>>
>> 1+ 1 + 1/2! + 1/3! + 1/4! + ...
>>
>> So... to calculate the Euler number you'd actually need to keep track of
>> the current addend. Dunno, something like
>>
>> e = 2;
>> factorial = 1;
>> fact_arg = 2;
>>
>> for(; {
>> factorial *= fact_arg++;
>> addend = 1/factorial;
>> e += addend;
>> }

>
> above loop end with exaustation of memory
> without return one result
>
>
>


That's why I said that filling out the blanks is left as an exercise to
the reader. Plus, where does that loop exhaust memory? It will never
terminate, but mainly because I neglected to do everything for the OP.
That's why there are no types in there, too.

At some point, someone should terminate the execution. At that point, e
should be pretty close to, well, e.

Ciao,
Markus
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      11-25-2011
On Nov 24, 8:08*am, "io_x" <(E-Mail Removed)> wrote:
> "88888 Dihedral" <(E-Mail Removed)> ha scritto nel messaggionews:24357587.77.1321816326271.JavaMail.g eo-discussion-forums@prnv30...
>
> > PROBLEM 1: COMPUTE THE EULER'S NUMBER UP TO 30 DIGITS AFTER 1.
> > THE DIGIT REQUIRED SHOULD BE PARAMETER BY THE CALLER!

>
> yes but i'm a little ot because it is C++...


So, let's try something /on-topic/, then, shall we?

/*
** This program was inspired by the article "The Impossible Dream:
** Computing e to 116,000 places with a Personal Computer" by
** Stephen Wozniak, published in Byte Magazine Vol 6, Issue 6
** (June 1981, pp392).
**
** Essentially, the following program implements a partial expansion
** of the equation: e = 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)
** by inverting the equation, such that it reads...
** e = (((((...(((((1/n) + 1) / (n-1)) + 1) ... / 3) + 1) / 2) + 1) /
1) + 1
**
** I originally wrote this program in Z80 Assembler code, to run under
** CP/M (Dec 1986), and subsequently have recoded the program in
** 370 Assembler to run under MVS and VSE, and C to run under MSDOS
** and Linux
**
** Lew Pitcher, 1999
*/
#include <stdio.h>

#define I 1 /* number of bytes in integer portion */
#define X 2078 /* number of bytes in fraction portion */
#define N 1776 /* number of divide iterations */
#define P 5000 /* number of multiply iterations */

char e[I+X]; /* holds fixed point binary value of e */

int main(void)
{
void divide(char *, unsigned, unsigned),
multiply(char *, unsigned, unsigned);

unsigned n, /* divisor for calculation loop */
p; /* precision for print loop */

e[0] = 1; /* start the series at 1.anything */
for (n = N; n > 0; --n)
{
divide(e,n,I+X);
e[0] += 1;
}

fputs(" e == ",stdout); fputc(e[0]+'0',stdout); fputc('.',stdout);
for (p = 0; p < P; ++p)
{
e[0] = 0;
multiply(e,10,I+X);
fputc(e[0]+'0',stdout);
if (p%5 == 4)
if (p%50 == 49)
fputs("\n\t",stdout);
else
fputc(' ',stdout);
}
fputc('\n',stdout);

return 0;
}

void divide(char *dvd, unsigned dvr, unsigned prc)
{
char *Wk_q;
unsigned bit, rmdr;

rmdr = 0;
for (Wk_q = dvd; Wk_q < dvd+prc; ++Wk_q)
{
for(bit = 0; bit < 8; ++bit)
{
rmdr *= 2;
if (*Wk_q & 0200) rmdr += 1;
*Wk_q <<= 1;
if (rmdr >= dvr)
{
*Wk_q += 1;
rmdr -= dvr;
}
}
}
}

void multiply(char *mcd, unsigned mpy, unsigned prc)
{
char *Wk_r;
unsigned bit, ovflo;

ovflo = 0;
for (Wk_r = mcd+prc-1; Wk_r >= mcd; --Wk_r)
{
for (bit = 8; bit > 0; --bit)
{
if (*Wk_r & 1) ovflo += mpy;
*Wk_r >>= 1; *Wk_r &= 0177;
if (ovflo & 1) *Wk_r = *Wk_r | 0200;
ovflo /= 2;
}
}
}

=========================== output ============================
e == 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69995
95749 66967 62772 40766 30353 54759 45713 82178 52516 64274
27466 39193 20030 59921 81741 35966 29043 57290 03342 95260
59563 07381 32328 62794 34907 63233 82988 07531 95251 01901
15738 34187 93070 21540 89149 93488 41675 09244 76146 06680
82264 80016 84774 11853 74234 54424 37107 53907 77449 92069
55170 27618 38606 26133 13845 83000 75204 49338 26560 29760
67371 13200 70932 87091 27443 74704 72306 96977 20931 01416
92836 81902 55151 08657 46377 21112 52389 78442 50569 53696
77078 54499 69967 94686 44549 05987 93163 68892 30098 79312
77361 78215 42499 92295 76351 48220 82698 95193 66803 31825
28869 39849 64651 05820 93923 98294 88793 32036 25094 43117
30123 81970 68416 14039 70198 37679 32068 32823 76464 80429
53118 02328 78250 98194 55815 30175 67173 61332 06981 12509
96181 88159 30416 90351 59888 85193 45807 27386 67385 89422
87922 84998 92086 80582 57492 79610 48419 84443 63463 24496
84875 60233 62482 70419 78623 20900 21609 90235 30436 99418
49146 31409 34317 38143 64054 62531 52096 18369 08887 07016
76839 64243 78140 59271 45635 49061 30310 72085 10383 75051
01157 47704 17189 86106 87396 96552 12671 54688 95703 50354
02123 40784 98193 34321 06817 01210 05627 88023 51930 33224
74501 58539 04730 41995 77770 93503 66041 69973 29725 08868
76966 40355 57071 62268 44716 25607 98826 51787 13419 51246
65201 03059 21236 67719 43252 78675 39855 89448 96970 96409
75459 18569 56380 23637 01621 12047 74272 28364 89613 42251
64450 78182 44235 29486 36372 14174 02388 93441 24796 35743
70263 75529 44483 37998 01612 54922 78509 25778 25620 92622
64832 62779 33386 56648 16277 25164 01910 59004 91644 99828
93150 56604 72580 27786 31864 15519 56532 44258 69829 46959
30801 91529 87211 72556 34754 63964 47910 14590 40905 86298
49679 12874 06870 50489 58586 71747 98546 67757 57320 56812
88459 20541 33405 39220 00113 78630 09455 60688 16674 00169
84205 58040 33637 95376 45203 04024 32256 61352 78369 51177
88386 38744 39662 53224 98506 54995 88623 42818 99707 73327
61717 83928 03494 65014 34558 89707 19425 86398 77275 47109
62953 74152 11151 36835 06275 26023 26484 72870 39207 64310
05958 41166 12054 52970 30236 47254 92966 69381 15137 32275
36450 98889 03136 02057 24817 65851 18063 03644 28123 14965
50704 75102 54465 01172 72115 55194 86685 08003 68532 28183
15219 60037 35625 27944 95158 28418 82947 87610 85263 98139
55990 06737 64829 22443 75287 18462 45780 36192 98197 13991
47564 48826 26039 03381 44182 32625 15097 48279 87779 96437
30899 70388 86778 22713 83605 77297 88241 25611 90717 66394
65070 63304 52795 46618 55096 66618 56647 09711 34447 40160
70462 62156 80717 48187 78443 71436 98821 85596 70959 10259
68620 02353 71858 87485 69652 20005 03117 34392 07321 13908
03293 63447 97273 55955 27734 90717 83793 42163 70120 50054
51326 38354 40001 86323 99149 07054 79778 05669 78533 58048
96690 62951 19432 47309 95876 55236 81285 90413 83241 16072
26029 98330 53537 08761 38939 63917 79574 54016 13722 36187
89365 26053 81558 41587 18692 55386 06164 77983 40254 35128
43961 29460 35291 33259 42794 90433 72990 85731 58029 09586
31382 68329 14771 16396 33709 24003 16894 58636 06064 58459
25126 99465 57248 39186 56420 97526 85082 30754 42545 99376
91704 19777 80085 36273 09417 10163 43490 76964 23722 29435
23661 25572 50881 47792 23151 97477 80605 69672 53801 71807
76360 34624 59278 77846 58506 56050 78084 42115 29697 52189
08740 19660 90665 18035 16501 79250 46195 01366 58543 66327
12549 63990 85491 44200 01457 47608 19302 21206 60243 30096
41270 48943 90397 17719 51806 99086 99860 66365 83232 27870
93765 02260 14929 10115 17177 63594 46020 23249 30028 04018
67723 91028 80978 66605 65118 32600 43688 50881 71572 38669
84224 22010 24950 55188 16948 03221 00251 54264 94639 81287
36776 58927 68816 35983 12477 88652 01411 74110 91360 11649
95076 62907 79436 46005 85194 19985 60162 64790 76153 21038
72755 71269 92518 27568 79893 02761 76114 61625 49356 49590
37980 45838 18232 33686 12016 24373 65698 46703 78585 33052
75833 33793 99075 21660 69238 05336 98879 56513 72855 93883
49989 47074 16181 55012 53970 64648 17194 67083 48197 21448
88987 90676 50379 59036 69672 49499 25452 79033 72963 61626
58976 03949 85767 41397 35944 10237 44329 70935 54779 82629
61459 14429 36451 42861 71585 87339 74679 18975 71211 95618
73857 83644 75844 84235 55581 05002 56114 92391 51889 30994
63428 41393 60803 83091 66281 88115 03715 28496 70597 41625
62823 60921 68075 15017 77253 87402 56425 34708 79089 13729
17228 28611 51591 56837 25241 63077 22544 06337 87593 10598
26760 94420 32619 24285 31701 87817 72960 23541 30606 72136
04600 03896 61093 64709 51414 17185 77701 41806 06443 63681
54644 40053 31608 77831 43174 44081 19494 22975 59931 40118
88683 31483 28027 06553 83300 46932 90115 74414 75631 39997
22170 38046 17092 89457 90962 71662 26074 07187 49975 35921
27560 84414 73782 33032 70330 16823 71936 48002 17328 57349
35947 56433 41299 43024 85023 57322 14597 84328 26414 21684
87872 16733 67010 61509 42434 56984 40187 33128 10107 94512
72237 37886 12605 81656 68053 71439 61278 88732 52737 38903
92890 50686 53241 38062 79602 59303 87727 69778 37928 68409
32536 58807 33988 45721 87460 21005 31148 33513 23850 04782
71693 76218 00490 47955 97959 29059 16554 70505 77751 43081
75112 69898 51884 08718 56402 60353 05583 73783 24229 24185
62564 42550 22672 15598 02740 12617 97192 80471 39600 68916
38286 65277 00975 27670 69777 03643 92602 24372 84184 08832
51848 77047 26384 40379 53016 69054 65937 46161 93238 40363
89313 13643 27137 68884 10268 11219 89127 52230 56256 75625
47017 25086 34976 53672 88605 96675 27408 68627 40791 28565
76996 31378 97530 34660 61666 98042 18267 72456 05306 60773
89962 42183 40859 88207 18646 82623 21508 02882 86359 74683
96543 58856 68550 37731 31296 58797 58105 01214 91620 76567
69950 65971 53447 63470 32085 32156 03674 82860 83786 56803
07306 26576 33469 77429 56346 43716 70939 71930 60876 96349
53288 46833 61303 88294 31040 80029 68738 69117 06666 61468


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      11-26-2011
On Saturday, November 26, 2011 7:31:46 AM UTC+8, Lew Pitcher wrote:
> On Nov 24, 8:08*am, "io_x" <(E-Mail Removed)> wrote:
> > "88888 Dihedral" <(E-Mail Removed)> ha scritto nel messaggionews:24357587.77.1321816326271.JavaMail.g eo-discussion-forums@prnv30...
> >
> > > PROBLEM 1: COMPUTE THE EULER'S NUMBER UP TO 30 DIGITS AFTER 1.
> > > THE DIGIT REQUIRED SHOULD BE PARAMETER BY THE CALLER!

> >
> > yes but i'm a little ot because it is C++...

>
> So, let's try something /on-topic/, then, shall we?
>
> /*
> ** This program was inspired by the article "The Impossible Dream:
> ** Computing e to 116,000 places with a Personal Computer" by
> ** Stephen Wozniak, published in Byte Magazine Vol 6, Issue 6
> ** (June 1981, pp392).
> **
> ** Essentially, the following program implements a partial expansion
> ** of the equation: e = 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)
> ** by inverting the equation, such that it reads...
> ** e = (((((...(((((1/n) + 1) / (n-1)) + 1) ... / 3) + 1) / 2) + 1) /
> 1) + 1
> **
> ** I originally wrote this program in Z80 Assembler code, to run under
> ** CP/M (Dec 1986), and subsequently have recoded the program in
> ** 370 Assembler to run under MVS and VSE, and C to run under MSDOS
> ** and Linux
> **
> ** Lew Pitcher, 1999
> */
> #include <stdio.h>
>
> #define I 1 /* number of bytes in integer portion */
> #define X 2078 /* number of bytes in fraction portion */
> #define N 1776 /* number of divide iterations */
> #define P 5000 /* number of multiply iterations */
>
> char e[I+X]; /* holds fixed point binary value of e */
>
> int main(void)
> {
> void divide(char *, unsigned, unsigned),
> multiply(char *, unsigned, unsigned);
>
> unsigned n, /* divisor for calculation loop */
> p; /* precision for print loop */
>
> e[0] = 1; /* start the series at 1.anything */
> for (n = N; n > 0; --n)
> {
> divide(e,n,I+X);
> e[0] += 1;
> }
>
> fputs(" e == ",stdout); fputc(e[0]+'0',stdout); fputc('.',stdout);
> for (p = 0; p < P; ++p)
> {
> e[0] = 0;
> multiply(e,10,I+X);
> fputc(e[0]+'0',stdout);
> if (p%5 == 4)
> if (p%50 == 49)
> fputs("\n\t",stdout);
> else
> fputc(' ',stdout);
> }
> fputc('\n',stdout);
>
> return 0;
> }
>
> void divide(char *dvd, unsigned dvr, unsigned prc)
> {
> char *Wk_q;
> unsigned bit, rmdr;
>
> rmdr = 0;
> for (Wk_q = dvd; Wk_q < dvd+prc; ++Wk_q)
> {
> for(bit = 0; bit < 8; ++bit)
> {
> rmdr *= 2;
> if (*Wk_q & 0200) rmdr += 1;
> *Wk_q <<= 1;
> if (rmdr >= dvr)
> {
> *Wk_q += 1;
> rmdr -= dvr;
> }
> }
> }
> }
>


A division of a long integer with a known length of bytes by an integer of 8 to 1bits in a digit by digit division which that every digit is at least 8 bits is still missing.

This is trivial in C, but I'll see if we have to start a c programming race here.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-26-2011
On Nov 25, 8:40*pm, 88888 Dihedral <(E-Mail Removed)>
wrote:

> A division of a long integer with a known length of bytes by an integer of 8 to
> 1bits *in a digit by digit *division which that every digit is at least 8 bits *is
> still missing.
>
> This is trivial in C, but I'll see if we have *to start a c programmingrace here.


Since I haven't seen one line of /your/ C code, I suggest that you
tackle this "trivial" problem yourself. I'm looking forward to your
solution.

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      11-26-2011
I know that a division in C in some compiler in most CPUs is really slow. I write that in assembly.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-26-2011
On 11/26/11 07:43 PM, 88888 Dihedral wrote:

If you are going to post to Usenet, for goodness sake quote the context
you are relying to!

> I know that a division in C in some compiler in most CPUs is really slow. I write that in assembly.


Examples and evidence please.

--
Ian Collins
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-26-2011
On November 26, 2011 06:11, in comp.lang.c, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Lew Pitcher ha scritto:

[snip]
>> for (p = 0; p < P; ++p)
>> {
>> e[0] = 0;
>> multiply(e,10,I+X);
>> fputc(e[0]+'0',stdout);
>> if (p%5 == 4)
>> if (p%50 == 49)

>
>
> here my compiler says:
>
> temp.c: In function ‘main’:
> temp.c:50: warning: suggest explicit braces to avoid ambiguous ‘else’


The C standard is pretty clear about the association of <<else>> to the
appropriate <<if>>For instance, in IEC 9899-1999, section 6.8.4.1 ("Theif
statement"), point 3, it says:
"An else is associated with the lexically nearest preceding if that is
allowed by the syntax."

That doesn't seem very ambiguous to me.

I suggest that your compiler's warning level is wound up too tight. I know
that compilers are permitted to emit warnings for anything they want, from
noting potential syntax errors to baying at a full moon. That doesn't mean
that the compiler doesn't know what to do with the code, or that the code
violates the C standard in any way. It just means that the compiler writer
thought that the programmer would like to know something.

Now, had my intent be to have the <<else>> associate to the /first/ <<if>>
statement in the sequence, then the warning would be relevant. But, since I
intended (as actually occurred) that the <<else>> be associated to
the /second/ <<if>> statement, and the C standard permits the expression I
used, then your compiler's warning is just noise.

>
>> fputs("\n\t",stdout);
>> else
>> fputc(' ',stdout);
>> }


HTH
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-26-2011
On November 26, 2011 01:43, in comp.lang.c, (E-Mail Removed)
wrote:

> I know that a division in C in some compiler in most CPUs is really slow.
> I write that in assembly.


Chicken.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      11-27-2011
On Saturday, November 26, 2011 3:13:26 PM UTC+8, Joe Wright wrote:
> On 11/26/2011 01:43, 88888 Dihedral wrote:
> > I know that a division in C in some compiler in most CPUs is really slow. I write that in assembly.

>
> Do you? Would you tell me what you know about division that the compiler
> writer doesn't know?
>
> --
> Joe Wright
> "If you rob Peter to pay Paul you can depend on the support of Paul."


I contribute a c function to divide a pointer to an unsigned char array with a known length that produces a remainder of the long real number.

int longreal_divide_mod5( char *longnum, int length)
{
int i;
unsinged long remainder; // 32 or 64 bits, I'll show the 32 bit version
tmp=0;

for( i=0, remainder=0;i<length;i++)
remainder+=longnum[i]; // mod 255 done in the first pass

// assume remainder won't over flow
tmp+= (remainder>>24); //top 8 bits
tmp+= ((remainder)<<>>24 ; // another 8 bits
tmp+= ((remainder)<<16)>>24 ; // another 8 bits
tmp+= remainder & 0xff;// final 8 bits
//4 *255<=1020 in 10 bits

while (i=tmp&512) if i { tmp^=512; tmp++};
while (i=tmp&256) if i { tmp^=256; tmp++};
// tmp in a byte now
return tmp%5; //there is another way to avoid the mod
}













 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what has python added to programming languages? (lets be esoteric,shall we ;) Wildemar Wildenburger Python 20 04-24-2006 01:56 PM
OT: Lets all get high James MCSE 93 10-19-2005 05:45 PM
Review: Lets get a T@2 Part 1 Silverstrand Reviews & How-To's 0 06-20-2005 02:34 AM
is there a hardware / driver combination out there the lets win2k connect to the network before login?? christiane kewitz Wireless Networking 1 02-13-2005 01:08 AM
Re: Olympus C-5050 Users Group - lets start one on usenet! gr Digital Photography 6 08-04-2003 07:57 AM



Advertisments