What is wrong?

D

DJ

What is wrong?


#include <stdio.h>

#define N 8

void order(int *p, int *q);

int main(void)
{
int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
int i;
int j;
int k;

printf("\n Unordered data:");
for (k = 0; k < N; ++k)
printf("%6d", a[k]);
printf("\n");
for (i = 0; i < N; ++i) {
for (j = i + 1; j < N; ++j)
order(&a, &a[j]);
printf(" After pass %d:", i + 1);
for (k = 0; k < N; ++k)
printf("%6d", (k % 2 == 0) ? 333 : a[k]);
printf("\n");
}
printf("\n Something is wrong!\n");
putchar('\n');
return 0;
}

void order(int *p, int *q)
{
int tmp;

if (*p > *q) {
tmp = *p;
*p = *q;
*q = tmp;
}
}
 
C

CBFalconer

DJ said:
What is wrong?

#include <stdio.h>

#define N 8

void order(int *p, int *q);

int main(void)
{
int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
int i;
int j;
int k;

printf("\n Unordered data:");
for (k = 0; k < N; ++k)
printf("%6d", a[k]);
printf("\n");
for (i = 0; i < N; ++i) {
for (j = i + 1; j < N; ++j)
order(&a, &a[j]);
printf(" After pass %d:", i + 1);
for (k = 0; k < N; ++k)
printf("%6d", (k % 2 == 0) ? 333 : a[k]);


printf("%6d", a[k]);
 
E

Emmanuel Delahaye

DJ wrote on 24/07/05 :
What is wrong?

printf("%6d", (k % 2 == 0) ? 333 : a[k]);

I don't know why are you using this, but your code is correct. Here is
a little enhanceded version of your code :

#include <stdio.h>

void order(int *p, int *q)
{
if (*p > *q)
{
int tmp = *p;
*p = *q;
*q = tmp;
}
}

int main(void)
{
#define N 8
int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
int i;
int j;
int k;

printf("\n Unordered data:");
for (k = 0; k < N; ++k)
printf("%6d", a[k]);
printf("\n");

for (i = 0; i < N; ++i) {
for (j = i + 1; j < N; ++j)
order(a+i, a+j);
printf(" After pass %d:", i + 1);
for (k = 0; k < N; ++k)
printf("%6d", a[k]);
printf("\n");
}

return 0;
}


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
M

Martin Ambuhl

DJ said:
What is wrong?

Apparently, since you didn't tell us, you think the '333' outputs of
for (k = 0; k < N; ++k)
printf("%6d", (k % 2 == 0) ? 333 : a[k]);
printf("\n");

indicate something is wrong. But that is bogus, of course. You tell the
program to print '333' instead of a[0], a[2], a[4], and a[6] and it does
so. Then you complain.
 
B

Barry Schwarz

What is wrong?

Give us a hint. Why do you think anything is wrong? What results are
you getting that you don't expect? What results are you missing that
expected?
#include <stdio.h>

#define N 8

void order(int *p, int *q);

int main(void)
{
int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
int i;
int j;
int k;

printf("\n Unordered data:");
for (k = 0; k < N; ++k)
printf("%6d", a[k]);
printf("\n");
for (i = 0; i < N; ++i) {
for (j = i + 1; j < N; ++j)
order(&a, &a[j]);
printf(" After pass %d:", i + 1);
for (k = 0; k < N; ++k)
printf("%6d", (k % 2 == 0) ? 333 : a[k]);


What is this intended to accomplish?
printf("\n");
}
printf("\n Something is wrong!\n");
putchar('\n');
return 0;
}

void order(int *p, int *q)
{
int tmp;

if (*p > *q) {
tmp = *p;
*p = *q;
*q = tmp;
}
}



<<Remove the del for email>>
 
D

Default User

DJ said:
What is wrong?

Others addressed the problem that you likely posted about, but I don't
care for another part of the problem, although it's not technically
wrong:
#define N 8
int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
for (k = 0; k < N; ++k)
printf("%6d", a[k]);


Your size macro is disjoint from the declaration of the array, and is a
safety hazard waiting to happen. Normally you let the compiler size the
array when you don't really want to use the size later. In this case
you do. I'd either change it to:

int a[N] = {7, 3, 66, 3, -5, 22, -77, 2};


Or capture the size:

size_t size = sizeof a / sizeof a[0];



Brian
 
A

akarl

Barry said:
Give us a hint. Why do you think anything is wrong? What results are
you getting that you don't expect? What results are you missing that
expected?

What's the point in helping someone who is too lazy to specify the
problem? In these cases it also seems to be less probable that the OP
will reply (or even read the replies for that matter).


August
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top