![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
mergesort program
Hello all,
My program is supposed to read in ints from a file, store in an array, then sort them with mergesort. I've went and found a mergesort function, retooled it to my needs, and combined it with an older program of mine that reads in from a file into an array. I get these 2 errors: error C2664: 'sorter::sorter(int *)' : cannot convert parameter 1 from 'const char [10]' to 'int *' error C2511: 'sorter::sorter(char *)' : overloaded member function not found in 'sorter' It seems to be an issue with my header, but just in case it isn't, i'll post my all of my program. Thanks in advance for your help!!!! Header: #ifndef SORTER_H
#define SORTER_H
class sorter {
public:
sorter(int* intFile); // Constructor reads in the grid from the infile
//Precondition: gridFile must be a pointer to a string of characters naming an input file
//Postcondtion: The input file has been opened and contents stored in a newly // allocated array based on size from the file
void mergeSort();
void m_sort(int numbers[], int temp[], int left, int right);
void merge(int numbers[], int temp[], int left, int mid, int right);
void display();
private:
int *intArr;
int size;
int *numbers;
int *tempArr;
};
#endifImplementation: //CECS 302 - CODY SLEDGE
#include "sorter.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
using namespace std;
sorter::sorter(char *intFile)
{
//Precondition: intFile must be a pointer to a string of characters naming an input file
//Postcondtion: The input file has been opened and contents stored in a newly allocated array based
// on size from the file
ifstream infile;
infile.open(intFile);
assert(infile);
infile >> size; //gets size of int array
//Allocate space for new array based on input from file
intArr = new int[size];
nums = new int[size];
tempArr = new int[size];
// Write data from file into new array
for(int i=0; i < size; i++)
{
int val;
infile >> val;
intArr[i] = val;
}
infile.close(); // Closes the opened file
return; // Constructor doens't return a value
}
void sorter::mergeSort()
{
m_sort(this->intArr, this->tempArr, 0, this->size - 1);
}
void sorter::m_sort(int intArr[], int tempArr[], int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort(intArr, tempArr, left, mid);
m_sort(intArr, tempArr, mid+1, right);
merge(intArr, tempArr, left, mid+1, right);
}
}
void sorter::merge(int intArr[], int tempArr[], int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
while ((left <= left_end) && (mid <= right))
{
if (intArr[left] <= intArr[mid])
{
tempArr[tmp_pos] = intArr[left];
tmp_pos = tmp_pos + 1;
left = left +1;
}
else
{
tempArr[tmp_pos] = intArr[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
while (left <= left_end)
{
tempArr[tmp_pos] = intArr[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
tempArr[tmp_pos] = intArr[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}
for (i=0; i <= num_elements; i++)
{
intArr[right] = tempArr[right];
right = right - 1;
}
}
void sorter::display()
{
for(int i=0; i< this->size; i++)
{
cout << intArr[i] << endl;
}
}Driver: //CECS 302 - CODY SLEDGE
#include "sorter.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
using namespace std;
int main() {
sorter s("input.txt");
s.mergeSort();
cout<< endl;
s.display();
cout << endl;
system("PAUSE");
} |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>cannot convert parameter 1 from 'const char [10]' to 'int *'
My husband is neither a programmer nor computer literate, and he didn't have any trouble figuring out that you're "using things that don't fit together". What's your excuse?
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
ummm.... ok.
So can anyone tell me what's being converted to *int from char? I'm using the exact same process for reading from a file as I did in the original program this part of code comes from, so I'm not sure what's going wrong. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
Your header has
sorter(int* intFile); // Constructor reads in the grid from the infile |
|
|
|
|
|
#5 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
ha.. didn't think to look at the prototype.... what a night...
Dark, you remain a personal savior... bless you, dear sir. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 6:45 PM |
| Creating a program to test a program | sixstringartist | C | 8 | Jan 21st, 2006 2:15 PM |
| move program console window back | badbasser98 | C++ | 21 | Oct 18th, 2005 3:02 PM |
| auto run hidden program. | kuroko | C++ | 9 | Aug 6th, 2005 11:05 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 5:12 PM |