Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #161639 > unrolled thread
| Started by | hamid daniali <hamiddaniaali@gmail.com> |
|---|---|
| First post | 2021-07-04 03:28 -0700 |
| Last post | 2021-07-05 18:45 +0000 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.c
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
| From | hamid daniali <hamiddaniaali@gmail.com> |
|---|---|
| Date | 2021-07-04 03:28 -0700 |
| Subject | 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 |
| Message-ID | <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com> |
#include <iostream>
using namespace std;
void swap(int *,int *);
void sort (int [],int );
int main()
{
const int size=12;
int a[size]={1,2,96,21,17,3,9,14};
sort(a,size);
for(int i=0;i<size;i++)
cout<<a[i]<<" ";}
void sort (int a[],int size)
{
int i,j;
int *p;
p=a;
for(i=0;i<size-1;i++)
for(j=1;j<size;j++)
if (*(p+j+1)<*(p+j))
swap ((p+j+1),(p+j));}
void swap(int *pa,int *pb)
{int t;
t=*pa;
*pa=*pb;
*pb=t;
}
[toc] | [next] | [standalone]
| From | WeAllWant ToBeHappy <weallwanttobehappy@gmail.com> |
|---|---|
| Date | 2021-07-04 03:56 -0700 |
| Message-ID | <26ee69c1-aadd-498a-9198-e549e76ab1e7n@googlegroups.com> |
| In reply to | #161639 |
On Sunday, July 4, 2021 at 11:29:04 AM UTC+1, hamid daniali wrote:
> int a[size]={1,2,96,21,17,3,9,14};
Some 0's added to pad out to 12 elements
> for(j=1;j<size;j++)
> if (*(p+j+1)<*(p+j))
When j=size-1, p+j+1 is off the end of the array
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2021-07-04 13:56 +0100 |
| Message-ID | <87tulack51.fsf@bsb.me.uk> |
| In reply to | #161639 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Manfred <noname@add.invalid> |
|---|---|
| Date | 2021-07-04 23:31 +0200 |
| Message-ID | <sbt9bc$ckk$1@gioia.aioe.org> |
| In reply to | #161641 |
On 7/4/2021 2:56 PM, Ben Bacarisse wrote: > hamid daniali <hamiddaniaali@gmail.com> writes: > [...] > > Your code layout is mighty odd. Many Google messed it up on posting... > Definitely odd layout, which might be source of confusion for the OP in the first place. [...] > >> 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. > The intention seems to be a basic O(N^2) algorithm (with mistakes, which I won't do a rewrite for because this sounds like a homework assignment). Hint (nonetheless): the outer 'for' loop iterates on 'i', but 'i' is never used. This might be more visible if the code had been properly indented.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2021-07-05 08:10 +0200 |
| Message-ID | <sbu7ph$9li$1@dont-email.me> |
| In reply to | #161652 |
On 04/07/2021 23:31, Manfred wrote: > On 7/4/2021 2:56 PM, Ben Bacarisse wrote: >> hamid daniali <hamiddaniaali@gmail.com> writes: >> > [...] >> >> Your code layout is mighty odd. Many Google messed it up on posting... >> > > Definitely odd layout, which might be source of confusion for the OP in > the first place. > [...] > >> >>> 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. >> > > The intention seems to be a basic O(N^2) algorithm (with mistakes, which > I won't do a rewrite for because this sounds like a homework assignment). > Hint (nonetheless): the outer 'for' loop iterates on 'i', but 'i' is > never used. This might be more visible if the code had been properly > indented. Second hint - always enable plenty of warnings on your compiler! A good compiler can give lots of help (such as telling you about unused variables) if you let it.
[toc] | [prev] | [next] | [standalone]
| From | Vir Campestris <vir.campestris@invalid.invalid> |
|---|---|
| Date | 2021-07-04 21:27 +0100 |
| Message-ID | <sbt5jg$i23$1@dont-email.me> |
| In reply to | #161639 |
On 04/07/2021 11:28, hamid daniali wrote:
> #include <iostream>
> using namespace std;
> void swap(int *,int *);
> void sort (int [],int );
> int main()
> {
> const int size=12;
> int a[size]={1,2,96,21,17,3,9,14};
> sort(a,size);
> for(int i=0;i<size;i++)
> cout<<a[i]<<" ";}
> void sort (int a[],int size)
> {
> int i,j;
> int *p;
> p=a;
> for(i=0;i<size-1;i++)
> for(j=1;j<size;j++)
> if (*(p+j+1)<*(p+j))
> swap ((p+j+1),(p+j));}
> void swap(int *pa,int *pb)
> {int t;
> t=*pa;
> *pa=*pb;
> *pb=t;
> }
>
Best thing for you to do is to run it step-by-step through a debugger,
then you'll be able to see for yourself.
FYI C++ has both sorted collections (such as std::set) and built in sort
algorithms; the fact that you aren't using them suggests to me that
you're just learning.
In which case learning how to use the debugger for your platform will be
far more valuable to you than having me tell you what is wrong.
It's not immediately obvious to me what it is though.
Andy
[toc] | [prev] | [next] | [standalone]
| From | Real Troll <real.troll@trolls.com> |
|---|---|
| Date | 2021-07-05 18:45 +0000 |
| Message-ID | <sbvk6t$1upr$1@gioia.aioe.org> |
| In reply to | #161639 |
On 04/07/2021 11:28, hamid daniali wrote:
> #include <iostream>
> using namespace std;
> void swap(int *,int *);
> void sort (int [],int );
> int main()
> {
> const int size=12;
> int a[size]={1,2,96,21,17,3,9,14};
> sort(a,size);
> for(int i=0;i<size;i++)
> cout<<a[i]<<" ";}
> void sort (int a[],int size)
> {
> int i,j;
> int *p;
> p=a;
> for(i=0;i<size-1;i++)
> for(j=1;j<size;j++)
> if (*(p+j+1)<*(p+j))
> swap ((p+j+1),(p+j));}
> void swap(int *pa,int *pb)
> {int t;
> t=*pa;
> *pa=*pb;
> *pb=t;
> }
You are mixing C++ syntax with C and this might be the problem for you.
I suggest start with this simple code that works in GCC & VS:
> #include <stdio.h>
> #include <stdlib.h>
>
> int cmpfunc(const void *a, const void *b)
> {
> return (*(int *)a - *(int *)b);
> }
>
> int main()
> {
> int a[] = {1, 2, 96, 21, 17, 3, 9, 14};
> size_t size = sizeof(a) / sizeof(a[0]);
> qsort(a, size, sizeof(int), cmpfunc);
> for (int i = 0; i < size; i++)
> {
> printf("%4d", a[i]);
> }
>
> return EXIT_SUCCESS;
> }
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web