473,394 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

critique my program!

Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;

e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}
}
++rounds;
}
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}

Aug 11 '07 #1
10 1234
On Aug 10, 10:40 pm, vonbres...@gmail.com wrote:
Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;
Right off the bat, you exceed the array bounds, by assigning something
to the 8th element (array[7]).
Also, the functions for printing a line and printing a newline are not
as good as just doing it inline.

Because of the way you have constructed your program the sort is not
reusable.

I would suggest something more along these lines:

#include <stdlib.h>
/* The definition for etype should come form an include file or
command line #define or the like. */
typedef int etype;

void quadratic_sort_bi(etype array[], const size_t lo,
const size_t hi)
{
size_t i,
j,
h,
l;
for (i = lo + 1; i <= hi; i++) {
const etype tmp = array[i];
for (l = lo - 1, h = i; h - l 1;) {
j = (h + l) / 2;
if (tmp < array[j])
h = j;
else
l = j;
}
for (j = i; j h; j--)
array[j] = array[j - 1];
array[h] = tmp;
}
}

#ifdef UNIT_TEST

#include <stdio.h>
#include <time.h>

/* Because of promotion to double, the dumper should work for many
integral and floating point types */
void dump_arr(etype array[], size_t size)
{
size_t index;
for (index = 0; index < size; index++)
printf("array[%u]=%f\n", (unsigned) index, (double)
array[index]);
}

void init_arr(etype array[], size_t size)
{
size_t index;
for (index = 0; index < size; index++)
array[index] = (etype) rand();
}
int main(void)
{
int array[8];
size_t arr_size = sizeof array / sizeof array[0];
srand((unsigned)time(NULL));

init_arr(array, arr_size);
puts("before sort:");
dump_arr(array, arr_size);
quadratic_sort_bi(array, 0, arr_size - 1);
puts("after sort:");
dump_arr(array, arr_size);

return 0;
}

#endif

Aug 11 '07 #2
On Fri, 10 Aug 2007 22:40:46 -0700, vonbreslau wrote:
Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
Is there any reason for using three keystrokes instead than one
whenever you want to write 0, considering than 1) the compiler
proper (i.e. the one which parses preprocessed code) will not even
be aware of that, 2) this doesn't improves readability in no way
whatsoever, and 3) its value will never change?
#define LIM 7
/* Sort numbers according to its increasing size */ void e(void); void
printar(void);
void pline(void);

int array[LIM];
int array[LIM] = {99, 0, 55, 18, 2, 67, 0, 9}
int main()
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18; array[4]=2;
array[5]=67; array[6]=0; array[7]=9;
You can also initialize it when you declare it, see above.
e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}
Look up selection sort, is much faster than bubblesort and it is
about as simple as it.
}
++rounds;
}
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)
You might want to pass the address and size of the array as
parameters.
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 11 '07 #3
Army1987 wrote:
On Fri, 10 Aug 2007 22:40:46 -0700, vonbreslau wrote:
>Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
Is there any reason for using three keystrokes instead than one
whenever you want to write 0, considering than 1) the compiler
proper (i.e. the one which parses preprocessed code) will not even
be aware of that, 2) this doesn't improves readability in no way
whatsoever, and 3) its value will never change?
>#define LIM 7
/* Sort numbers according to its increasing size */ void e(void); void
printar(void);
void pline(void);

int array[LIM];
int array[LIM] = {99, 0, 55, 18, 2, 67, 0, 9}
You left off the semicolon and initialised one to many items.

One good thing about initialising an array this way is the compiler will
be able to spot the error!

--
Ian Collins.
Aug 11 '07 #4
<vo********@gmail.comwrote:
Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()
array is what is called a global variable. This is bad practice, and while
not harmful here, there is nothing gained by making it global. Move the
array definition to below main. Rule of thumb: don't use global variables
unless there is a darn good reason.

I quit looking after seeing that.
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;

e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}
}
++rounds;
}
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}

Aug 11 '07 #5
"osmium" <r1********@comcast.netwrites:
<vo********@gmail.comwrote:
>Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()

array is what is called a global variable. This is bad practice, and while
not harmful here, there is nothing gained by making it global. Move the
array definition to below main. Rule of thumb: don't use global
variables
Not below main at all. Into main is what you meant. A big difference.

And had he done that then he must change the function declarations to
take a reference to the array. For the sake of this test code it hardly
seems worth it .... Clearly if this is NOT "test and learn" code then
globals are not the way.
unless there is a darn good reason.
There are many reasons. This might not be one of them....
>
I quit looking after seeing that.
Possibly you are in the wrong NG? This is a help newsgroup. If you get
all prissy and spit the dummy because of someone breaking one of the
conventions that YOU favour then you really are probably in the wrong
place.

>{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;

e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}
}
++rounds;
}
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}

--
Aug 11 '07 #6
Thanks a lot all of you guys for your comments!

Everyone gave me something to think about and to see the possibilities
of this language with your examples. For instance I certainly must
investigate all this:

"you exceed the array bounds by assigning something to the 8th
element",
the alternate way to initialize arrays
array[] = {x, x2, x3},
the fact that putting the array outside main is considered a bad
practice and could be harmful ( yet my program didn't worked until I
put it outside, don't have a clue why!)
Later someone said that if the array was to be into main then there
should be function declaration to reference it.
I trust all these things would be revealed in the next chapters of my
K&R book.

Aug 11 '07 #7
On Sat, 11 Aug 2007 11:44:05 -0700, vonbreslau wrote:
Thanks a lot all of you guys for your comments!

Everyone gave me something to think about and to see the possibilities
of this language with your examples. For instance I certainly must
investigate all this:

"you exceed the array bounds by assigning something to the 8th
element",
int array[7]; declares an array of 7 elements, numbered from
array[0] to array [6].
the alternate way to initialize arrays
array[] = {x, x2, x3},
the fact that putting the array outside main is considered a bad
practice and could be harmful ( yet my program didn't worked until I
put it outside, don't have a clue why!)
Because then the function printarray() can't see it (unless you
pass it a pointer to it).
Later someone said that if the array was to be into main then there
should be function declaration to reference it.
I trust all these things would be revealed in the next chapters of my
K&R book.
Yes. You might find the FAQ www.c-faq.com useful, in case you
should not understand something.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 11 '07 #8
vo********@gmail.com wrote:
Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
One person already recommended against the definition for ZER, but not
LIM. I agree. The difference is that LIM, presumably an abbreviation
for limit, can be seen as a parameter. You may later want to change
that value. ZER, an abbreviation for zero, should never change and has
no meaning beyond the constant zero. If your code had an adjustable
lower limit, which happened to be 0, but might be changed to something
else, then you could write
#define LOWER_LIMIT 0 /* lowest sort index */
I don't think it applies to your current program, though, since the
lower limit is not expected to change.
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()
{
int i, temporal, rounds;
One of my habits I use to reduce errors is to add a comment for each
declaration, most importantly variables and data types. If the variable
usage is blindingly obvious, such as i here, I will probably write /*
temp */. But what about temporal and rounds? What do they really hold?
My suggestion is /* temporary for swap */ and /* number of swap passes
completed */. This helps because a clear definition reduces confusion
over how it should be used.

As an example of reducing confusion, look at LIM. Is it the number of
elements in the array or is it the highest index of an array element?
Part of your code assumes one definition while other parts assume
another. By being explicit in the name and comment for the definition,
you help to prevent those types of problems:
#define N_VALUES 8 /* number of values to sort */
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;

e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
How did you choose your limit for the while loop? Is it always
sufficient? Is it more than needed?
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}
}
++rounds;
}
There are many ways to write a loop for a fixed number of iterations, as
you do with the while loop. My favorite is the for loop, since it has
the counter initialization, termination test, and increment together,
making it easier for me to read. The while loop is more appropriate,
IMO, when the index variable does not get incremented in a regular
manner at the end/beginning of the loop.
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
Here you use both a tab and spaces to separate elements. It would
probably be better with one or the other, not both. Also, I would
probably have printar output a terminating new line, unless you expect
to use it some time without a following new line. That is a minor
issue, though.
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}
Have fun with C!

--
Thad
Aug 11 '07 #9

"Ian Collins" <ia******@hotmail.comwrote in message
news:5i*************@mid.individual.net...
One good thing about initialising an array this way is the compiler will
be able to spot the error!
isnt it strange that not many compilers spot this already? lint would see
it, why dont compilers do this yet

int arr[2];

arr[0] = 0;
arr[1] = 1;
arr[2] = 2;
Aug 12 '07 #10
Serve Lau wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5i*************@mid.individual.net...
>One good thing about initialising an array this way is the compiler will
be able to spot the error!

isnt it strange that not many compilers spot this already? lint would see
it, why dont compilers do this yet

int arr[2];

arr[0] = 0;
arr[1] = 1;
arr[2] = 2;
There has to be a trade off between optional checking and compile speed,
that's why we have tools like lint.

--
Ian Collins.
Aug 12 '07 #11

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

Similar topics

54
by: Martin Eisenberg | last post by:
Hi, I've written a program that I'd like to hear some opinions on regarding my C++ usage. As a program it's smallish, but on Usenet 300 lines seem a bit much. Do you think it's appropriate to...
19
by: TC | last post by:
Are there any good sites or forums for a web critique? I went to alt.html.critique and it's pretty dead.
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
8
by: G Patel | last post by:
I wrote the following program to remove C89 type comments from stdin and send it to stdout (as per exercise in K&R2) and it works but I was hoping more experienced programmer would critique the...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
39
by: Eric | last post by:
There is a VB.NET critique on the following page: http://www.vb7-critique.741.com/ for those who are interested. Feel free to take a look and share your thoughts. Cheers, Eric. Ps: for those...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
2
by: winston | last post by:
I wrote a Python program (103 lines, below) to download developer data from SourceForge for research about social networks. Please critique the code and let me know how to improve it. An...
2
by: Kevin McKinley | last post by:
# I posted a few days ago about a memory leak that I think i'm having with my first Tkinter program. # I've had trouble pinpointing what is wrong so i thought i would submit the code and see if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.