Bubble sorting using pointers

Apr 7, 2013 at 6:45pm
I am haveing some trouble with my pointers in my bubble sorting.
this is a class project. I tried using & as well, but to no avail. Please help me solve this.

2 errors are:
error LNK1120:1 unsolved externals
ResistorMain.exe

error LNK2019: unresolved external symbol "void_cdecl bubbleSort(int*,int*)" ResistorMain.obj

Here is the code:

#include "Resistor.h" // random number generator for up to 10 values.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <random>
#include <windows.h>
#include <conio.h>
using namespace std;

//Sort the resistors in ascending order
void bubbleSort (int *, int *);

int main ()
{

int resistor = 0;

int *ptrResistor
ptrResistor = 0;
ptrResistor = new int;

int *ptrResistorArray;
ptrResistorArray = 0;
ptrResistorArray = new int[10];
Resistor resObj;

// set array elements to 0
for (int i=0; i <= 9; i++)
{
ptrResistorArray[i] = 0;
}

// User sets array size up to a Max of 10
do
{
cout << "How many Random Resistors do you want? (Max 10): ";
cin >> resistor;
cout << endl;
}while (resistor < 1 || resistor > 10);

ptrResistor = &resistor;

// output user array size value
cout << "You chose " << resistor << " Random Resistors\n";
cout << "Here are your Random Resistor Values." << endl;

// put random number generated into each array element
for (int i = 0; i < resistor; i++)
{
ptrResistorArray[i] = resObj.getRandomResistorValue();
cout << ptrResistorArray[i] << endl;
}
cout << endl << endl;

// Sort Array from smallest to biggest
// Call the function: bubbleSort
bubbleSort(ptrResistorArray, ptrResistor);
cout << "\nSorted resistors in ascending order: " << endl;
for (int i = 0; i < *ptrResistor; i++)
{
cout << ptrResistorArray[i] << endl;
}

// Free up memory
delete ptrResistor;
delete ptrResistorArray;

cout << endl;
system("pause");
}

//Function: Sort the resistors in ascending order
void bubbleSort (int *x[], int *y)
{
for (int i = 0 ; i < *y; i++)
{
for (int j = 0; j < *y-i-1; j++)
{
//Swap the resistors if the resistors are out of order
if (*x[j] > *x[j+1])
{
int temp = *x[j];
*x[j] = *x[j + 1];
*x[j + 1] = temp;
}
}
}
}
Apr 7, 2013 at 7:27pm
I found that if I remove the * for the x variables, it works until the program ends.
It is giving me an error called:
Debug Assertion failed!
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlocksUse)

Help with this please.
I will also continue debugging.


//Function: Sort the resistors in ascending order
void bubbleSort (int x[], int *y)
{
for (int i = 0 ; i < *y; i++)
{
for (int j = 0; j < *y-i-1; j++)
{
//Swap the resistors if the resistors are out of order
if (x[j] > x[j+1])
{
int temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
}
}
Apr 7, 2013 at 7:35pm
By commenting out:
delete ptrResistor;

to

//delete ptrResistor;

Solved the error.
It is working fine now.
Topic archived. No new replies allowed.