Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #161641
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash |
| Date | 2021-07-04 13:56 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87tulack51.fsf@bsb.me.uk> (permalink) |
| References | <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com> |
hamid daniali <hamiddaniaali@gmail.com> writes:
> #include <iostream>
Wrong group. You want comp.lang.c++ but apparently Google Groups has
trouble with the ++. If you plan to use Usenet a lot, get a news reader
and a news server account (there are some free ones out there).
> using namespace std;
> void swap(int *,int *);
Why, in C++, would you not use references?
> void sort (int [],int );
> int main()
> {
> const int size=12;
> int a[size]={1,2,96,21,17,3,9,14};
In C (and not C++) this would be using an optional feature called a
variable length array.
Note that size is a bad name because it's ambiguous. I'd use length or
some such term. sizeof a is most likely 48 because the "size" is the
size in bytes.
> sort(a,size);
> for(int i=0;i<size;i++)
> cout<<a[i]<<" ";}
> void sort (int a[],int size)
Your code layout is mighty odd. Many Google messed it up on posting...
> {
> int i,j;
> int *p;
> p=a;
Why not "int *p = a;" but I don't see the purpose of an extra pointer.
> for(i=0;i<size-1;i++)
> for(j=1;j<size;j++)
> if (*(p+j+1)<*(p+j))
I'd write p[j+1] and p[j] here. But note that p[0] is never looked at
and (as has already been pointed out) when j == size-1, p[j+1] is out of
bounds.
> swap ((p+j+1),(p+j));}
I fond the extra () slightly distracting. If you want to keep a symmetry
with the p[j+1] notation you can write &p[j+1] for p+j+1.
> void swap(int *pa,int *pb)
> {int t;
> t=*pa;
> *pa=*pb;
> *pb=t;
> }
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash hamid daniali <hamiddaniaali@gmail.com> - 2021-07-04 03:28 -0700
Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash WeAllWant ToBeHappy <weallwanttobehappy@gmail.com> - 2021-07-04 03:56 -0700
Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-04 13:56 +0100
Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash Manfred <noname@add.invalid> - 2021-07-04 23:31 +0200
Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash David Brown <david.brown@hesbynett.no> - 2021-07-05 08:10 +0200
Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash Vir Campestris <vir.campestris@invalid.invalid> - 2021-07-04 21:27 +0100
Re: Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash Real Troll <real.troll@trolls.com> - 2021-07-05 18:45 +0000
csiph-web