简体   繁体   中英

Indexes to pointers in C++

I have a program that works using indexes. However, we are supposed to change it to pointers for the assignment.

In the code that I have attached down below where I try to switch it to pointers, I take the values anywhere throughout the program that say index and change them to pointer. That is at least where my thinking is. Here is the code using indexes:

#include <cstdlib>
#include <iostream>
#include <iomanip>

/*
  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";

    system("PAUSE");
    return EXIT_SUCCESS;
}

// 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;
         }
     }
}

This is the same code that I am trying to change to use pointers but keep getting errors. Can someone explain to me what i am doing wrong?

#include <cstdlib>
#include <iostream>
#include <iomanip>

/*
  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_pointer (int start_pointer, 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_ptr
        , *start_pointer;  // current starting spot for search
    int *small_ptr
        , *small_pointer;  // index of the smallest number in the array
    int * mover;
    int *size;


    start_ptr = 0;

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

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

    cout << "\n\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}

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

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

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

     swapper = numbers [mover1];
     numbers [mover1] = numbers [mover2];
     numbers [mover2] = 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
         mover;    // index of current number being printed

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

In your while loop

while (start_ptr < size - 1)

you are comparing the pointer to size. You should be comparing the pointee. So,

*start_ptr < size - 1

Same thing for a few lines before that when you set it to 0.

*start_ptr = 0

Be sure you know whether you want to do something to the pointer or its pointee.

You have two different things called size . The second should be named something else, eg, end , as a matter of good taste and maintainability. More importantly, it should be initialized: int *end = numbers + size . Then the statement while (start_ptr < end - 1) will behave sensibly.

In C++11, this variable isn't even necessary, since you can write:

#include <iterator>
⋮
    while (start_ptr < std::end(numbers) - 1)
    ⋮

Also note that swapValues is unnecessary. You can write:

#include <utility>
⋮
    std::swap(numbers[small_index], numbers[start_index]);

Or, in the pointer version (in which the call to swapValues is actually wrong, since you're passing pointers as integers):

    std::swap(*small_pointer, *start_pointer);

There are several more problems with the code, but I've run out of time. Sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM