When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
137 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
the Ary was originally not a void constructor but compiler wants it to be
File i/o selection sort need help debugging
Do you know what a constructor is?
has a name that takes in the parameters given when the constructor is called to do a given function that was defined
what's the purpose of a constructor though? What makes it special, different than other class member functions?
that i dont know
It's a special member function that is automatically called when an object is created. It also doesn't have a return type and it's used most of the time to initialize variables. I think the void Ary you created is what you thought a constructor should be
the void part was what the terminal compiler wanted it to be
Here is a good resource to learn about constructors
https://www.learncpp.com/cpp-tutorial/introduction-to-constructors/
The constructor has to have a matching name to the class. If you use other variable names then they will require a return type.
so i should rename Ary to sort?
Yea, and go through the learncpp.com website. It will teach you a lot of useful things that will make your assignment easier
can i call another constructor in a constructor?
the step by step the professor game me in selectionSort constructor printDataAry is in there
step 0: logFile “Entering selectionSort ()” step 1: i 0 step 2: minVal dataAry[i] minIndex i j i+1 step 3: if dataAry[j] < minVal minIndex j minVal dataAry[j] step 4: j++ step 5: repeat step 3 to step 4 while j < count step 6: if minIndex != i tmp dataAry [i] dataAry [i] dataAry [minIndex] dataAry [minIndex] tmp step 7: logFile “** Below is after” i “iteration of selection sort \n” // write i. step 8: printDataAry (dataAry, logFile) step 9: i++ step 10: repeat step 2 to step 9 while i < count-1 step 11: logFile “leaving selectionSort ()”
this is the algorithm given for selectionSort constructor
I just noticed why do countData, loadData, and SelectionSort not have return types
i will say im confused with given assignment
because idk if they are meant to be in the class or in main itself
lol, it's okay. Working with classes in the beginning is very confusing
It will finally click after some point
In main is where the execution of code is supposed to go.
logFile “In main () count = ” write count.
// i.e., logFile << “In main () count=” << count << \n
step 2: dataAry dynamically allocate, size of count. // i.e., dataAry = new int [count]
step 3: close inFile
step 4: inFile open input file from argv [1] // re-open inFile
step 5: loadData (inFile, dataAry, count, logFile)
step 6: outFile “** printing data of before sorting. **”
printDataAry (dataAry, outFile)
step 7: selectionSort (dataAry, count, logFile)
step 8: outFile “** Printing data after sorting. ***”
printDataAry (dataAry, outFile)
step 9: close all files```
this is the steps my professor wants me to do for main
so i was a bit confused about why the print in class and if others are to be out or not
I see, there are no return types in the instructions, so you'll have to decide on your own what they should be
so follow what my code has for contData?
because somewhere in main im making a dynamic array in there
That's fine. You should be making the dynamic array in main, so you can pass it as an argument for all the functions that need it.
so i would call it sort* Ary= new dataAry[count] or do i make Ary the same as dataAry
Oh okay, I see what you mean
The fact there is two Arys messed up.
The instructions are not clear too on whether the dynamic array variable is supposed to be part of sort or not
i feel so lost
I think an example can help you understand this a bit more.
To represent a date what variables would you need?
int month
int day
int year
Yes, so if you wanted to represent a couple of dates without classes you would do it like this.
int month1, month2, month3;
int day1, day2, day3;
int year1, year2, year3;
with classes you could just do
class Date{
public:
int month, day, year;
};
int main(){
Date date1;
Date date2;
Date date3;
}
It's much less repetitive code
To represent the sort it's harder to figure out than Date but I think you need the int count, the array dataAry and the int data (but I'm not sure about the int data)
data gets used in the countData and loadData
oh okay, then it's required
what do i do with the data variable
because on paper the sort class only lists
int count
int* dataAry
Then it's not required lol
You'll have to change the code in countData and loadData to make use of dataAry instead
prof lists the step as step 2:data<- read an integer at a time from ifile //ie ifile>>data
I have to go soon but before I do
There is a problem with this can you try to figure out what it is?
void printDataAry(int *dataAry,ofstream&ofile){//print command
ofile<<dataAry<<" ";
}
no index for the array?
There's actually a couple of things.
so take out the asterisk?
in clude a way of navigating through the point
If you dereference it, you can get the value of the first element
sothe variable in command becomes &dataAry
Here's the difference
;compile
#include <iostream>
void print(int* a){
std::cout << a;
}
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
print(arr);
}
0x7ffdd52cdfc0
;compile
#include <iostream>
void print(int* a, int size){
for(int i = 0; i < size; i++){
std::cout << a[i];
}
}
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int size = 5;
print(arr, 5);
}
12345
so it is *dataAry insteadof dataAry for variable
Yea, I'm not saying you should change the type for dataAry
got u
I messed up this part by printing *a (which printed 11111) instead of a[i] lol
i dont need to add count variable to the parameter because the class already has it?
Good question. The instructions said sort has int count and int* dataAry but the instructions also say to create a count variable in main function.
The teacher is really making this confusing for you guys
There are lots of horrible coding teachers and I think this one is no different
would i put the for loop in main?
which for loop?
to go through the array to print like urs
You probably wouldn't want to. Putting it inside the printDataAry member function would make a lot more sense.
so confusing
Well, the name of the member function is short for print the array of data.
yea
If you can call it in main function with only one line, it's convenient also
but they way prodessor list these are confusing me
Alright, I have to go. Good luck with your assignment!
ok
I'm sure more people will help eventually you if you ask another question you're confused about
ofile<<"Entering selectionSort()";
int i=0;
while(i<count-1){
int minVal=dataAry[i];
int minIndex=i;
int j=i+1;
while(j<count){
if(dataAry[j]<minVal){
minIndex=j;
minVal=dataAry[j];
}
j++;
}
if(minIndex!=i){
int tmp=dataAry[i];
dataAry[i]=dataAry[minIndex];
dataAry[minIndex]=tmp;
}
ofile<<"**Below is after "<<i<<" iteration of selection sort\n";
for(int index=0;index<i;index++){
dataAry[index]=printDataAry(ofile);
}
i++;
}
ofile<<"leaving selectionSort()";
}```
i would like to know if i coded sslectionSort correctly tothe given steps
selectionSort(dataAry,count,logFile)
step 0: logFile “Entering selectionSort ()”
step 1: i 0
step 2: minVal dataAry[i]
minIndex i
j i+1
step 3: if dataAry[j] < minVal
minIndex j
minVal dataAry[j]
step 4: j++
step 5: repeat step 3 to step 4 while j < count
step 6: if minIndex != i
tmp dataAry [i]
dataAry [i] dataAry [minIndex]
dataAry [minIndex] tmp
step 7: logFile “** Below is after” i “iteration of selection sort \n” // write i.
step 8: printDataAry (dataAry, logFile)
step 9: i++
step 10: repeat step 2 to step 9 while i < count-1
step 11: logFile “leaving selectionSort ()”```
@sick dust
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
When creating an object it has to match the constructors in the class
The sort constructor you've created in the class expects an int argument first and int* argument second
well go past that now
just need to know why main saying the constructors are not declared now
I think it has to do with this
sort* dataAry=new sort[count];
i see
I think you can create the object normally, since from what I remember the instructions said to create the dataAry in the heap
the heap i am assuming is in main
The heap memory is like when you use new and you have a pointer pointing to it
so i just got to fix that one line
When you have stuff on the heap memory you have to manually free it otherwise it can be bad. And that's what you do with the delete[] dataAry; line in your code
yea
Here's the difference between a normal pointer and an owning pointer which has stuff in the heap memory.
Normal pointer:
int num = 5;
int* pointer = #
//You're not responsible for deleting pointer variable
Owning pointer
int* pointer = new int;
*pointer = 4;
delete pointer;
//You're responsible for deleting pointer variable
how does this transfer to an array
would it be a double asterisl?
after the first int?
no
int *myArray = new int[10];
//have to use [] after delete for array
delete[] myArray;
the myArray is just a pointer and it's pointing to the memory in the heap
i keep thinking i got to pointer to the class
i see
but what would a (class)** (var)= new (class)*[index] mean
It's not a pointer to the class. Sort is the type which happens to be a class you created instead of like an int or double and you're basically making an object on the heap
I'm not too good with double pointers but I don't think you would want to do that.
yea, but it's not necessary. You'll be responsible for deleting it after using it. If you just made a normal object it would make more sense
i will post this example because im trying to use it as reference:
later in code
Person* p= new Person(Tname,Tage);```
You can ask in #cpp-help-text
that was the reference
There are people that are more knowledgeable about this type of stuff there
Maybe mark this one as solved and create a new #1013107104678162544 so more people can see the new one.
nvm i think i did it
My fault for the #cpp-help-text recommendation. I should have known it was for specific questions.
but constructors are still not declared inmain
just need to now figure out why constructors being undefined and why line 70 is not doing
There's a couple problems with this. I advise you do this since I gotta go workout