#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0

    Few errors in program.


    I am getting few errors in program. Plz correct them. I am trying for recursive Bubble sort.


    Code:
    #include "stdafx.h"
    #include<stdio.h>
    #include<conio.h>
    int Brecurrsion(int, int *,int);
    int main()
    {
    	int i=0;int j=0;
    	int arr[10],b[10];
    	int k;
    	printf("\n Enter the no of elements of Array ");
    	for(i=0;i<9;i++)
    	{
            scanf("%d",&arr[i]);
    	}
        int Brecurrsion(int i,&arr[0],10); // error C2059: syntax error : '&'
    	
    	_getch();
    	return 0;
    }
      int Brecurrsion(int r, int *p, int q)
     {
    			int i,j=0; int temp=0;
    			
    			if(i=q-1 && (p[q-1]> p[q-2]))
    				printf("%d",p[q-1]);
    			else
    				for(i=0;i<=q-2;)
    				{
    
    				if(p[i]>p[i+1])
    				{
    					temp= *p;
    				    *p= *(p+1);
    					*(p+1)=*p;
    				     i++;
    				}
    				   else
    					   i++;
    				  
    
    				}
    				   return *(p[q-1]); // error C2100: illegal indirection
    // Here I just want to return or print last element of array. But where to return or how to print , not getting. 
    
    				return Brecurrsion( i++, arr ,int q);
    
     // error C2065: 'arr' : undeclared identifier
    // Here I want to call same function again, Recursive call. But not getting how to do it. //
     error C2062: type 'int' unexpected 
     }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    3
    You can't call a function like this in C.

    Code:
    int Brecurrsion(int i,&arr[0],10); // error C2059: syntax error : '&'
    What is int i?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    if p is a pointer to integer
    int *p;
    you can access some other integer in memory relative to the address stored in p

    /* dereferencing notation */
    *(p+17); /* offset the address and look up the integer there */

    or

    /* array notation */
    p[17]; /* this does the same work as above but looks different in your source */


    By combining the notations
    *(p[q-1])
    p[q-1] /* is an integer */
    *(p[q-1]) /* dereferencing an integer is illegal */


    Code:
    int Brecurrsion(int r, int *p, int q) {
      int i,j=0; int temp=0;
      if(i=q-1 && (p[q-1]> p[q-2]))	/* think about the i= assignment of this condition */
        printf("%d",p[q-1]);
      else
        for(i=0;i<=q-2;  ++i   /* i always increments.  move it into the for loop control**************** */ ) {
          if(p[i]>p[i+1]) {
    	temp= *p;
    	*p= *(p+1);
    	*(p+1)=*p; /* you probably intended *(p+1) = temp; **************** */
          }
        }
      fprintf(stderr,"in Brecurrsion the last element of \"p\" is %d\n",p[q-1]); /* **************** */
    
      /* Here you do not want recursion because you provided no way to terminate the recursion *****************/
      return 666;  /* Brecurrsion( i++, arr ,int q);****************/
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0
    Code:
    #include "stdafx.h"
    #include<stdio.h>
    #include<conio.h>
    #include <stdlib.h>
    
    int Brecurrsion(int, int *,int);
    int main()
    {
    	int i=0;int j=0;
    	int arr[10];
    	
    	printf("\n Enter the no of elements of Array ");
    	for(i=0;i<=9;i++)
    	{
            scanf("%d",&arr[i]);
    	}
        int Brecurrsion(j,arr,10); //  error C2078: too many initializers[U]
    
    	_getch();
    	return 0;
    }
      int Brecurrsion(int r,int *p,int q)
     {
    			int a=0,i=0; int temp=0;
    			
    			if(r=q && (p[0]> p[1]))
    				exit(1);            
    			else
    				for(a=0;a<=q-2;)
    				{
    
    					if(p[a]>p[a+1])
    						{
    							temp= *p;
    							*p= *(p+1);
    							*(p+1)=temp;
    							 a++;
    						}
    				   else
    					   a++;
    				  
    
    				}
    					 return Brecurrsion(r++,p,q);
    					return  printf("%d",p[i++]);
     }
    I am getting the error "C2078: too many initializers"
    I have done some changes. Once the Anchor step occurs I m forcefully coming out of the recursion by exit(1).
    But I have doubt whether I will able to print array element through last sentence or not i.e.
    return printf("%d",p[i++]);
    if not plz tell me how to print sorted array element.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    I recommend that you study c syntax and then restart your programming adventure with "hello world", followed by any short program in which you write a function, and use it. Following that, never think about bubble sort again. It's retarded.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    Studying indentation guidelines as well would be a good idea.

    The code posted so far by the OP is an unreadable mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo