Pointers

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:52 PM GMT

Question

Hi Zlatko, I am trying to output ASCII chars from a to z but my function crashed. Pls advise me further. Tks.

Code:

  1. include <iostream>
  2. include <ctime>
  3. include <cstring>
  4. include <cstdlib>

using namespace std;

const int MAX = 10;

void outputChar (char*, int);


int main () {

char* a; int size = MAX; srand (time(NULL));

outputChar (a, 10);

}


//Output characters void outputChar (char* a, int size) {

for (int i = 0; i < size; i++) { *a = static_cast <char> (rand () % 26 + 97); //Displaying ASCII char for lower case a to z. cout << &a [i] << "\t"; }

cout << endl;

}


Answer

Hello Steve.

Your program is crashing because you are trying to put a byte into a location pointed at by 'a', but the 'a' pointer has not been initialized to point to a valid piece of memory.

In main, you need char x[MAX]; char* a = x;

or char a[MAX];

or char* a = new char[MAX];

Any one of those will set aside MAX bytes in memory and set a to point to it.

In your for loop you want a[i] = static_cast .... not

  • a = static_cast ....

because *a always refers to the start of the array

and you want cout << a[i] not cout << &a[i] because &a[i] gives the address of the i'th element.

OK?


Question

QUESTION: I am trying to display an array of digits using rand. My final result need to be: 1) Output all digits <=40 to the left side of array 2) Output digits > 40 to the right side of array Prob: The random digits does not change & result incorrect

  1. include <iostream>
  2. include <cstdlib>
  3. include <ctime>

using namespace std;

const int MAX = 10; typedef void* VoidPtr;

void constructArrayVP (VoidPtr [], int); void printArrayVP (VoidPtr [], int); void swapArray (VoidPtr [], int); void swap2Elements (VoidPtr&, VoidPtr&);

int main () { VoidPtr vp [MAX];

constructArrayVP (vp, MAX); printArrayVP (vp, MAX); swapArray (vp, MAX); printArrayVP (vp, MAX); }

void constructArrayVP (VoidPtr vp [], int size) { int *item;

for (int i = 0; i < size; i++) { item = new int; *item = rand () % 100; vp [i] = item; } }

void printArrayVP (VoidPtr vp [], int size) { int item;

for (int i = 0; i < size; i++) { item = *(static_cast <int *> (vp [i])); cout << item << "\t"; }

cout << endl; }

void swapArray (VoidPtr vp [], int size) { int left = 0; int right = size - 1;

do { if (left > 50) { if (right <= 50) swap2Elements (vp [left], vp [right]); else left++; } else right++; }while (left > right); }

void swap2Elements (VoidPtr& vp1, VoidPtr& vp2) { VoidPtr temp = vp1; vp1 = vp2; vp2 = temp; }


ANSWER: Steve, that is a good try. You have some confusion about when you need to allocate memory. You do not need any memory allocation in this program. Your memory allocation made things more confusing for you. I suggest that you look at how the program is set up, how vp is created, and passed around, but I suggest you don't look at swapArray. Try to make your own swapArray. Use pencil and paper to trace through your own algorithm. My solution will be there if you get stuck but if you want to be a programmer, you need to train your mind to think about algorithms.


  1. include <iostream>
  2. include <cstdlib>
  3. include <ctime>

using namespace std;

const int MAX = 10;

void constructArrayVP (int [], int); void printArrayVP (int [], int); void swapArray (int [], int); void swap2Elements (int&, int&); void checkArray(int[], int);

int main () {

   //You do not need to allocate memory for each integer. 
   //You just need an array of integers.
   int vp [MAX];
   //Provide a seed to the random number generator
   //so that the array is different on each run
   srand((unsigned int)time(NULL));
   // Here is a loop to do a large number of tests of the swapArray algorithm
   for(int testNumber = 0; testNumber < 1000; ++testNumber)
   {
       constructArrayVP (vp, MAX);
       printArrayVP (vp, MAX);
       swapArray (vp, MAX);
       printArrayVP (vp, MAX);
       checkArray(vp, MAX);
   }

}

/* Checks that the array was "sorted" correctly

The first part of the array must be <= 50. Once we see a number > 50, 
we know we are in the second part. In the second part all numbers must be
> 50 or there is an error and the program quits.
*/

void checkArray(int vp[], int size) {

   bool firstHalf = true;
   for(int ix = 0; ix < size; ++ix)
   {
       if (firstHalf)
       {
           if (vp[ix] > 50) firstHalf = false;
       }
       else
       {
           if (vp[ix] <= 50)
           {
               cout << "Error at line " << __LINE__ << endl;
               exit(0);
           }
       }
   }

}

void constructArrayVP (int vp [], int size) {

   for (int i = 0; i < size; i++)
   {
       vp [i] = rand () % 100;
   }

}

void printArrayVP (int vp [], int size) {

   for (int i = 0; i < size; i++)
   {
       cout << vp [i] << "\t";
   }
   cout << endl;

}

void swapArray (int vp [], int size) {

   int left = 0;
   int right = size - 1;
   while (left < right)
   {
       // Find the next element > 50, searching from the left
       while (left < right && vp[left] <= 50)
       {
           ++left;
       }
       // Find the next element <= 50 searching from the right
       while (right > left && vp[right] > 50)
       {
           --right;
       }
       // The above loops may have exited because left crossed right
       // Check here that infact the numbers need swapping
       // Notice that the comparison is opposite to what you want in the finished array
       if (vp[left] > 50 && vp[right] <= 50) 
       {
           swap(vp[left], vp[right]);
       }
       else
       {
           // Some error checking of the algorithm
           if (left < right)
           {
               cout << "Programming error at line " << __LINE__ << endl;
               exit(0);
           }
       }
   }

}

void swap2Elements (int& vp1, int& vp2) {

   int temp = vp1;
   vp1 = vp2;
   vp2 = temp;

}

---------- FOLLOW-UP ----------

QUESTION: Hi Zlatko,

I had re-write my code. My program crashed. The swapArray () function don't work. Also, I need to use pointers here and not by ref. Pls advise.

Code:

  1. include <iostream>
  2. include <cstdlib>
  3. include <ctime>

using namespace std;

const int MAX = 10;


void constructArray_1 (int*, int); void printArray (int*, int); void swap (int*, int*); void swapArray (int*, int*);

int main () {

	int a [MAX];

int size; int *first = &a[0]; int *last = &a[size - 1];


srand (time (NULL));

constructArray_1 (a, MAX); printArray (a, MAX); swapArray(&a[0], &a[size - 1]); swap (first, last); printArray (a, MAX);

}

void constructArray_1 (int *a, int size) { for (int i = 0; i < size; i++) { *a = rand () % 10; ++a; } }


void printArray (int *a, int size) { for (int i = 0; i < size; i++) { cout << *a << " "; ++a; }

cout << endl; }


void swap (int *first, int *last) { int tempvar1;


tempvar1 = *first;

  • first = *last;
  • last = tempvar1;

}


void swapArray (int *first, int *last) {

int *a; int size;

first = &a[0]; last = &a[size - 1];


if (first < last) { if (*first <= 50) first++; else if (*last >= 50) last--; else if ((*first > 50) && (*last <= 50)) { swap (first, last); first++; last--; } swapArray(first, last); } }


Answer

Hello Steve. I see you are using a recursive algorithm for swapArray. That's very good. Your code had a few small errors in it. My comments are in the code. I believe it is correct now.

  1. include <iostream>
  2. include <cstdlib>
  3. include <ctime>

using namespace std;

const int MAX = 10;


void constructArray_1 (int*, int); void printArray (int*, int); void swap (int*, int*); void swapArray (int*, int*);

int main () {

   int a [MAX];
   //int size; you forgot to initialize size, and you can just use MAX
   int *first = &a[0];
   int *last = &a[MAX - 1];
   
   srand ((unsigned int)time (NULL));
   constructArray_1 (a, MAX);
   printArray (a, MAX);
   swapArray(&a[0], &a[MAX - 1]); //using MAX, instead of size
   //swap (first, last); you probably don't want this
   printArray (a, MAX);

}

void constructArray_1 (int *a, int size) {

   for (int i = 0; i < size; i++)
   {
       *a = rand () % 100; // %100, not % 10
       ++a;
   }

}


void printArray (int *a, int size) {

   for (int i = 0; i < size; i++)
   {
       cout << *a << " ";
       ++a;
   }
   cout << endl;

}


void swap (int *first, int *last) {

   int tempvar1;


   tempvar1 = *first;
   *first = *last;
   *last = tempvar1;

}


void swapArray (int *first, int *last) {

   // The 4 removed lines must be left over from an old version
   //removed int *a;
   //removed int size;
   //removed first = &a[0];
   //removed last = &a[size - 1];


   if (first < last)
   {
       if (*first <= 50)
           first++;
       else if (*last >= 50)
           last--;
       else if ((*first > 50) && (*last <= 50))
       {
           swap (first, last);
           first++;
           last--;
       }
       swapArray(first, last);
   }

}

Advertisement

©2024 eLuminary LLC. All rights reserved.