forked from vishnuparikh/vmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5 CppArray.cpp
More file actions
167 lines (128 loc) · 3.23 KB
/
Copy path5 CppArray.cpp
File metadata and controls
167 lines (128 loc) · 3.23 KB
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
160
161
162
163
164
165
166
167
/*
ASSIGNMENT 03
NINAD M DESHPANDE
Roll No: 10
SE COMPUTER II
Question:
Implement a class CppArray which is identical to a one dimensional C++ array (i.e. the index is a set of consecutive integers starting from 0) except for the following:
1. It performs range check
2. It allows one to be assigned through the use of assignment operator (cp1=cp2)
3. It supports a function that returns the size of the array
4. It allows the reading or printing of the array through the use of cout and cin
*/
#include<iostream>
using namespace std;
class CppArray
{
int a[50],i,n,t;
int mem;
public:
CppArray()
{
for(i=0;i<50;i++)
a[i]=0;
}
void getdata();
void print();
void sort();
void range();
CppArray operator=(CppArray &obj)
{
CppArray temp;
n=obj.n;
for(i=0;i<n;i++)
a[i]=obj.a[i];
return temp;
};
void size();
friend ostream &operator<<(ostream &out, CppArray &cp);
};
ostream &operator<<(ostream &out, CppArray &cp)
{
int m;
out<<"EqArray [1]: "<<cp.a[0]<<endl;
for(m=1;m<cp.n;m++)
out<<"EqArray ["<<m+1<<"]: "<<cp.a[m]<<endl;
return out;
}
void CppArray::getdata()
{
cout<<"Enter the number of elements in the array:\n";
cin>>n;
cout<<"Enter the elements of the array:\n";
for(i=0;i<n;i++)
cin>>a[i];
}
void CppArray::sort()
{
int temp;
temp=0;
for(t=0;t<n;t++)
for(i=0;i<n-1;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
void CppArray::print()
{
cout<<"\nThe sorted array is:\n";
for(i=0;i<n;i++)
cout<<"Element "<<i+1<<": "<<a[i]<<endl;
}
void CppArray::range()
{
cout<<"\nThe range of the array is: "<<a[n-1]-a[0]<<endl;
}
void CppArray::size()
{
cout<<"\nThe size of the array is: "<<n<<endl;
mem=n*sizeof(a[0]);
cout<<"Memory storage of the array is: "<<mem<<" bytes"<<endl;
}
int main()
{
char out,cont;
out='N';
cont='N';
int op;
CppArray cp1,cp2;
do
{
cp1.getdata();
cp1.sort();
do
{
cout<<"\nSelect an operation to perform:\n";
cout<<"1. Size\n2. Range\n3. Equate\n4. Print\n";
cout<<"Enter Selection: ";
cin>>op;
switch(op)
{
case 1:
cp1.size();
break;
case 2:
cp1.range();
break;
case 3:
cp2=cp1;
cout<<"The equated array is:\n";
cout<<cp2;
break;
case 4:
cp1.print();
break;
default:
cout<<"Enter correct operation code\n";
}
cout<<"\nDo you want to select another operation?(Y/N): ";
cin>>cont;
}while(cont=='Y' || cont=='y');
cout<<"\nDo you want to try for another array?(Y/N): ";
cin>>out;
}while(out=='Y' || out=='y');
return 0;
}