classes

Lads, I've got a question:
I am trying to identify the maximum value of an array of objects which was copied from the initial array of objects, writing a method inside the Class and calling it in the main function.

( I am posting just some lines of the code) if there is a need of the entire code I am at your disposal.

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 var
{
public:

int maxY(int t[], int n)
{
	int max=t[0];
	for (int i=0; i<n; i++)
 {
 	if(max<t[i]) max=t[i];
 }
 return max;	
}

void insert(char mes[]);{ cout<<mes; cin>>x; }
int returnX();{ return x; }
}

int main()
{
var Y[100], n, max;

n.insert("Enter n= ");

// copying the array of objects X into another Y and it works fine

for (int i=0,j=0; i<n.returnX(); j++,i++)
{
	Y[j]=X[i];
}

//displaying the new array of objects
for (int i=0; i<n.returnX(); i++)
{
	cout<<"\n Y["<<i<<"]="<<Y[i].returnX()<<endl;
	
}

cout<<" maximum value= "<<max.maxY(Y[100], n.returnX()); // I am not sure how to send correctly the arguments as they are objects not simple variables, please help  
Last edited on
 
int max;


You haven't initialised max!

1
2
3
4
5
6
7
8
9
10
11
12
int maxY(int t[], int n)
{
    int max = t[0];

    for (int i = 1; i < n; i++)
    {
        if (max < t[i])
            max = t[i];
    }

    return max;	
}

Last edited on
Hello MaxGreen,

Let's start with the class:

Your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  class var
{
public:

int maxY(int t[], int n)
{
	int max;
	for (int i=0; i<n; i++)
 {
 	if(max<t[i]) max=t[i];
 }
 return max;	
}

void insert(char mes[]);{ cout<<mes; cin>>x; }
int returnX();{ return x; }
}


Add with proper indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class var
{
    public:

    int maxY(int t[], int n)
    {
        int max;
        for (int i = 0; i < n; i++)
        {
            if (max < t[i]) max = t[i];
        }
        return max;
    }

    void insert(char mes[]); { cout << mes; cin >> x; }
    int returnX(); { return x; }
}


And with a little rearranging:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class var
{
    public:

    int maxY(int t[], int n)
    {
        int max;

        for (int i = 0; i < n; i++)
        {
            if (max < t[i]) max = t[i];
        }

        return max;
    }

    void insert(char mes[]);
    {
        std::cout << mes; std::cin >> x; 
    }

    int returnX(); { return x; }
}

Line 17 is a great forward declaration, but not very good as a function.

Writing lines 17 - 20 this way helps the errors stand out.

Once you make it a function then "x" becomes a problem. Where is it defined? The function expecting it to be a variable of the class.

Then there is the same problems with line 22.

Line 23 is missing something to make it a proper class.

Two more points. "insert" may be better named as "setX" and "returnX" as "getX". "insert" is a bit misleading as to what it does.

Sometimes it is better to keep the code readable until it is working and then put it into 1 line later. I refer to the "insert" function and the "returnX" function.

I also have to ask if you have to use a "char" array or if you could use a "std::string"?

If you have to use a "char" array then the "insert" function needs changed because you are trying to pass a constant char pointer ,(n.insert("Enter n= ");), to a char pointer, "char* mag". And this does not work.

You need to get the class right before you can start working on "main".

Andy
@seeplus - indeed,
but anyway line 39 is a faulty one and I can't figure it out how to send the arguments , in order to get a maxY function working properly.

@Handy Andy - thanks a lot for your guidance , I will keep it in mind. That's my second lesson studying classes so there could be a lot of issues but I will do my best.
Last edited on
Post your full code so we can see what's being done.
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
96
97
98

class var
{
int x=0;

public:

void insert(char mes[]);
void randomvariable(int xxx){ x=rand()%xxx; }

//void equalvalues(count){	if(x %2==0) count++; }

int maxY(int t[], int n)
{
	int max=t[0];
	for (int i=0; i<n; i++)
 {
 	if(max<t[i]) max=t[i];
 }
 return max;	
}

int minY(int t[], int n)
{
	int min=t[0];
	for (int i=0; i<n; i++)
 {
 	if(min>t[i]) min=t[i];
 }
 return min;	
}

int returnX();

void sum(int yyy){ x+=yyy; }
};


void var::insert(char mes[]){ cout<<mes; cin>>x; }

int var::returnX(){ return x; }



int main()
{

var a;
var b;
var c;
var X[100], n, s;
var Y[100],max, min, count;

n.insert("Enter n= ");

a.insert("Enter a=");
b.insert("Enter b=");
c.insert("Enter c=");

cout<<"  a="<< a.returnX()<<endl;
cout<<"  b="<< b.returnX()<<endl;
cout<<"  c="<< c.returnX()<<endl;

cout<<" a+b-c="<< a.returnX() + b.returnX() - c.returnX() <<endl;

for (int i=0; i<n.returnX(); i++)
{
	X[i].randomvariable(60);
	s.sum(X[i].returnX());
}

for (int i=0; i<n.returnX(); i++)
{
	cout<<" X["<<i<<"]="<< X[i].returnX() <<endl;
}


// copying the array of objects X into a new one Y
for (int i=0,j=0; i<n.returnX(); j++,i++)
{
	Y[j]=X[i];
}

for (int i=0; i<n.returnX(); i++)
{
	cout<<"\n Y["<<i<<"]="<< Y[i].returnX() <<endl;
}


cout<<" sum of array of objects="<<s.returnX()<<endl;

//cout<<" a, b si c got even values for "<<count.count()<<" ori"<<endl;

cout<<" max value= "<<max.maxY(Y[100], n.returnX()); // faulty one and I can't figure it out how to send the arguments , in order to get a maxY function working properly.

return 0;
}
You really should use a sensible indentation style. That would help you (and us) see the structure of your code more clearly, and help you (and us) spot problems in the code more quickly.

After 78 posts here, I cannot possibly be the first person to tell you this.
I think this is what you're after:

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 <cstdlib>
#include <ctime>
using namespace std;

class var
{
	int x = 0;

public:

	void insert(char mes[]);
	void randomvariable(int xxx) { x = rand() % xxx; }

	static int maxY(var t[], int n)
	{
		int max = t[0].returnX();

		for (int i = 1; i < n; i++)
			if (max < t[i].returnX()) max = t[i].returnX();

		return max;
	}

	static int minY(var t[], int n)
	{
		int min = t[0].returnX();

		for (int i = 1; i < n; i++)
			if (min > t[i].returnX()) min = t[i].returnX();

		return min;
	}

	int returnX();

	void sum(int yyy) { x += yyy; }
};


void var::insert(char mes[]) { cout << mes; cin >> x; }

int var::returnX() { return x; }



int main()
{
	srand(time(0));

	var a;
	var b;
	var c;
	var X[100], n, s;
	var Y[100], max, min, count;

	n.insert("Enter n= ");
	a.insert("Enter a=");
	b.insert("Enter b=");
	c.insert("Enter c=");

	cout << "  a=" << a.returnX() << endl;
	cout << "  b=" << b.returnX() << endl;
	cout << "  c=" << c.returnX() << endl;

	cout << " a+b-c=" << a.returnX() + b.returnX() - c.returnX() << endl;

	for (int i = 0; i < n.returnX(); i++)
	{
		X[i].randomvariable(60);
		s.sum(X[i].returnX());
	}

	for (int i = 0; i < n.returnX(); i++)
	{
		cout << " X[" << i << "]=" << X[i].returnX() << endl;
	}


	// copying the array of objects X into a new one Y
	for (int i = 0, j = 0; i < n.returnX(); j++, i++)
	{
		Y[j] = X[i];
	}

	for (int i = 0; i < n.returnX(); i++)
	{
		cout << "\n Y[" << i << "]=" << Y[i].returnX() << endl;
	}

	cout << " sum of array of objects=" << s.returnX() << endl;
	cout << " max value= " << var::maxY(Y, n.returnX());

	return 0;
}


maxY() is marked as static as it doesn't use any of the member variables and its data is passed as arguments.
max.maxY(Y[100], n.returnX())

Your problem has nothing to do with classes - it's because you don't know how to pass an array into a function.

Y[100] is the 101st element of the array. Since the array only has 100 elements, you are trying to pass a value beyond the end of the array, which is undefined behaviour. To pass the array, simply pass Y.

EDIT: Your design is strange. It makes no sense for MaxY and MinY to be members of var, because they make no use of any of the data members of var. They should simply be free functions.

Also, it makes no sense to call them "MinY" and "MaxY", because they have nothing to do with anything called "Y". They simply provide the max and min values of elements in any array.
Last edited on
Thank you all for the support, really appreciate your help!
Topic archived. No new replies allowed.