#File i/o selection sort need help debugging

137 messages · Page 1 of 1 (latest)

ripe tuskBOT
#

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.

sick dust
#

the Ary was originally not a void constructor but compiler wants it to be

#

File i/o selection sort need help debugging

pseudo briar
#

Do you know what a constructor is?

sick dust
#

i have the rough idea of it

#

i think

#

unless im doing it wrong

pseudo briar
#

Explain it, so I can make sure

#

in your own words, dw

sick dust
#

has a name that takes in the parameters given when the constructor is called to do a given function that was defined

calm nest
sick dust
#

that i dont know

pseudo briar
#

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

sick dust
#

the void part was what the terminal compiler wanted it to be

pseudo briar
#

The constructor has to have a matching name to the class. If you use other variable names then they will require a return type.

sick dust
#

so i should rename Ary to sort?

pseudo briar
#

Yea, and go through the learncpp.com website. It will teach you a lot of useful things that will make your assignment easier

sick dust
#

can i call another constructor in a constructor?

pseudo briar
#

Why would you want to do that?

#

for this assignment

sick dust
#

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

pseudo briar
#

I just noticed why do countData, loadData, and SelectionSort not have return types

sick dust
#

i will say im confused with given assignment

#

because idk if they are meant to be in the class or in main itself

pseudo briar
#

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.

sick dust
#
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

pseudo briar
#

I see, there are no return types in the instructions, so you'll have to decide on your own what they should be

sick dust
#

so follow what my code has for contData?

#

because somewhere in main im making a dynamic array in there

pseudo briar
#

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.

sick dust
#

so i would call it sort* Ary= new dataAry[count] or do i make Ary the same as dataAry

pseudo briar
#

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

sick dust
#

i feel so lost

pseudo briar
#

I think an example can help you understand this a bit more.
To represent a date what variables would you need?

sick dust
#

int month
int day
int year

pseudo briar
#

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)

sick dust
#

data gets used in the countData and loadData

pseudo briar
#

oh okay, then it's required

sick dust
#

what do i do with the data variable

#

because on paper the sort class only lists
int count
int* dataAry

pseudo briar
#

Then it's not required lol

#

You'll have to change the code in countData and loadData to make use of dataAry instead

sick dust
#

prof lists the step as step 2:data<- read an integer at a time from ifile //ie ifile>>data

pseudo briar
#

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<<" ";
}
sick dust
#

no index for the array?

pseudo briar
#

There's actually a couple of things.

  • dataAry is a pointer, so you're printing a pointer instead of the value.
  • When you pass in a c-style array to a function it loses it's size, so you'll have to pass in an int size along with the array.
sick dust
#

so take out the asterisk?

pseudo briar
#

no

#

The pointer is pointing to the first element in your array

sick dust
#

in clude a way of navigating through the point

pseudo briar
#

If you dereference it, you can get the value of the first element

sick dust
#

sothe variable in command becomes &dataAry

pseudo briar
#

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);
}
minor estuaryBOT
#
Program Output
0x7ffdd52cdfc0
pseudo briar
#

;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);
}
minor estuaryBOT
#
Program Output
12345
sick dust
#

so it is *dataAry insteadof dataAry for variable

pseudo briar
#

Yea, I'm not saying you should change the type for dataAry

sick dust
#

got u

pseudo briar
# minor estuary

I messed up this part by printing *a (which printed 11111) instead of a[i] lol

sick dust
#

i dont need to add count variable to the parameter because the class already has it?

pseudo briar
#

The teacher is really making this confusing for you guys

sick dust
#

grading policy is strict

#

and no late submission so yea

pseudo briar
#

There are lots of horrible coding teachers and I think this one is no different

sick dust
#

would i put the for loop in main?

pseudo briar
#

which for loop?

sick dust
#

to go through the array to print like urs

pseudo briar
#

You probably wouldn't want to. Putting it inside the printDataAry member function would make a lot more sense.

sick dust
#

so confusing

pseudo briar
#

Well, the name of the member function is short for print the array of data.

sick dust
#

yea

pseudo briar
#

If you can call it in main function with only one line, it's convenient also

sick dust
#

but they way prodessor list these are confusing me

pseudo briar
#

Alright, I have to go. Good luck with your assignment!

sick dust
#

ok

pseudo briar
#

I'm sure more people will help eventually you if you ask another question you're confused about

sick dust
#
        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 ()”```

ripe tuskBOT
#

@sick dust

Please Do Not Delete Posts!

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.

sick dust
#

why is my sort class expecting 2 arguements with 0 provided

pseudo briar
#

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

sick dust
#

well go past that now

#

just need to know why main saying the constructors are not declared now

pseudo briar
#

I think it has to do with this

sort* dataAry=new sort[count];
sick dust
#

i see

pseudo briar
#

I think you can create the object normally, since from what I remember the instructions said to create the dataAry in the heap

sick dust
#

the heap i am assuming is in main

pseudo briar
#

The heap memory is like when you use new and you have a pointer pointing to it

sick dust
#

so i just got to fix that one line

pseudo briar
#

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

pseudo briar
#

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 = &num;
//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
sick dust
#

how does this transfer to an array

#

would it be a double asterisl?

#

after the first int?

pseudo briar
#

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

sick dust
#

i keep thinking i got to pointer to the class

#

i see

#

but what would a (class)** (var)= new (class)*[index] mean

pseudo briar
sick dust
#

oh

#

so the object in question would be containing count and dataAry in it?

pseudo briar
pseudo briar
sick dust
#

i will post this example because im trying to use it as reference:

later in code
Person* p= new Person(Tname,Tage);```
pseudo briar
#

You can ask in #cpp-help-text

sick dust
#

that was the reference

pseudo briar
#

There are people that are more knowledgeable about this type of stuff there

sick dust
#

ok

#

fml

pseudo briar
#

Maybe mark this one as solved and create a new #1013107104678162544 so more people can see the new one.

sick dust
#

nvm i think i did it

pseudo briar
#

My fault for the #cpp-help-text recommendation. I should have known it was for specific questions.

sick dust
#

but constructors are still not declared inmain

#

just need to now figure out why constructors being undefined and why line 70 is not doing

pseudo briar
sick dust
#

ok

#

!solved