Vectors, linear search, bubblesorting program

Alright, i am not quite sure how to manage this but i am kinda stuck to get this program to work. so i am looking for comments on it, its probebly aloth of errors and faults inside this. but i appriciate every comment i can get on this to improve myself.


my task is to make a class that holds a name and a age of a person.

make a function that writes that information out.

make a function that can linear search in a vector with a person object after the age.

make a function that bubblesort in the person vector after the age of the person.

And finaly test my class by making a family "Person familj[4];"
with 4 persons with diffrent names and ages and the program should sort
the people by there age and last call the linear function to search for a family members age.

i feel kinda lost this is how my code looks so far



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
82
#include <iostream>
#include <string>
using namespace std;

class person     //<---- class that contains name and age of a person
{
public:
	string namn;
	int alder = 0;
	static void skrivut(string namn, int alder);
	person(string _name, int _age);

};

void person::skrivut(string namn, int alder) { //<---- writes out name and age

	cout << "Ålder: " << alder << "Namn: " << namn << endl;

}

person::person(string _name, int _age) {  //<--- constructor
	namn = _name;
	alder = _age;

}

int linsok(person person[], int n, int a) { //<<----- Linear searcher
	for (int i = 0; i < n; i++)
	{
		if (a == person[i]) //<--- error
		{
			return i;
		}
	}
	return -1;
}

void bubblesort(person person[], int n) {   //Bubblesorting the persons after age? how to do it?
		bool not_sorted = true;
		int j = 1, tmp;

		while (not_sorted) {
			not_sorted = false;
			j++;
			for (int i = 0; i < n - j; i++) {
				if (person[i] > person[i + 1]) {  //<--- error ">"
					tmp = person[i];  //<--- error
					person[i] = person[i + 1];
					person[i + 1] = tmp;   //<--- error operand types are person = int
					not_sorted = true;

				}//end of if
			}//end of for loop
		}//end of while loop



}


int main() {
	int person[6] = { 4, 234, 67, 23, 69, 34 };

	int uservalue;

	cout << "Skriv in åldern du letar efter: " << endl;
	cin >> uservalue;

	int result = linsok(person, 4, uservalue);   //<<----- error 

	if (result >= 0) {
		cout << "Åldern " << person[result] << " was found at the element with index " << result << endl;
	}
	else {
		cout << "the number " << uservalue << " was not found. " << endl;
	}

	person familj[4];     //<<<---- how do decorate a vector familj with 4 persons inside?



}
you need to access to the object field with . (dot).

line 46 for instance would be : if(person[i].age > person[i + 1].age)

line 62 confusing your array name is person like the class , but it has integers ????

line 30 you try to compare two objects (not pointers in this case where it would have work) , you need to implement the operator == in your structure.

line 9 , might be legal now , but initialize it in the constructor initializer list , instead.

Prefer make the two function as method member like linearSearch() and bubbleSort(), use good name for parameter -> "n" and "a" means nothing for anyone else , you might lose points for that.

line 78 are you sure you array is initialized ? Use aggregation for this case.

line 79 , call the functions and verify the values.

Person instead of person , use capital for classes.

With that it should be good.
Thank you for helping me

Accessing like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void bubblesort(person person[].alder, int n) {   //Bubblesorting the persons after age? how to do it?
		bool not_sorted = true;
		int j = 1, tmp;

		while (not_sorted) {
			not_sorted = false;
			j++;
			for (int i = 0; i < person[].size - j; i++) {  // <-- error person[].size - j
				if (person[i].alder > person[i + 1].alder) {
					tmp = person[i].alder;
					person[i].alder = person[i + 1].alder;
					person[i + 1].alder = tmp;  
					not_sorted = true;

				}//end of if
			}//end of for loop
		}//end of while loop
}


 
void bubblesort(person person[].alder, int n) {   //i get an error here at the DOT "Expected a )" 




 
for (int i = 0; i < person[].size - j; i++) { //also an error here "expected an expression" 



line 62 you can ignore i removed it

line 30 you need to implement the operator == in your structure. (not quite sure what you mean?)

line 9 how do you mean? i get errors if i try to put "alder = 0" inside the constructor


make them method members? to the class?

Is this how i make the linear search a member of the class?
1
2
3
4
5
6
7
public:
	string namn;
	int alder = 0;
	static void skrivut(string namn, int alder);
	person(string _name, int _age);
	int linsok(Person person[], int n, int a);   //<----- add this inside class?
};



and line 78

 
Person familj[4]{"olle", 5, "mia", 12 , "rolf", 38, "lina", 29};     //<<<---- how do decorate a vector familj with 4 persons inside? 

How do i do this correctly?



bubblesort parameter , only
(person persons[] , int n )


for ( ... ) person structure does not have the field size , you can make it and set it but prefer using n.

alder = 0; initialization not in the declaration , except for const static member

method of the class -> encapsulation (protect your stuff)

so linksok yes like that but add also the code { }

Person family[4] = {{"olle", 5},{"mia", 12},{"rolf", 38},{"lina", 29}};
I still cant get the program to run, this is how it looks at this moment, any suggestions what to do to fix it? :)


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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
using namespace std;

class Person     //<---- class that contains name and age of a person
{
public:
	string namn;
	int alder = 0;
	static void skrivut(string namn, int alder);

	void person(string _name, int _age)  //<--- constructor
	{ 
		namn = _name;
		alder = _age;

	}


	int linsok(Person person[], int n, int a) //<<----- Linear searcher
	{ 
		for (int i = 0; i < n; i++)
		{
			if (a == person[i]) //<--- error   how do i fix this?
			{
				return i;
			}
		}
		return -1;
	}

	void skrivut(string namn, int alder){

	cout << "Ålder: " << alder << "Namn: " << namn << endl;

	}




	void bubblesort(Person person[], int n) {   //Bubblesorting the persons after age?
		bool not_sorted = true;
		int j = 1, tmp;

		while (not_sorted) {
			not_sorted = false;
			j++;
			for (int i = 0; i < n - j; i++) { 
				if (person[i].alder > person[i + 1].alder) {
					tmp = person[i].alder;
					person[i].alder = person[i + 1].alder;
					person[i + 1].alder = tmp;
					not_sorted = true;

				}//end of if
			}//end of for loop
		}//end of while loop



	}




};





int main() {
	int uservalue;

	cout << "Skriv in åldern du letar efter: " << endl;
	cin >> uservalue;

	Person pob;
	int result = pob.linsok;

	if (result >= 0) {
		cout << "Åldern " << uservalue << " was found " << result << endl;
	}
	else {
		cout << "Åldern " << uservalue << " was not found. " << endl;
	}

	Person family[4] = { { "olle", 5 }, { "mia", 12 }, { "rolf", 38 }, { "lina", 29 }//<<<---- how do decorate a vector familj with 4 persons inside?
};     



}





This is not working i get the following error - no person constructer match the list.

 
Person family[4] = {{"olle", 5},{"mia", 12},{"rolf", 38},{"lina", 29}};

Last edited on
Anyone have a suggestion to fix?
Topic archived. No new replies allowed.