Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #161678

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 Real Troll <real.troll@trolls.com>
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-05 18:45 +0000
Organization Aioe.org NNTP Server
Message-ID <sbvk6t$1upr$1@gioia.aioe.org> (permalink)
References <142675bd-7993-49b5-9b68-40864f26003an@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c | Previous | NextPrevious in thread | Find similar | Unroll thread


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