How to create a default size array

Pages: 12
So my whole program works but I am beating my head on something that should be really easy. I need my program to create a default array of size 5, when the user types 0. Not sure where or how I could come about it. I was thinking of doing an if statement on main() but I have a void create_array() function no parameters, and information hiding is in place. Or, that maybe inside of void create_array() i could do

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef ARRAY_H
#define ARRAY_H

using namespace std;

class Array {
    private:
        double* p_arr;
        int     size;
        void create_array();
    public:
        Array();
        Array(int); //int is to receive an array size
        ~Array();
        void    fill_array();
        double  find_avrg();
        int  maximum();
        void    reverse();
        void    print_array(ostream &);
        void setSize(int);
        int getSize();
};  
#endif

 #include <iostream>
#include <string>
#include <ostream>
#include <cstdlib>
#include <iomanip>

#include "Array.h"

using namespace std;

Array::Array() {

	size = 0;
	p_arr = NULL;
	
	
	cout << " Default constructor called" << endl;
}



Array::Array(int arr_size) { //int is to receive an array size
	
 //Here is where I wanted to do if size == 0 then default size 5, but not //sure how
	size = arr_size;
	if (size == 0) {
		create_array();

	}
	else {

		p_arr = new double [size];
	}
	// array size should be set here, this is what main is going to call 
	cout << "Overloaded constructor called " << endl;
} 

Array::~Array() {

	if (size > 0) {
		delete[] p_arr;
	}
	cout << "Destructor is Called " << endl;
}

void Array::create_array() {

	p_arr = new double[];

// I also thought i could try and put a 5 inside the brackets but it still doesnt do the trick. 

}

void Array::fill_array() {
	for (int i = 0; i < size; i++) {
		p_arr[i] = rand() % 20 + 10;
		cout << "Filling array   " << p_arr[i] << endl;
	}

}

double Array::find_avrg() 
{
	double avg, sum = 0.0;
	for (int i = 0; i < size; i++) {
		sum += p_arr[i];
	}
	sum = sum / size;
	avg = sum;
	cout << "Average is = " << avg << endl;
	return avg;

}

void  Array::print_array( ostream & out) 
{
	for (int i = 0; i < size; i++)
		out << fixed << setw(5) << setprecision(3) << p_arr[i] << " " << setw(15) << &p_arr[i] << " " << endl;
}

int Array::maximum() 
{	
	if (size == 0)
		return -1;
	double* max_pos = p_arr;
	for (int i = 1; i < size; ++i) {
		if (p_arr[i] > *max_pos) {
			max_pos = &p_arr[i];
		}		
	}
	cout << "Max value is : " << *max_pos << endl;
	return *max_pos;
	
}

void Array::reverse() 
{
	double comp, * last;
	double * first = p_arr;
	last = &p_arr[size - 1];
	for (int i = 0; i < size / 2; i++) {
		comp = *first;
		*first = *last;
		*last = comp;
		first++;
		last--;
	}
}

#include <iostream>
#include <string>
#include "Array.h"


using namespace std;

int main()
{
	cout << " Enter the size of the array : ";
	int size_arr;
	cin >> size_arr;

	Array arr (size_arr); 
	arr.fill_array();
	arr.print_array(cout);
	arr.find_avrg();
	arr.maximum();
	arr.reverse();
	arr.print_array(cout);
	arr.reverse();
	
	return 0;
	system("pause");
}
Also how can I avoid possible lost data inside my int maximum() function ? I am returning double and the function is returning an int
So change the return type of the function to be a double.

As for your first question, something like:
1
2
3
4
5
6
7
8
9
10
11
{
    if (arr_size <= 0)
    {
        size = 5;
    }
    else
    {
        size = arr_size;
    }
    p_arr = new double[size];
}
Last edited on
I cannot change the datatype of the function, it's a requirement to have it return int
Is Array::maximum() supposed to return the maximum value, or the index of the maximum value?
I.e. for [1.1, 3.2, 5.6, 4.0, 2.3], should it return 5.6, or 2?
Last edited on
I actually need help with Maximum () function too i dont know how to return an int, when my pointer is a double
it is supposed to return the max value yes
Unless you show us the actual wording of the assignment, this is just a bad game of "telephone" to decipher.

Note that you aren't doing anything with the return value of your function either way, rendering this entire worry moot.
Last edited on
Find the index that stores the maximum value among the data and returns it
int maximum();

If the size of the array is 0, return -1. Even if your program checks the size of the array that is greater than 0 early in the program, still add this requirement.
My whole code is carshing at rand() now i have no idea why, and My Maximum() is not returning anything, it also breaks there, please help
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
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>
#include <ostream>
#include <cstdlib>
#include <iomanip>

#include "Array.h"

using namespace std;

Array::Array() {

	size = 0;
	p_arr = NULL;
	
	
	cout << " Default constructor called" << endl;
}



Array::Array(int arr_size) { //int is to receive an array size
	
	p_arr = NULL;
	size = arr_size;
	if (size <= 0) {
		size = 5;
	}
	else {

		p_arr = new double [size];
	}
	// array size should be set here, this is what main is going to call 
	cout << "Overloaded constructor called " << endl;
} 

Array::~Array() {

	if (size > 0) {
		delete[] p_arr;
	}
	cout << "Destructor is Called " << endl;
}

void Array::create_array() {

	p_arr = new double[size];

}

void Array::fill_array() {
	for (int i = 0; i < size; i++) {
		p_arr[i] = rand() % 20 + 10;
		
	}

}

double Array::find_avrg() 
{
	double avg, sum = 0.0;
	for (int i = 0; i < size; i++) {
		sum += p_arr[i];
	}
	sum = sum / size;
	avg = sum;
	cout << "Average is = " << avg << endl;
	return avg;

}

void  Array::print_array( ostream & out) 
{
	for (int i = 0; i < size; i++)
		out << fixed << setw(5) << setprecision(3) << p_arr[i] << " " << setw(15) << &p_arr[i] << " " << endl;
}

int Array::maximum() 
{	
	if (size == 0)
		return -1;
	double* max_pos = p_arr;
	for (int i = 1; i < size; ++i) {
		if (p_arr[i] > *max_pos) {
			max_pos = &p_arr[i];
		}		
	}
	cout << "Max value is : " << *max_pos << endl;
	return *max_pos;
	
}

void Array::reverse() 
{
	double comp, * last;
	double * first = p_arr;
	last = &p_arr[size - 1];
	for (int i = 0; i < size / 2; i++) {
		comp = *first;
		*first = *last;
		*last = comp;
		first++;
		last--;

	}



}

"Find the index .... and return it."
Good, that settles it. You're supposed to return an index, not the value, which matches what helios suggested.
Last edited on
I am not sure how to return the index in this case the whole maximum function is wrong, please help me
You're making this much more complicated that it needs to be. Consider:

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
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <ctime>

class Array {
private:
	int size {};
	double* p_arr {nullptr};

public:
	Array();
	Array(int);
	~Array();
	void    fill_array();
	double  find_avrg();
	int  maximum();
	void    reverse();
	void    print_array(std::ostream&);
};

Array::Array()
{
	std::cout << " Default constructor called" << std::endl;
}

Array::Array(int arr_size) : size(arr_size <= 0 ? 5 : arr_size), p_arr(new double[size])
{
	std::cout << "Overloaded constructor called " << std::endl;
}

Array::~Array()
{
	delete[] p_arr;
	std::cout << "Destructor is Called " << std::endl;
}

void Array::fill_array()
{
	std::generate(p_arr, p_arr + size, [] {return rand() % 20 + 10; });
}

double Array::find_avrg()
{
	return std::accumulate(p_arr, p_arr + size, 0.0) / size;
}

void  Array::print_array(std::ostream& out)
{
	for (int i = 0; i < size; ++i)
		out << std::fixed << std::setw(5) << std::setprecision(3) << p_arr[i] << std::endl;
}

int Array::maximum()
{
	return std::max_element(p_arr, p_arr + size) - p_arr;
}

void Array::reverse()
{
	std::reverse(p_arr, p_arr + size);
}

int main()
{
	srand(time(0));	// Seed random number generator

	std::cout << "Enter the size of the array : ";

	int size_arr;
	std::cin >> size_arr;

	Array arr(size_arr);

	arr.fill_array();

	std::cout << "Original array is\n";
	arr.print_array(std::cout);

	std::cout << "Average is = " << arr.find_avrg() << std::endl;
	std::cout << "Max position is: " << arr.maximum() << std::endl;

	arr.reverse();
	std::cout << "Reversed array is\n";
	arr.print_array(std::cout);
}


which displays as an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Enter the size of the array : 0
Overloaded constructor called
Original array is
18.000
25.000
27.000
27.000
29.000
Average is = 25.200
Max position is: 4
Reversed array is
29.000
27.000
27.000
25.000
18.000
Destructor is Called

As seeplus points out, a lot of what you're trying to do is available in the standard library. If you can't do that, then here are some comments on your latest version of the code.

Line 29. Why the else? You always want to allocate p_arr, so delete lines 29 and 32. This means you can also delete line 24.

Line 39: I'd always delete p_arr. It's legal to delete a null pointer.

Why does the default constructor create an array with no elements, but Array(0) creates an array with 5? That's inconsistent. It seems to me that the default constructor should also make an array with 5 elements. Read the assignment carefully on this point.

Speaking of the assignment, you should post the whole thing here. It's very VERY common for students to miss critical aspects of an assignment. Usually every single word of the text means something to the code.

create_array() sure looks lonely. If you don't call it then why do you need it? If you need it then where should you call it? I suspect that what you really need is something that will resize the array, not simply create it.

I don't think maximum() or find_avrg() should print their results. Whatever calls them should print it.

Speaking of maximum, just change max_pos to the index of the max item:
1
2
3
4
5
6
7
8
9
10
11
12
int Array::maximum() 
{	
	if (size == 0)
		return -1;
	int max_pos = 0
	for (int i = 1; i < size; ++i) {
		if (p_arr[i] > p_arr[max_pos]) {
			max_pos = i;
		}		
	}
	return max_pos;
}


You don't have any code to access items in the array. Does the assignment require them?
@Seeplus That is amazing, I see you used:

1
2
#include <algorithm>
#include <numeric> 


How as a programmer did you figure this trick? Like how did you ever first encountered this and thought " oh i am going to use this" Did it just come around or did you study it, did someone introduce you to is? What is a good book for these kinds of easier methods? I hope you know what i am talking about
@Seeplus also my code breaks at fill_array() with what you showed, the message says
" corruption of memory in the heap "
If I wanted to use a similar way of coding and not what SeePlus did, how would I be able to finish this project ? Someone please explain with similar code to mine
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

#include <iostream>
#include <string>
#include <ostream>
#include <cstdlib>
#include <iomanip>

#include "Array.h"

using namespace std;

Array::Array() {

	size = 0;
	p_arr = NULL;
	
	
	cout << " Default constructor called" << endl;
}



Array::Array(int arr_size) { //int is to receive an array size
	
	p_arr = NULL;
	size = arr_size;
	if (size <= 0) {
		size = 5;
	}
	else {

		p_arr = new double [size];
	}
	// array size should be set here, this is what main is going to call 
	cout << "Overloaded constructor called " << endl;
} 

Array::~Array() {

	if (size > 0) {
		delete[] p_arr;
		
	}
	cout << "Destructor is Called " << endl;
	cout << "Deleting =  " << &p_arr << endl;
}

void Array::create_array() {

	p_arr = new double[size];

}

void Array::fill_array() {
	for (int i = 0; i < size; i++) {
		p_arr[i] = rand() % 20 + 10;
		
	}

}

double Array::find_avrg() 
{
	double avg, sum = 0.0;
	for (int i = 0; i < size; i++) {
		sum += p_arr[i];
	}
	sum = sum / size;
	avg = sum;
	cout << "Average is = " << avg << endl;
	return avg;

}

void  Array::print_array( ostream & out) 
{
	for (int i = 0; i < size; i++)
		out << fixed << setw(5) << setprecision(3) << p_arr[i] << " " << setw(15) << &p_arr[i] << " " << endl;
}

int Array::maximum() 
{	
	if (size == 0)
		return -1;
	double* max_pos = p_arr;
	for (int i = 1; i < size; ++i) {
		if (p_arr[i] > *max_pos) {
			max_pos = &p_arr[i];
		}		
	}
	cout << "Max value is : " << *max_pos << endl;
	return *max_pos;
	
}

void Array::reverse() 
{
	double comp, * last;
	double * first = p_arr;
	last = &p_arr[size - 1];
	for (int i = 0; i < size / 2; i++) {
		comp = *first;
		*first = *last;
		*last = comp;
		first++;
		last--;
	}
}

#include <iostream>
#include <string>
#include "Array.h"
#include <ctime>

using namespace std;

int main()
{
	srand(time(0));
	cout << " Enter the size of the array : ";
	int size_arr;
	cin >> size_arr;

	Array arr (size_arr); 
	arr.fill_array();
	arr.print_array(cout);
	cout << "Average of the array is : " << arr.find_avrg() << endl;
	cout << "Maximum position :  " << arr.maximum() << endl;
	arr.reverse();
	cout << "Reversed array is :  " << endl;
	arr.print_array(cout);
		
	return 0;
	system("pause");
}

@Seeplus also my code breaks at fill_array() with what you showed, the message says
" corruption of memory in the heap "


I'm using VS2019 and in both debug/release modes it works fine with array sizes up to 50 (I included my output in the post). What compiler/system are you using? Are you compiling as debug or release?

How as a programmer did you figure this trick? Like how did you ever first encountered this and thought " oh i am going to use this" Did it just come around or did you study it, did someone introduce you to is? What is a good book for these kinds of easier methods? I hope you know what i am talking about


I've used C++ for quite a while...., I've programmed for a lot longer, I have a selection of C++ books, I look at the documentation for C++ such as http://www.cplusplus.com/reference/ and https://en.cppreference.com/w/cpp

In this case, mainly http://www.cplusplus.com/reference/algorithm/. Every C++ programmer should be aware of these.

What resources are you using to learn C++ (book/on-line)?
Last edited on
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef ARRAY_H
#define ARRAY_H

using namespace std;

#include <iostream>

class Array {
    
private:
    double* p_arr = nullptr;
    int size = 5;
    
public:
    Array();
    Array(int);
    ~Array();
    void fill_array();
    double find_avrg();
    int maximum();
    void reverse();
    void print_array(ostream &);
    int getSize(){return size;}
};
#endif

#include <iostream>
#include <string>
#include <ostream>
#include <cstdlib>
#include <iomanip>

//#include "Array.h"

using namespace std;

Array::Array()
{
    cout << " Default constructor called" << endl;
}

Array::Array(int arr_size)
{
    
    if(arr_size < size)
        size = 5;
    else
        size = arr_size;
    
    p_arr = new double [size];
    
    cout << "Overloaded constructor called " << endl;
}

Array::~Array()
{
    delete[] p_arr;
    cout << "Destructor is Called " << endl;
}

void Array::fill_array() {
    for (int i = 0; i < size; i++) {
        p_arr[i] = rand() % 20 + 10;
        cout << "Filling array   " << p_arr[i] << endl;
    }
    
}

double Array::find_avrg()
{
    double avg, sum = 0.0;
    for (int i = 0; i < size; i++) {
        sum += p_arr[i];
    }
    sum = sum / size;
    avg = sum;
    cout << "Average is = " << avg << endl;
    return avg;
    
}

void  Array::print_array( ostream & out)
{
    for (int i = 0; i < size; i++)
    out
    << fixed << setw(5) << setprecision(3) << p_arr[i] << " "
    << setw(15) << &p_arr[i] << " " << endl;
}

int Array::maximum()
{
    if (size == 0)
        return -1;
    double* max_pos = p_arr;
    for (int i = 1; i < size; ++i) {
        if (p_arr[i] > *max_pos) {
            max_pos = &p_arr[i];
        }
    }
    cout << "Max value is : " << *max_pos << endl;
    return *max_pos;
    
}

void Array::reverse()
{
    double comp, * last;
    double * first = p_arr;
    last = &p_arr[size - 1];
    for (int i = 0; i < size / 2; i++) {
        comp = *first;
        *first = *last;
        *last = comp;
        first++;
        last--;
    }
}

#include <iostream>
#include <string>
//#include "Array.h"


using namespace std;

int main()
{
    Array arr_1;
    std::cout << "Size: " << arr_1.getSize() << '\n';
    
    int sz;
    std::cout << "Input size: ";
    std::cin >> sz;
    Array arr(sz);
    std::cout << "Size: " << arr.getSize() << '\n';
    
    
    arr.fill_array();
    arr.print_array(cout);
    arr.find_avrg();
    arr.maximum();
    arr.reverse();
    arr.print_array(cout);
    arr.reverse();
    
    return 0;
}


 Default constructor called
Size: 5
Input size: 0
Overloaded constructor called 
Size: 5
Filling array   17
Filling array   19
Filling array   23
Filling array   28
Filling array   20
17.000     0x100504270 
19.000     0x100504278 
23.000     0x100504280 
28.000     0x100504288 
20.000     0x100504290 
Average is = 21.400
Max value is : 28.000
20.000     0x100504270 
28.000     0x100504278 
23.000     0x100504280 
19.000     0x100504288 
17.000     0x100504290 
Destructor is Called 
Destructor is Called 
Program ended with exit code: 0
how would I be able to finish this project ? Someone please explain with similar code to mine


Consider:

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
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <ctime>

class Array {
private:
	int size {};
	double* p_arr {nullptr};

public:
	Array();
	Array(int);
	~Array();
	void    fill_array();
	double  find_avrg();
	int  maximum();
	void    reverse();
	void    print_array(std::ostream&);
};

Array::Array()
{
	std::cout << " Default constructor called" << std::endl;
}

Array::Array(int arr_size) : size(arr_size <= 0 ? 5 : arr_size), p_arr(new double[size])
{
	std::cout << "Overloaded constructor called " << std::endl;
}

Array::~Array()
{
	delete[] p_arr;
	std::cout << "Destructor is Called " << std::endl;
}

void Array::fill_array()
{
	//std::generate(p_arr, p_arr + size, [] {return rand() % 20 + 10; });
	for (int i = 0; i < size; ++i)
		p_arr[i] = rand() % 20 + 10;
}

double Array::find_avrg()
{
	//return std::accumulate(p_arr, p_arr + size, 0.0) / size;
	double sum = 0.0;

	for (int i = 0; i < size; ++i)
		sum += p_arr[i];

	return sum / size;
}

void  Array::print_array(std::ostream& out)
{
	for (int i = 0; i < size; ++i)
		out << std::fixed << std::setw(5) << std::setprecision(3) << p_arr[i] << std::endl;
}

int Array::maximum()
{
	//return std::max_element(p_arr, p_arr + size) - p_arr;
	if (size < 1)
		return -1;

	int max_pos = 0;
	double max_val = p_arr[0];

	for (int i = 1; i < size; ++i)
		if (p_arr[i] > max_val) {
			max_val = p_arr[i];
			max_pos = i;
		}

	return max_pos;
}

void Array::reverse()
{
	//std::reverse(p_arr, p_arr + size);
	for (int i = 0, mid = size / 2; i < mid; ++i)
		std::swap(p_arr[i], p_arr[size - i - 1]);
}

int main()
{
	srand(time(0));	// Seed random number generator

	std::cout << "Enter the size of the array : ";

	int size_arr;
	std::cin >> size_arr;

	Array arr(size_arr);

	arr.fill_array();

	std::cout << "Original array is\n";
	arr.print_array(std::cout);

	std::cout << "Average is = " << arr.find_avrg() << std::endl;
	std::cout << "Max position is: " << arr.maximum() << std::endl;

	arr.reverse();
	std::cout << "Reversed array is\n";
	arr.print_array(std::cout);
}

Pages: 12