-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrays_and_Pointers.cpp
136 lines (129 loc) · 3.17 KB
/
Arrays_and_Pointers.cpp
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
#include <iostream>
using namespace std;
double large_of_average(double a[], int size);
int sum_negative(int arr[], int size);
int distance(int arr[], int size);
bool symmetry(int matrix[][3], int size);
void main() {
int option, num, size;
double doubleNum;
cout << "Menu:\n0. Exit\n1. Display a percent of elements bigger on average \n2. Display a sum of negative elements\n3. Difference between the largest and smallest elements in an array\n4. Is matrix symmetric\n";
do {
cout << "Enter your choice (0-5):" << endl;
cin >> option;
if(option == 1) {
int size;
cin >> size;
double* a = new double[size];
for(int i = 0; i < size; i++) {
cin >> a[i];
}
double percent = large_of_average (a, size);
}
if(option == 2) {
int size;
cin >> size;
int* arr = new int[size];
for(int i = 0; i < size; i++) {
cin >> arr[i];
}
int sum = sum_negative (arr, size);
}
if(option == 3) {
int size;
cin >> size;
int* arr = new int[size];
for(int i = 0; i < size; i++) {
cin >> arr[i];
}
int dis = distance (arr, size);
}
if(option == 4) {
int matrix[3][3];
int size;
cin >> size;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
cin >> matrix[i][j];
bool result = symmetry (matrix, size);
}
}
while(option != 0);
}
//A program that receives an array of real numbers and its size and returns the percentage of organs in the array that are really larger than average
double large_of_average(double a[], int size) {
double* arr = new double[size];
double sum = 0;
double average = 0;
double percent = 0;
int x = 0;
int flag = 0;
double sizeD = size;
arr = a;
for(int i = 0; i < size; i++) {
sum += a[i];
}
average = sum / size;
for(int j = 0; j < size; j++) {
if(a[j] > average) {
x++;
}
}
percent = x / sizeD;
delete []arr;
return percent;
}
//A program that receives an array of integers and its size, the function returns the sum of all the negative members in the array
int sum_negative(int arr[], int size) {
int* arrTemp = new int[size];
int sum = 0;
arrTemp = arr;
for(int i = 0; i < size; i++) {
if(arrTemp[i] < 0) {
sum += arrTemp[i];
}
}
delete []arrTemp;
return sum;
}
//A program that returns the difference between the largest organ and the smallest organ in the array
int distance(int arr[], int size) {
int* arrTemp = new int[size];
arrTemp = arr;
int distance, max, min;
if(size == 0)
return 0;
else {
min = arrTemp[0];
max = arrTemp[0];
}
for(int i = 0; i < size; i++) {
if(arrTemp[i] < min)
min = arrTemp[i];
if(arrTemp[i] > max)
max = arrTemp[i];
}
distance = max - min;
delete []arrTemp;
return distance;
}
//A program that checks whether a quadratic matrix is diagonal
bool symmetry(int matrix[][3], int size) {
int flag = 0;
if(size != 3)
return false;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
if(matrix[i][j] != matrix[j][i])
flag = 1;
}
}
/*for(int i = 0; i < 3; i ++)
delete[] matrix[i];
delete[] matrix;
*/
if(flag == 0)
return true;
else
return false;
}