+ 2
C++ Ticket Office
I keep encountering the 'Timeout: the monitored program dumped core' message when trying to run this code. I think it has to do with memory allocation and using pointers etc. Please could someone explain how to fix this issue. Thank youuu #include <iostream> using namespace std; int main() { int ages[5]; float minNumber; float discountPrice; float discount; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } if (int y; minNumber < ages[y]) { minNumber = ages[y]; } discount = minNumber / 100; discountPrice = 5 * 10 * (1 - discount); cout << discountPrice; return 0; }
6 Respuestas
+ 2
Maximus Hillius
Try this:
#include <iostream>
using namespace std;
int main() {
int ages[5];
for (int i = 0; i < 5; ++i) {
cin >> ages[i];
}
double youngest = ages[0];
for (int a = 0; a <5; ++a)
{
if(youngest>ages[a])
{
youngest = ages[a];
}
}
double prezzo = 50 - (50*youngest/100);
cout << prezzo;
return 0;
}
+ 1
1. minNumber is not initialized. This causes anything you do with minNumber except assignments and initialization will be undefined. (Note, minNumber shouldn't be initialized too less.
2. I suppose you try to get the min of the ages array with the following:
if (int y; minNumber < ages[y]) {
minNumber = ages[y];
}
This doesn't work. Both minNumber and y is uninitialized and it should be minNumber > ages[y]. Note that you don't even put it in the loop. How do you compare every numbers with minNumber?
You could do:
for (int i = 0; i < 5; ++i) {
cin >> ages[i];
if(minNumber > ages[i]) {
minNumber = ages[i];
}
}
+ 1
Thank you both for your help!! I managed to get it to work for every example except the first for some reason ...
#include <iostream>
using namespace std;
int main() {
int ages[5];
float minNumber = ages[0];
float discount = 0;
float discountPrice = 0;
for (int i = 0; i < 5; ++i) {
cin >> ages[i];
if (minNumber > ages[i]) {
minNumber = ages[i];
}
}
discount = (1 - (minNumber / 100));
discountPrice = (50 * discount);
cout << discountPrice;
return 0;
}
0
minNumber = ages[0] results garbage number because ages[0] is uninitialized. ages[0] doesn't hold a number until cin.
0
Maximus Hillius
#include <iostream>
using namespace std;
int main() {
int ages[5];
float minNumber;
float discountPrice;
float discount;
cin >> ages[0];
minNumber = ages[0];
for (int i = 1; i < 5; ++i) {
cin >> ages[i];
if (minNumber > ages[i])
minNumber = ages[i];
}
discount = minNumber / 100;
discountPrice = 5 * 10 * (1 - discount);
cout << discountPrice;
return 0;
}
0
See my answer:
#include <iostream>
using namespace std;
int main() {
int ages[5];
int min , temp;
float disc, total;
for (int i = 0; i < 5; ++i) {
cin >> ages[i];
min = ages[0];
}
for (int i = 1; i < 5; i++) {
if (min < ages[i]){
temp = min;
}
else {
temp = ages[i];
}
min = temp;
}
disc = temp*(0.5);
total = 50-disc;
cout << total;
return 0;
}