Heroes of C++ assemble!: Questions about Classes

Pages: 12
thanks for all your help but I think you'll have to dumb it down even further!

not sure what you mean, int comes from sorting the age but I even if I did auto wouldn't it just use int for the age anyway?

if my function sorts 21 as the first number I also want the string name that's assigned in the same index to get sorted, but if I sort my string (even if I could) how would it know that e.g "karolina" would be the least valued string.

so either I have to assign the string numeral values that's equal to the age somehow or they have to follow the age that is part of the index in the array
int comes from sorting the age
Byt you are not sorting ages. You are sorting by ages, but objects you are sorting are Persons. You do not want Johan suddenly becoming 21 y/o and karolina 25 y/o, do you? So you need to swap whole persons. So you just need
1) To determine if Persons need swapping. This is done by checking their ages:
if (persArray[j].age<persArray[j-1].age){ 
2) To actually swap Persons. Not their ages, not their names, not both at the same time. Do the same thing you did with ints, but with Person:
1
2
3
Person temp = persArray[j];
persArray[j] =persArray[j-1];
persArray[j-1]=temp;


Bonus: there is swap routine in standard library. Use it instead of writing your own each time:
1
2
3
4
5
6
7
8
9
10
void SortArray(Person* persArray, int n)
{ 
    using std::swap;
    for(int i = 0; i < n; ++i) {
        int nrLeft = n - i;
        for(int j = 0; j < nrLeft; ++j)
             if (persArray[j].age < persArray[j-1].age)
                 swap(persArray[j], persArray[j-1])
    }
}
Last edited on
oh thank you!... I'm so new the answer was really staring me in the face but I need more routine to spot my faults.

I tried "Person temp" earlier but it wouldn't work:
 In function 'int SortArray(Person*, int)':
48:45: error: conversion from 'int' to non-scalar type 'Person' requested
50:44: error: expected primary-expression before 'temp'


But of course there's no reason to swap .age only using .age as the condition and that's why Persons temp wouldn't work because I was swapping ages in int and nothing else.

It makes perfect sense when you point it out but I was blind to it otherwise
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int SortArray(Person* persArray, int n){ 
    for(int i =0 ;i<n;i++){
        int nrLeft=n-i;
    
         for(int j = 0 ;j<nrLeft;j++){
            
             if (persArray[j].age<persArray[j-1].age){
                 Person temp = persArray[j];
                 persArray[j]=persArray[j-1];
                 persArray[j-1]= temp;
             }
         }
    }


the road to learning programming is long and hard...
This code works perfectly in C++ shell but for some reason when running it in code::blocks
everything crashes is it a fault in the code or my settings in codeblocks?

EDIT: if I remove any of the function calls or remove Print everything works could it be memory issue?
I also have a warning for an unused variable " int sort=SortArray(p,4);"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>


class Person{
    public:
        std::string name;
        int age;
        void Print();
        Person(std::string,int);
        std::string arName; 
        int arAge;
        Person();
        
};
//constructor
Person::Person(std::string keyName, int keyAge){
    name=keyName;
    age=keyAge;
};

Person::Person(){
    arName="Johan";
    arAge=23;
};

void Person::Print(){
        std::cout<<arName<<", "<<arAge<<"\n";
};


int LinearSearch(Person* persArray,int n,int key){
    for(int i = 0;i<n;i++){
        if(persArray[i].age==key){
            return i;
        }
    }
    return -1;
};

int SortArray(Person* persArray, int n){ 
    for(int i =0 ;i<n;i++){
        int nrLeft=n-i;
    
         for(int j = 0 ;j<nrLeft;j++){
            
             if (persArray[j].age<persArray[j-1].age){
                 Person temp = persArray[j];
                 persArray[j]=persArray[j-1];
                 persArray[j-1]= temp;
             }
         }
    }
    
    
    for(int i=0;i<n;i++){
        std::cout<<persArray[i].age<<", "<<persArray[i].name<<std::endl;
    }
     return 0;   
};
int main(){
    
    //Print
    Person pers;
    pers.Print();
    
    
    
    Person p[4] {{"karolina",21},{"Chuck Norris",9001},{"Bad Luck Brian",19},{"Johan",23}};
    int sort=SortArray(p,4);
    int index=LinearSearch(p,5,23);
   
    if(index==-1){
        std::cout<<"person not found";
    }   else{
            std::cout<<p[index].name<<" can be found on index "<<index<<std::endl;
    }

    
    return 0;
};
Last edited on
1) Your linear search call: int index=LinearSearch(p, 5, 23);
There is only 4 elements in the array.

2) Your sort function:
1
2
for(int j = 0 ;j<nrLeft;j++){
    if (persArray[j].age<persArray[j-1].age){
What do you think will happen when j is 0? Which elements are compared and then swapped?
originally I sorted the higher numbers first and forgot to change j! and 5 is just a typo I did see that but didn't edit it here thanks yet again!.... it will take alot more hours of programming before I'll start seeing these kinds of faults I think

all my issues should be solved now and I feel I've learnt a lot thanks everyone who contributed and special thanks to MiiNiPaa!
Topic archived. No new replies allowed.
Pages: 12