Re: [C] erroneous fprintf output

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/11/04


Date: Tue, 11 May 2004 00:40:38 +0100

In message <409FFD24.8040305@bogus.net>, Curley Q. <curleyq@bogus.net>
writes
>The exercise is to write a program that takes the sqrt of all integers
>1-100 and separate them into two sets whose sums are nearly equal.
>"Nearly equal" is defined by some threshold value. My approach is to
>select the the two sets at random, and compare their sums until I get
>two that meet the "nearly equal" criterion. Background not really
>important to my problem, but there it is.
>
>Problem:
>The code below outputs the correct values to stdout. But if I write
>them to a file with fprintf it outputs the correct value for the first
>sum, and -nan for the second.

The problem is that the format flags for the printf family are not the
same as those for the scanf family.

For output any arguments of type float will have been promoted to double
so %f handles both floats and doubles. (There is no lf for printf only
Lf which is for long doubles)

>
>I know some of you would like to p--s all over this kludgy prototype
>with its magic numbers, goto branching, lack of error checking,
>confusing array of index variables, etc, and I will gladly submit to
>flagellation if you will find the solution to my problem. BTW, it's not
>homework; I'm sweating out every line on my own.

My criticism is that awful selection mechanism that isn't even
guaranteed to ever terminate when creating a single shuffle of the 100
numbers. If you want to do it by random selection use a standard shuffle
function:

void swap_random_with_last(int array[], int size){
   int choice = rand() % size;
   int temp = array[choice];
   array[choice] = array[size-1];
   array[size-1] = temp;
}

void shuffle(int array[], int size){
   int i;
   for(i = size; i != 1; i--){
     swap_random_with last(array, i);
}

Now create an array of 100 entries and initialise them with 1 to 100 and
call shuffle.

However are you required to have an equal number of items in each (i.e.
50 : 50)?

Your code is one of the worst pieces of spaghetti I have seen in a long
while including jumping out of a loop with a goto. I am not even clear
as to what it is actually doing (and, to be honest, I do not think you
know either)

Programming isn't just throwing together some semi-random source code
statements that the compiler is happy to compile, it requires thought
and planning before you even start to write code. Yes, many of us appear
to cut code right off the bat but in fact we are just calling on our
experience and insights into a problem to start writing code (and all
the while we are willing to throw away the first cut when we realise
that there is a clearly better approach.

Consider creating two queues (stacks or whatever other image you like)
and add the next square-root to whichever stack currently has the
smaller total. Initially the values will go on alternating piles, but
eventually the one that is getting the even numbers will get far enough
ahead so that the square roots of a consecutive pair will both get added
to the first pile, then they will alternate again. Actually
investigating the pattern of when successive square roots go onto the
same pile could be quite interesting, but that is another problem.

If you are dealing with 100 numbers you just might find that this
algorithm results in a pile of 51 and another of 49 but no worse than
that (proving that is not very hard).

When you have two piles you may need to fine tune to get them more equal
if that is what you want.

Of course my planned solution is very different from yours and will have
far less complexity but that is the art of programming.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: 11-plus grammar
    ... written maths problems has gone down over the last 25 years. ... Most people can do sums if they are written as sums, but children are no longer taught to solve written problems, as we were in the 60s. ... but it is also true that some questions are written better than others (20 plus years ago I worked in an exams board in a job that partly involved weeding out poorly written questions). ... My first stab at visualising the required scenario was to have the first pile as 3x3x1 and the next pile either as 3x3x2 or as any configuration that produced another step one brick high, but then I pretty well gave up. ...
    (alt.usage.english)
  • Re: 11-plus grammar
    ... written maths problems has gone down over the last 25 years. ... Most people can do sums if they are written as sums, but children are no longer taught to solve written problems, as we were in the 60s. ... My first stab at visualising the required scenario was to have the first pile as 3x3x1 (one brick high) and the next pile either as 3x3x2 or as any configuration that produced another step one brick high, but then I pretty well gave up. ...
    (alt.usage.english)
  • Re: Tcl event loop and threading
    ... The quote that sums up the argument for me is this one: ... "Nondeterminism should be explicitly added to programs, and only where needed, as it is in sequential programming. ...
    (comp.lang.tcl)
  • Re: The Case Against RosAsm (#7) (LONG)
    ... > he could learn to use fast hash tables, dynamic trees and a whole pile ... > of other normal programming style structures that are a part of modern ...
    (alt.lang.asm)
  • Re: Ha! Rivera in cuz he needs the work?
    ... That move sums up the season. ... I didn't want to pile on in here last night so kept my opinion to ... pitches tonight though? ...
    (alt.sports.baseball.ny-yankees)