Sunday, August 2, 2009

C++ help, ten points?

#define size 7


void sortArray (int numbers[]);


int indexMin (int numbers[], int low, int high);


void swap (int numbers[], int loc1, int loc2);


void getArray (int numbers[]);


void displayArray (int numbers[]);


int main()


{


int numbers[size];


getArray(numbers);


sortArray(numbers);


displayArray(numbers);


}


void getArray (int numbers[])


{


int i;


for (i=0; i%26lt;size; i++)


{


printf("\Enter next integer? ");


numbers[i]=GetInteger();


}


}


void displayArray (int numbers[])


{


int i;


printf("\n The sorted list is \n");


for (i=0; i%26lt;size; i++)


{


printf(" %d ", numbers[i]);


}


}


void sortArray (int numbers[])


{


int i, minInd;


for (i=0; i%26lt;size; i++)


{


minInd = indexMin (numbers, i, size-1);


swap (numbers, i, minInd);


}


}


int indexMin (int numbers[], int low, int high)


{


int i, minInd;


minInd=low;


for (i=low;i%26lt;=high;i++)


{


if (numbers[i] %26gt; numbers[minInd]) minInd=i;


}


return (minInd);


}


void swap (int numbers[], int loc1, int loc2)


{


int temp;


temp = numbers[loc1];


numbers[loc1]=numbers[loc2];


numbers[loc

C++ help, ten points?
Just don't print the duplicates at the end. This is easy, since the list is sorted.





Here's a modification of your code. The modified lines are indicated with comments.





void displayArray (int numbers[])


{


int i;


int last; // ***** The last number printed.


printf("\n The sorted list is \n");


for (i=0; i%26lt;size; i++)


{


if (i == 0 || numbers[i] != last) { // ***** Suppress duplicates.


printf(" %d ", numbers[i]);


last = numbers[i]; // ***** Save last number printed.


} // ***** End of if.


}


}





Hope this helps!
Reply:i'll get u the algorithm 4 selection sort .. hope u convert it into the program....


step 1: i=0;


step 2 : repeat steps 3-9 while(i%26lt;n-1)


step 3 : small=a[i];


step 4: loc=i; j=i+1;


step 5 : repeat steps 6,7 while(j%26lt;n)


step 6: if(small%26gt;a[j]) do steps 6.1 and 6.2;


step 6.1 : small=a[j];


step 6.2 : loc=j;


step 7 J++


step 8 interchange a[i] and a[loc]


step 9 j++


step 10 stop
Reply:In the function displayArray (int [ ]), try replacing what's inside the for loop with the following:


for (i=0; i%26lt;size; i++)


{


if ((numbers[i] != numbers[i+1]) || (i == (size - 1)))


printf(" %d ", numbers[i]);


else


continue;


}





I hope this helps you.


No comments:

Post a Comment