hetThe code is the executing fine,but i need to keep a pointer variable to return the last index of the array

//for eg if i enter n as 20,my lastindex is 19,the pointer will return the lastindex...

void sort(int a[],int *,int);
void swap(int a[],int *,int,int);
int main(void)
{
    int i,n,a[100],b[100];

    printf("\n Enter the number of items for A and B:\n",n);
        scanf("%d",&n);
    printf("\n Sorting is only possible if A and B are same\n");
    printf("\n Enter the elements of the array for A \n ");
    for(i = 0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\n Enter the elements of the array for B \n" );
    for(i=0; i<n; i++)
    {
        scanf("%d",&b[i]);
    }
    printf("\n Input Elements \n");
    for(i=0;i<n;i++)
        printf("\n%d %3d\n",a[i],b[i]);
        sort(a,b,n);
    printf("\nAfter sorting, A is the key:\n a: b:\n\n");
    for(i=0;i<n;i++)
    printf("\n%d%3d\n",a[i],b[i]);
    printf("\n");
return 0;

}

void sort(int a[], int *b, int n)
    {
     int i,j;
     for(i = 0;i < n-1; i++)
     {
            for(j = i+1;j<n;j++)
            {
         if((a[i]>a[j]))
         {
                    swap(a,b,i,j);

         }
         }
     }
 }
void swap(int a[], int *b, int i, int j)
{
    int temp;


    temp = a[i];
    a[i] = a[j];
    a[j] = temp;

//swap the b array
    temp = b[i];
    b[i] = b[j];
    b[j]= temp;
 }

Recommended Answers

All 6 Replies

I don't understand the requirement. n already has the number of items, so to get the last index all that's necessary is to subtract 1 from n. Theres no need for a pointer that I can see.

yes u r right,i need to maintain a variable which has to return the index of the lastelement in the array
if i enter n as 5
then the the variable has to return the lastindex i.e 4(0,1,2,3,(4<-last_index))

Then just store that value in another variable:

int last = n - 1;

Or if you insist on using an unnecessary level of indirection:

int last = n - 1;
int *plast = &last;

I think I understand what you want, I just don't understand the point of it.

i need to pass the variable inside the function as argument...

i need to pass the variable inside the function as argument...

Okay, then do it. You wrote the functions right? So you know how to define parameters and pass arguments, right? You know I'm not going to do it for you, right?

I know how to do it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.