Tickets problem. Did you find the error . Send solutions in comment | Sololearn: Learn to code for FREE!

+1

Tickets problem. Did you find the error . Send solutions in comment

#include <iostream> using namespace std; int main() { int ages[5],temp; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here for(int i = 0; i<5;i++) { if(ages[i]<ages[i+1]) { temp=ages[i]; ages[i]=ages[i+1]; ages[i+1]=temp; } } /* temp = INT_MAX; for(int i= 0;i<5;i++) temp= min(temp,ages[i]) */ float result=50*((100.0-temp)/100.0); cout<<result; return 0; }

1/13/2021 5:55:31 AM

ROHIT KUMAR

6 Answers

New Answer

+3

Your solution is most likely failing when the lowest value is the last element in the array (index 4). Your if statement(ages[i] < ages[i+1]) is only checking if the current value is smaller than the next one. Therefore you will miss evaluating the last element as the preceeding element would have been greater than it. Here is an example set: {25, 20, 34, 52, 10} With this set the discount applied is 10%. You can debug your code further in the playground with these values. Reply if you are still stuck.

+3

Adding to Elizabeth Kelly 's answer, comparing ages[i] with ages[i + 1] will also cause undefined behaviour when i = 4, in the second for loop. This is because you will be comparing ages[4] with ages[5], but the 5th index does not exist as the capacity of the array is 5. Also, please link the problem in the question, because currently, I don't know what you're trying to achieve.

+3

XXX Your point is completely valid, though in Sololearn’s playground the code does not fail, so the undefined behavior goes unnoticed. The problem referenced is #36, in the C++ tutorial. Its the at the end of module 3. ROHIT KUMAR please include the problem source in future questions as XXX asked. I just happened to complete the problem a few weeks ago and still remembered it.

+2

Elizabeth Kelly sorry, but I don't understand how it will go unnoticed. Sometimes accessing unallocated memory gives a huge number. If that is the case, then ages[4] < ages[5] will be true, no matter what is in ages[4]. And if ages[4] < ages[5] is true, ages[4] will assigned that huge undefined number, and the initial value of ages[4] will be moved out of the array bounds into ages[5]. ROHIT KUMAR the way you are finding the minimum number in the array is totally invalid. It looks like you are trying to sort the array, but the code is not going to work for that too. To find the minimum of an array, follow these steps (assume that the name of the array is `arr`): 1. make a variable of type int, and give it the value of arr[0]. For now let's call it `min` 2. make a for loop that loops a variable, let's say `i`, from 0 till (the length of the array - 1) 3. if arr[i] is less than `min`, change the value of `min` to arr[i] At the end of the loop, `min` will hold the minimum value in the array.

+1

Elizabeth Kelly XXX I check all elements in a loop . When I run in my compiler everything is fine But answer is not acceptable So I am sharing the Question 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 The youngest age is 15, so the group gets a 15% discount from the total price, which is $50 - 15% = $42.5 Thanks you For your efforts and time. 🥳🥳🥳

+1

Elizabeth Kelly c++ tickets office project