Question

In: Computer Science

Your job is to change the program so that it no longer usesindexes anywhere within...

Your job is to change the program so that it no longer uses indexes anywhere within the executable code but instead uses pointers into the array. Hint: This will be easy to think about if you change all indexes in the program to pointers. It might make sense if you also change their names. Thus: int start_index; would become: int *start_ptr; Make sure that your program will correctly sort any set of numbers. Pay particular attention to the boundary cases (start and end of the array).

SORT.CPP

#include#include#include/*
  Program sorts an array of integers using a selection sort.
  The general algorithm repeatedly finds the smallest number
  in the array and places it at the front of the list.
*/
using namespace std;

const int size = 20;

int find_small_index (int start_index, int numbers []);
void swap_values (int index1, int index2, int numbers []);
void print (int numbers []);

int main(int argc, char *argv[])
{
    // array of numbers
    int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
                       54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
    int start_index;  // current starting spot for search
    int small_index;  // index of the smallest number in the array

    start_index = 0;
    // continue finding the smallest value and placing it
    // at the front of the list
    while (start_index < size - 1)
    {
          small_index = find_small_index (start_index, numbers);
          swap_values (small_index, start_index, numbers);
          start_index++;
    }

    cout << "\n\nThe sorted array is:\n";
    print (numbers);

    cout << "\n\n";

    return 0;
}

// finds and returns the index of the smallest number remaining in
// the array
int find_small_index (int start_index, int numbers [])
{
    int small_index, // smallest index to be returned
        index;       // current index being viewed

    small_index = start_index;
    // look at each element
    for (index = start_index + 1; index < size; index++)
        // remember index of smaller value
        if (numbers [index] < numbers [small_index])
           small_index = index;
    return small_index;
}

// swap the values in the array at indexes index1 and index2
void swap_values (int index1, int index2, int numbers [])
{
     int swapper;

     swapper = numbers [index1];
     numbers [index1] = numbers [index2];
     numbers [index2] = swapper;
}

// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
     int on_line,  // number of values printed on the line
         index;    // index of current number being printed

     on_line = 0;
     // print each element in the array
     for (index = 0; index < size; index++)
     {
         cout << setw (5) << numbers [index];
         on_line++;
         // if 10 numbers have been printed on the line
         // go to next line
         if (on_line == 10)
         {
            cout << "\n";
            on_line = 0;
         }
     }
}

Solutions

Expert Solution

Program:

SELECTIONSORT.CPP
#include
#include
#include

/*
Program sorts an array of integers using a selection sort.
The general algorithm repeatedly finds the smallest number
in the array and places it at the front of the list.
*/
using namespace std;

const int size = 20;

int find_small_value (int *start_pointer, int numbers []);
void swap_values (int *const, int *const,int numbers[]);
void print (int numbers []);

int main(int argc, char *argv[])
{
// array of numbers
int numbers [size] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41,
54, 128, 62, 44, 12, 1023, 89, 905, 32, -12};
int *start_pointer; // current starting spot for search
int *small_pointer; // pointer of the smallest number in the array

*start_pointer = 0;
// continue finding the smallest value and placing it
// at the front of the list
while (*start_pointer < size - 1)
{
*small_pointer = find_small_value (*start_pointer, numbers);
swap_values (*small_pointer, *start_pointer, numbers);
*start_pointer++;
}

cout << "\n\nThe sorted array is:\n";
print (numbers);

cout << "\n\n";

return 0;
}

// finds and returns the index of the smallest number remaining in
// the array
int find_small_value (int *start_pointer, int numbers [])
{
int *small_pointer, // smallest index to be returned
index; // current index being viewed

*small_pointer = *start_pointer;
// look at each element
for (index = *start_pointer + 1; index < size; index++)
// remember index of smaller value
if (numbers [index] < numbers [*small_pointer])
*small_pointer = index;
return *small_pointer;
  
}

// swap the values in the array at indexes index1 and index2
void swap_values (int *const pointer_value1, int *const pointer_value2, int numbers [])
{
int swapper;

swapper = *pointer_value1;
*pointer_value1 = *pointer_value2;
*pointer_value1 = swapper;
}

// prints the array in nice format, 10 numbers per line
void print (int numbers [])
{
int on_line, // number of values printed on the line
index; // index of current number being printed

on_line = 0;
// print each element in the array
for (index = 0; index < size; index++)
{
cout << setw (5) << numbers [index];
on_line++;
// if 10 numbers have been printed on the line
// go to next line
if (on_line == 10)
{
cout << "\n";
on_line = 0;
}
}
}


Related Solutions

Is the accumulated cost of construction work in progress recorded as an asset anywhere within the...
Is the accumulated cost of construction work in progress recorded as an asset anywhere within the financial statements or notes? Which fund, or funds, account for cash received, or receivables created, from sales of general capital assets? Are the proceeds of sales of general capital assets reported as an other financing source or as revenue.
Is any job (at any wage) better than no job? Does your answer change if you...
Is any job (at any wage) better than no job? Does your answer change if you apply the question to a worker in a developing, rather than developed country?
2. Change all of the numbers in the data area of your worksheet so that it...
2. Change all of the numbers in the data area of your worksheet so that it looks like this: A B C 1 2 3 4 5 6 7 Chapter 5: Applying Excel Data Unit sales 30,000 units Selling price per unit $30 per unit Variable expenses per unit $18 per unit Fixed expenses $288,000 If your formulas are correct, you should get the correct answers to the following questions. (a) What is the break-even in dollar sales? (b) What...
2. Change all of the numbers in the data area of your worksheet so that it...
2. Change all of the numbers in the data area of your worksheet so that it looks like this: A B C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Chapter 4: Applying Excel Data Selling price per unit $334 Manufacturing costs: Variable per unit produced: Direct materials $135 Direct labor $54 Variable manufacturing overhead $26 Fixed manufacturing overhead per year $119,600 Selling and administrative expenses: Variable per unit sold...
2. Change all of the numbers in the data area of your worksheet so that it...
2. Change all of the numbers in the data area of your worksheet so that it looks like this: If your formulas are correct, you should get the correct answers to the following questions. A B C D 1 Chapter 5: Applying Excel 2 3 Data 4 Unit sales 80,000 units 5 Selling price per unit $20 per unit 6 Variable expenses per unit $14 per unit 7 Fixed expenses $384,000 8 (a) What is the break-even in dollar sales?...
2. Change all of the numbers in the data area of your worksheet so that it...
2. Change all of the numbers in the data area of your worksheet so that it looks like this: A B C D 1 Chapter 3: Applying Excel 2 3 Data 4 Unit sales 30,000 units 5 Selling price per unit $50 per unit 6 Variable expenses per unit $20 per unit 7 Fixed expenses $720,000 8 If your formulas are correct, you should get the correct answers to the following questions. (a) What is the break-even in dollar sales?...
2. Change all of the numbers in the data area of your worksheet so that it...
2. Change all of the numbers in the data area of your worksheet so that it looks like this: 3 Data 4 Selling price per unit $374 5 Manufacturing costs: 6   Variable per unit produced: 7     Direct materials $152 8     Direct labor $58 9     Variable manufacturing overhead $38 10   Fixed manufacturing overhead per year $166,400 11 Selling and administrative expenses: 12   Variable per unit sold $4 13   Fixed per year $98,000 14 15 Year 1 Year 2 16 Units in...
2. Change all of the numbers in the data area of your worksheet so that it...
2. Change all of the numbers in the data area of your worksheet so that it looks like this: 3 Data 4 Selling price per unit $374 5 Manufacturing costs: 6   Variable per unit produced: 7     Direct materials $152 8     Direct labor $58 9     Variable manufacturing overhead $38 10   Fixed manufacturing overhead per year $166,400 11 Selling and administrative expenses: 12   Variable per unit sold $4 13   Fixed per year $98,000 14 15 Year 1 Year 2 16 Units in...
Think about yourself, your home, your car, your job and so on. What risks that you...
Think about yourself, your home, your car, your job and so on. What risks that you face are covered by insurance? More importantly, what risks are NOT insured against? Consider these risks and what extensions to your insurance cover you might contemplate? Recent events in the financial sector have shown that risk perception plays a critical role in effective risk management. How much influence do you think any one individual should have alone on key risk management decisions eg traders,...
Think about yourself, your home, your car, your job and so on. What risks that you...
Think about yourself, your home, your car, your job and so on. What risks that you face are covered by insurance? More importantly, what risks are NOT insured against? Consider these risks and what extensions to your insurance cover you might contemplate? Recent events in the financial sector have shown that risk perception plays a critical role in effective risk management. How much influence do you think any one individual should have alone on key risk management decisions eg traders,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT