Я много раз компилировал свой код и получал ошибку, связанную с памятью.Я новичок в программировании и не мог понять проблему.Если кто-то может помочь мне понять, почему это происходит, и дать мне несколько советов, как очистить мой код / сделать его более эффективным, я был бы очень признателен за это!
#include <iostream>
#include <cmath>
void chefsMenuitems();
void chefsMenuitems(int P[], int arr_size) {
int count = 0;
int current_item = 0;
int n = 0;
int i = 0;
for (int j = 0; j < arr_size; j++ ) {
while(P[j] > 0) {
for ( i = 0; current_item < P[j]; i++) {
current_item = pow(2,(i));
if (current_item > P[j]){
current_item = pow(2,(i - 1));
break;
}
}
P[j] = P[j] - current_item;
current_item = 0;
n++;
}
count++;
std::cout << "The number of menu items for price " << count << " are: " << n << "\n";
n = 0;
current_item = 0;
}
}
int main() {
int T = 0;
int P[] = {0};
int arr_size;
std::cout << "Please enter the number of test cases: \n";
std::cin >> T;
while(T < 1 || T > 5 ) {
std::cout << "Test cases must be between 1 & 5 inclusive: \n";
std::cin >> T;
}
arr_size = T;
for (int i = 0; i < T; i++) {
std::cout << "Please enter the amount you are willing to spend: \n";
std::cin >> P[i];
while(P[i] < 1 || P[i] > pow(10, 5)) {
std::cout << "The amount you are willing to spend must be between 1 and 10^5 inclusive: \n";
std::cin >> P[i];
}
}
chefsMenuitems(P, arr_size);
return 0;
}