|
From: sophia on 14 Apr 2008 08:55 Dear all, what are the corrections/improvements needed in the following program which shows the creation of heap from an array of elements ? #include<stdio.h> #include<stdlib.h> #define MAX 50 void restoreup(int,int*); void restoredown(int,int*,int); void makeheap(int*,int); void add(int,int*,int*); void display(int*,int); int readval(void); int del(int*,int*); int main(void) { int a[MAX] = {1000,7,10,25,17,23}; int n = 5,i; makeheap(a,n); display(a,n); printf("\n\nEnter the element you want to add:: "); i= readval(); add(i,a,&n); printf("\nAfter adding the element %d\n",i); display(a,n); i = del(a,&n); printf("\n\nAfter Deletion\n"); display(a,n); puts(""); return EXIT_SUCCESS; } void makeheap(int *a,int n) { int i; for(i= (n/2);i>= 1;i--) restoredown(i,a,n); } void restoredown(int pos,int *a,int n) { int i,val; val = a[pos]; while(pos <= n/2) { i = 2 * pos; if((i < n) && (a[i] < a[i+1])) i++; if(val >= a[i]) break; a[pos] = a[i]; pos = i; } a[pos] = val; } int readval() { int i,ch; while( (1 != scanf("%d",&i)) ) { while( (ch= fgetc(stdin)) != EOF && ch != '\n'); printf("\nSorry i could n't read that\nplease re enter the value:: "); if(1 == scanf("%d",&i)) break; } return i; } void display(int *a,int n) { int i; printf("\n**********"); printf("\n HEAP "); printf("\n**********\n"); puts(""); for(i=1;i <= n;i++) printf("%d\t",a[i]); } void add(int val,int* a,int* n) { (*n)++; a[*n] = val; restoreup(*n,a); } void restoreup(int i,int *a) { int val; val = a[i]; while(a[i/2] <= val) { a[i] = a[i/2]; i = i/2; } a[i] = val; } int del(int* a , int *n) { int temp,i; temp = a[1]; a[1] = a[*n]; (*n)--; restoredown(1,a,*n); return temp; }
From: Richard Heathfield on 14 Apr 2008 09:46 sophia said: > Dear all, > > what are the corrections/improvements needed in the following program > which shows the creation of heap from an array of elements ? What are your thoughts on this? What have you tried? What are you stuck on? <snip> -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
From: sophia on 14 Apr 2008 10:17 On Apr 14, 6:46 pm, Richard Heathfield <r...(a)see.sig.invalid> wrote: > sophia said: > > > Dear all, > > > what are the corrections/improvements needed in the following program > > which shows the creation of heap from an array of elements ? > > What are your thoughts on this? What have you tried? What are you stuck on? i am not getting you , what you mean by "what are you stuck on?"
From: Richard Heathfield on 14 Apr 2008 10:27 sophia said: > On Apr 14, 6:46 pm, Richard Heathfield <r...(a)see.sig.invalid> wrote: >> sophia said: >> >> > Dear all, >> >> > what are the corrections/improvements needed in the following program >> > which shows the creation of heap from an array of elements ? >> >> What are your thoughts on this? What have you tried? What are you stuck >> on? > > i am not getting you , > what you mean by "what are you stuck on?" Which aspect of the problem you stated are you having difficulty in solving? -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
From: sophia on 14 Apr 2008 13:40 On Apr 14, 10:27 am, Richard Heathfield <r...(a)see.sig.invalid> wrote: > sophia said: > > > On Apr 14, 6:46 pm, Richard Heathfield <r...(a)see.sig.invalid> wrote: > >> sophia said: > > >> > Dear all, > > >> > what are the corrections/improvements needed in the following program > >> > which shows the creation of heap from an array of elements ? > > >> What are your thoughts on this? What have you tried? What are you stuck > >> on? > > > i am not getting you , > > what you mean by "what are you stuck on?" > > Which aspect of the problem you stated are you having difficulty in > solving? well i have posted here, because i can get feedback from C gurus like you,so that i can improve mu programming skills. I would like to know if there is any other simpler/good method for creating heap from an array of elements
|
Next
|
Last
Pages: 1 2 Prev: Tree from inorder and pre-order traversals Next: becoming a system programmer |