Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Don't think; let the machine do it for you!" -- E. C. Berkeley


devel / comp.lang.c / 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

SubjectAuthor
* Does anybody have any idea that why the output is not the sortedhamid daniali
+- Re: Does anybody have any idea that why the output is not the sortedWeAllWant ToBeHappy
+* Re: Does anybody have any idea that why the output is not the sorted array and hBen Bacarisse
|`* Re: Does anybody have any idea that why the output is not the sortedManfred
| `- Re: Does anybody have any idea that why the output is not the sortedDavid Brown
+- Re: Does anybody have any idea that why the output is not the sortedVir Campestris
`- Re: Does anybody have any idea that why the output is not the sortedReal Troll

1
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

<142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17227&group=comp.lang.c#17227

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:4cd0:: with SMTP id l16mr7807540qtv.54.1625394537101;
Sun, 04 Jul 2021 03:28:57 -0700 (PDT)
X-Received: by 2002:ae9:f30c:: with SMTP id p12mr8775381qkg.19.1625394536914;
Sun, 04 Jul 2021 03:28:56 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 4 Jul 2021 03:28:56 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2.188.3.142; posting-account=XGaSHQoAAAAVHOc-2ce31rit8aSRcgap
NNTP-Posting-Host: 2.188.3.142
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
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
From: hamiddan...@gmail.com (hamid daniali)
Injection-Date: Sun, 04 Jul 2021 10:28:57 +0000
Content-Type: text/plain; charset="UTF-8"
 by: hamid daniali - Sun, 4 Jul 2021 10:28 UTC

#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;
}

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

<26ee69c1-aadd-498a-9198-e549e76ab1e7n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17228&group=comp.lang.c#17228

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:584f:: with SMTP id h15mr8051139qth.362.1625396189298;
Sun, 04 Jul 2021 03:56:29 -0700 (PDT)
X-Received: by 2002:a05:6214:8ec:: with SMTP id dr12mr8509069qvb.12.1625396189166;
Sun, 04 Jul 2021 03:56:29 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 4 Jul 2021 03:56:28 -0700 (PDT)
In-Reply-To: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=78.148.175.158; posting-account=awf5hgkAAABqmRbuxFvyskemnmkj_c_3
NNTP-Posting-Host: 78.148.175.158
References: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <26ee69c1-aadd-498a-9198-e549e76ab1e7n@googlegroups.com>
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
From: weallwan...@gmail.com (WeAllWant ToBeHappy)
Injection-Date: Sun, 04 Jul 2021 10:56:29 +0000
Content-Type: text/plain; charset="UTF-8"
 by: WeAllWant ToBeHappy - Sun, 4 Jul 2021 10:56 UTC

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

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

<87tulack51.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17229&group=comp.lang.c#17229

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
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: Sun, 04 Jul 2021 13:56:42 +0100
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <87tulack51.fsf@bsb.me.uk>
References: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="88b05831db0d35b478f9edb0e6f6324f";
logging-data="17620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19u4Ot2IYtfyULhEFkEOAPS6zDlXuay9Ko="
Cancel-Lock: sha1:Wo6MGC5X8kMP1Q032Y2AARviJak=
sha1:SyIfAU/N7LKl5XWTGvMU8W7TBf8=
X-BSB-Auth: 1.f8163c9a4de13c6a53e4.20210704135642BST.87tulack51.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 4 Jul 2021 12:56 UTC

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.

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

<sbt5jg$i23$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17238&group=comp.lang.c#17238

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: vir.camp...@invalid.invalid (Vir Campestris)
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: Sun, 4 Jul 2021 21:27:28 +0100
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <sbt5jg$i23$1@dont-email.me>
References: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 4 Jul 2021 20:27:28 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="7d02125e4cec9ac873fcde5e6961a9f1";
logging-data="18499"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191r3VjSVGYK1hb7eYdYFBrbaNVxrGz+G8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:1/WekAhDymkhWzuBbAgQy0BJ5zQ=
In-Reply-To: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
Content-Language: en-GB
 by: Vir Campestris - Sun, 4 Jul 2021 20:27 UTC

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

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

<sbt9bc$ckk$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17239&group=comp.lang.c#17239

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!ElFVPqY+TvFDjG0suNPrQQ.user.gioia.aioe.org.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
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: Sun, 4 Jul 2021 23:31:26 +0200
Organization: Aioe.org NNTP Server
Lines: 32
Message-ID: <sbt9bc$ckk$1@gioia.aioe.org>
References: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
<87tulack51.fsf@bsb.me.uk>
NNTP-Posting-Host: ElFVPqY+TvFDjG0suNPrQQ.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Sun, 4 Jul 2021 21:31 UTC

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.

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

<sbu7ph$9li$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17245&group=comp.lang.c#17245

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
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: Mon, 5 Jul 2021 08:10:56 +0200
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <sbu7ph$9li$1@dont-email.me>
References: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
<87tulack51.fsf@bsb.me.uk> <sbt9bc$ckk$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 5 Jul 2021 06:10:57 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="15622ff4d199f1d0aa0977a641ff4dc4";
logging-data="9906"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+c/rDN9CZ8VWEQ/DtXc/VzYwfHyG/xqmM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:mQkbU3vuXxBQl4fF+4x3Vs8jpa4=
In-Reply-To: <sbt9bc$ckk$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Mon, 5 Jul 2021 06:10 UTC

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.

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

<sbvk6t$1upr$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17265&group=comp.lang.c#17265

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!N3RkA6XWd6njRvlzOTcjxg.user.gioia.aioe.org.POSTED!not-for-mail
From: real.tr...@trolls.com (Real Troll)
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: Mon, 5 Jul 2021 18:45:00 +0000
Organization: Aioe.org NNTP Server
Lines: 54
Message-ID: <sbvk6t$1upr$1@gioia.aioe.org>
References: <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>
NNTP-Posting-Host: N3RkA6XWd6njRvlzOTcjxg.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@aioe.org
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Real Troll - Mon, 5 Jul 2021 18:45 UTC

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;
> }
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor