Final assignment

Okay so, I kinda need help with this assignment. I don't know if you guys understand just by looking at the code what I try to accomplish but I'll try to explain this the best I can. So what i need to do Is to create a class(Person) and add the attributes "namn" and "alder" whch stores a name and an age.
Then I've created a function for a linear search through the vector p[].
Then I created a bubblesort function, as my assignment tells me to do, to sort
a vector from youngest to oldest by age.
And finally, inside the main function, create a vector(Person familj[4]) with 4 elements. Then the program should call the bubblesort function to the vector and print the vector. Then It should call the linsok function to do a linear search to the familj[] vector and print the index on the found person.



#include <iostream>

using namespace std;

int tot = 4;

class Person
{
public:

string namn;

int alder;

void SkrivUt()
{
cout<<namn<<endl;
cout<<alder<<endl;
}
};

int linsok(Person p[], int n, int a)
{
for(int i = 0; i < 5; i++)
{
if(p[i] == a)
return n;
else
return -1;
}
}

void bubblesort(Person p[], int n)
{
for(int i = 0; i < 5; i++)
{
int nrLeft = tot - 1;

for(int j = 0; j < nrLeft; j++)
{
if(p[j] > p[j] + 1)
{
int temp = p[j];
p[j] = p[j] + 1;
p[j+1] = temp;
}
}
}
for (int k = 0, k < 5, k++)
{
cout<<p[k]<<endl;
}
}

int main()
{
int a;
cout<<"Skriv åldern på den person du söker"<<endl;
cin>>a;

Person familj[4];
Person.familj[0] = {"Gustaf", 12};
Person.familj[1] = {"Lina", 14};
Person.familj[2] = {"Fredrik", 32};
Person.familj[3] = {"Anna", 33};
bubblesort(Person familj[], 4);
linsok(Person familj[], 4, a);

}


Thanks in advance, all help is appreciated :)


Hello elliotawerstedt,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can us the preview button at the bottom to see how it looks.

The first part that jumps out at me is your class. A little better way to write the class is:

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
class Person
{
public:
	Person();
	~Person();
	void Set(std::string namn, int alder);
	std::string GetNamn();
	int Getalder();
	void SkrivUt();

private:
	std::string m_namn;

	int m_alder;

};

Person::Person()
{
}

Person::~Person()
{
}

void Person::Set(std::string namn, int alder)
{
	m_namn = namn;
	m_alder = alder;
}

std::string Person::GetNamn() { return m_namn; }

int Person::Getalder() { return m_alder; }

void Person::SkrivUt()
{
	cout << m_namn << endl;
	cout << m_alder << endl;
}


You have your variables as "public" which defeats the the purpose of the class. They should be "private".

When describing the problem you talk about vectors, but the line Person familj[4]; only creates an array not a vector. If you want to use a vector it would defined as: std::vector<Person> familj; or you could use std::vector<Person> familj[4];, but it is not necesary with a vector. This will create a vector of classes of type Person where each element is a separate class object.

The line Person.familj[0] = {"Gustaf", 12}; is not correct. Person is a type and does not have a member familj". What you need is
1
2
familj[0].Set("Gustaf", 12);
familj[1].Set("Lina", 14);


1
2
bubblesort(Person familj[], 4);
linsok(Person familj[], 4, a);

are not function calls, but forward declarations. These lines are better used before main or before the function definitions if they are before main. All you need for a function call is:
1
2
bubblesort(familj[], 4);
linsok(familj[], 4, a);


I have not checked the functions "bubblesort" or "linsok" yet to see if they work.

Hope this helps.

Andy
Last edited on
Hello elliotawerstedt,

I was wondering if you have made any progress with your program or if you have given up?

A correction I need to make, when I said all you need for a function call is:
1
2
bubblesort(familj[], 4);
linsok(familj[], 4, a);

in my copy and paste I missed removing the []. The corrected version is:

1
2
bubblesort(familj, 4);
linsok(familj, 4, a);


Although I did have a problem with the functions receiving the array and when I could not solve the problem I changed the code in main to use a vector which worked much better.

After some minor changs in the linsok and bubblesort to access the correct variables in the class both functions worked as you had written them.

Andy
Hi! I made an updated version of the code which you can find
here.
http://www.cplusplus.com/forum/beginner/206996/

I haven't yet corrected my previous code, I started over instead.

Topic archived. No new replies allowed.