0

Ticket office C++

You are working on a ticketing system. A ticket costs $10. The office is running a discount campaign: each group of 5 people is getting a discount, which is determined by the age of the youngest person in the group. You need to create a program that takes the ages of all 5 people as input and outputs the total price of the tickets. Sample Input: 55 28 15 38 63 Sample Output: 42.5 MY CODE: #include <iostream> using namespace std; int main() { int ages[5]; for(int i=0; i<5; i++){ cin >> ages[i]; } int temp = ages[5]; for(int i=0; i<5; i++) { if(temp>ages[i]){ temp = ages[i]; } } float d = 50*(temp/100.00); float total = 50-d; cout << "

quot; << total; } Whats problem. Output comes $50. but when i run it on PC it runs.

21st Jan 2021, 2:28 PM
Md Omar Faruk Maruf
Md Omar Faruk Maruf - avatar
15 Réponses
+ 16
#include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here int temp = ages[0]; for(int i=0; i<5; i++) { if(ages[i]<temp){ temp = ages[i]; } } float d = 50*(temp/100.00); float total = 50-d; cout << total; return 0; } This is working properly.
1st Jul 2021, 2:52 PM
Suraj Kumar
+ 3
I use pointer. This is work. #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here int sum = *ages; for (int j = 0; j < 5; j++) { if(*(ages + j) < sum){ sum = *(ages + j); } else { sum = sum; } } double percen = (double) 50 * sum / 100; double result = (double) 50 - percen; cout << result; return 0; }
4th Jul 2021, 10:45 AM
Azhar Faturohman Ahidin
Azhar Faturohman Ahidin - avatar
+ 3
My solution: #include <iostream> using namespace std; int main() { int ages[5]; int a = ages[1]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; if (a > ages[i]) { a = ages[i]; } } double b = a*(50/100.00); cout << 50 - b; return 0; }
22nd Dec 2021, 7:21 AM
Yisroel Yitzchock
+ 2
The problem is that you initialize "temp" with the value ages[ 5 ], which is undefined since it is no element of the array. I would suggest you to choose ages[ 0 ] instead, which also lets you skip the first loop cycle, i.e. int temp = ages[ 0 ]; for ( int i = 1; ... ) ... Regarding the duplicate post: Just delete the other, unused one, and everything is fine. Tips for questions: https://www.sololearn.com/Blog/38/8-simple-rules-to-get-help-from-the-community
21st Jan 2021, 2:55 PM
Shadow
Shadow - avatar
+ 2
This code is correct // your code goes here int temp = ages[0] ; for (int i=0; i<5; i++) { if(temp>ages[i]) { temp = ages[i] ; } } float d = 50*(temp/100.00); float total = 50-d; }
28th Apr 2021, 11:16 PM
Ayesha Kazi
+ 1
#include<iostream> using namespace std; int main(){ int ages[5]; int young; for (int i=0 ; i < 5; i++){ cin>> ages[i]; } young = ages[0]; for ( int i=0; i < 5 ; i++){ if (young > ages[i]){ young = ages[i]; } } float discount = young/100.0; int total = sizeof(ages)/sizeof(ages[0])*10; float afterDiscount = total - (total * discount); cout << afterDiscount; return 0; }
19th Dec 2023, 10:41 AM
Shahad K
Shahad K - avatar
0
Don't post same question repeatedly
21st Jan 2021, 2:39 PM
Krish
Krish - avatar
0
I don't understand you
21st Jan 2021, 2:40 PM
Md Omar Faruk Maruf
Md Omar Faruk Maruf - avatar
0
Sorry for that. It's my 2nd question in this app. For that I don't know proper asking process.
21st Jan 2021, 2:44 PM
Md Omar Faruk Maruf
Md Omar Faruk Maruf - avatar
0
24th Jun 2021, 1:15 PM
Carmen Morales
Carmen Morales - avatar
0
#include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here int temp=ages[0]; for(int i=0;i<5;i++) { if(temp>ages[i]) temp=ages[i]; } float price=50-((temp/100.0)*50); cout<<price<<endl; return 0; }
31st Jul 2021, 6:20 PM
Mohammad Matin Kateb
Mohammad Matin Kateb - avatar
0
#include <iostream> using namespace std; int main() { int ages[5],min ; float dis; for (int i = 0; i < 5; i++) { cin >> ages[i]; } min= ages[0]; for(int i = 1;i <5; i++){ if( min >ages[i]){ min = ages[i]; } } dis = 50*(min/100.00); float t= 50-dis; cout << t<<endl; //your code goes here return 0; }
16th Aug 2021, 6:47 AM
Vandana
Vandana - avatar
- 1
Why you posted the same question twice It is considered spam
21st Jan 2021, 2:41 PM
Krish
Krish - avatar
- 1
14th Oct 2021, 8:21 PM
Abyot Tilahun
Abyot Tilahun - avatar
- 2
Try this code. it's working #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } int n = sizeof(ages)/sizeof(ages[0]); int minimum = ages[0]; for(int i=0; i<n;i++) { minimum = min(minimum, ages[i]); } double d = 50*(minimum/100.00); double total = 50 - d; cout<<total; return 0; } If you have any doubts or issues then you can ask me.
8th Mar 2021, 4:16 PM
Sumit