Question:

How can we use pointers in the following sorting program? ?

by  |  earlier

0 LIKES UnLike

And please tell me the benefit as well

#include <iostream.h>

void sort(void);

int n[10];

main()

{

for (int i=0;i<=9;i++)

{

cout<<"Enter value "<<(i+1)<<":";

cin>>n[i];

}

cout<<"\nVALUES YOU ENTERED:\n\n";

for(int j=0;j<=9;j++)

cout<<n[j]<<"\n";

//sorting

sort();

cout<<"\nSORTED LIST:\n ";

for (int k=0;k<=9;k++)

cout<<n[k]<<endl;

system("pause");

}

void sort(void)

{

int temp;

for (int i=0;i<=9;i++)

{

for(int j=0;j<=9;j++)

{

if (i==j)

{

continue;

}

else if (n[i]>n[j])

{

temp = n[i];

n[i] = n[j];

n[j] = temp;

}

}

}

}

 Tags:

   Report

1 ANSWERS


  1. pass a pointer to array n to your sort() function. Whats the benefit? what if you used the sort() function for other arrays somewhere else in your program? without using pointers you would either need another sort() function or you would need to copy the arrays to array n first, then when done sorting, copy them back.

    Just another note, I see you use local variables i,j,k in your main. you could just use one of them for all of your for loops and use less variables.  

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.