How to create a default size array

Pages: 12
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
#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
Topic archived. No new replies allowed.
Pages: 12