Re: [C] erroneous fprintf output
From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/11/04
- Next message: Francis Glassborow: "Re: [C] erroneous fprintf output"
- Previous message: Arthur J. O'Dwyer: "Re: [C] erroneous fprintf output"
- In reply to: Curley Q.: "[C] erroneous fprintf output"
- Next in thread: Curley Q.: "Re: [C][OT] erroneous fprintf output"
- Reply: Curley Q.: "Re: [C][OT] erroneous fprintf output"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Francis Glassborow: "Re: [C] erroneous fprintf output"
- Previous message: Arthur J. O'Dwyer: "Re: [C] erroneous fprintf output"
- In reply to: Curley Q.: "[C] erroneous fprintf output"
- Next in thread: Curley Q.: "Re: [C][OT] erroneous fprintf output"
- Reply: Curley Q.: "Re: [C][OT] erroneous fprintf output"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|