-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.cpp
140 lines (108 loc) · 3.6 KB
/
malloc.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
137
138
139
140
// Pawel Adamczuk
//Dynamically allocated arrays, adding and modifying content.
#include <iostream>
#include <cstdarg>
#include <string.h>
#include <stdlib.h>
using namespace std;
void mySwap (int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
char oper;
int mSize;
int nSize;
int** mainArr;
int temp1;
int temp2;
int temp3;
int* tempArr;
cin >> oper;
if (oper == 'A')
{
cin >> mSize;
cin >> nSize;
//A (&mainArr, mSize, nSize);
mainArr = (int**)malloc(mSize * sizeof(int*));
for(int i=0; i < mSize; i++)
mainArr[i] = (int*)malloc(nSize * sizeof(int));
for (int j = 0; j < mSize; j++)
for (int k = 0; k < nSize; k++)
cin >> mainArr [j] [k];
}
while (oper != 'K')
{
switch (oper)
{
case 'B':
cin >> temp1 >> temp2;
cin >> mainArr [temp1] [temp2];
break;
case 'C':
cin >> temp1 >> temp2;
for (int i = 0; i < nSize; i++)
mySwap (&mainArr [temp1] [i], &mainArr [temp2] [i]);
break;
case 'D':
cin >> temp1;
temp2 = mainArr [temp1] [nSize - 1];
for (int i = nSize - 1; i > 0; i--)
mainArr [temp1] [i] = mainArr [temp1] [i - 1];
mainArr [temp1] [0] = temp2;
break;
case 'E':
cin >> temp1;
temp3 = 0;
for (int i = 0; i < nSize; i++)
{
cin >> temp2;
temp3 += temp2 * mainArr [temp1] [i];
}
cout << temp3 << endl;
break;
case 'F':
tempArr = new int [mSize];
for (int i = 0; i < mSize - 1; i++)
tempArr [i] = 0;
for (int i = 0; i < nSize; i++)
{
cin >> temp1;
for (int j = 0; j < mSize; j++)
{
tempArr [j] += mainArr [j] [i] * temp1;
}
}
for (int i = 0; i < mSize - 1; i++)
for (int j = 0; j < mSize - i - 1; j++)
if (tempArr [j] > tempArr [j+1])
mySwap(&tempArr [j], &tempArr[j+1]);
for (int i = 0; i < mSize; i++)
cout << tempArr [i] << " ";
cout << endl;
break;
case 'G':
cin >> temp1;
temp3 = 1001;
for (int i = 0; i < mSize; i++)
if (mainArr [i] [temp1] < temp3)
{
temp3 = mainArr [i] [temp1];
temp2 = i;
}
cout << temp2 << endl;
break;
}
cin >> oper;
}
for (int i = 0; i < mSize; i++)
{
for (int j = 0; j < nSize; j++)
cout << mainArr [i] [j] << ' ';
cout << endl;
}
}