+3

# Loop in C++

The question is in the post. Can anyone teach me how to do it together with crystal-clear explaination? https://www.sololearn.com/post/175551/?ref=app

+3

Here I wrote a code. (it's not an optimal version) https://code.sololearn.com/cL6ife5qABtZ/?ref=app Let's walkthrough it. struct CarInfo { int price; int regNo; }; This defines a structure for our car info. const int N = 5; // change this to the car count you want const int lowPriceCut = 30000; CarInfo cars[N]; int main() { float avgPrice = 0.0f; int maxPrice = 0; int maxPricedCarRegNo; for(int i = 0; i < N; i++) { cin >> cars[i].price >> cars[i].regNo; cout << '\n'; This gets input from user and stores in relevant variables. Sample input 80000 7399 avgPrice += cars[i].price; As you know we can calculate an average by adding all elements up and dividing by element count. if (cars[i].price > maxPrice) // Instead of this you could use maps { maxPrice = cars[i].price; maxPricedCarRegNo = cars[i].regNo; }

+2

This checks if the price you just entered is higher than max price program has seen so far. If yes the price you entered now is the max price. You could maps to do this but I chose this approach. } avgPrice /= N; After the loop avgPrice is equal to total price of all cars. Dividing it by number of cars give us the average. cout << "Average Price : " << avgPrice << '\n'; cout << "Max price car registration number : " << maxPricedCarRegNo << ", Max Price : " << maxPrice << '\n'; for(int i = 0; i < N; i++) { if(cars[i].price < lowPriceCut ) // using bool is one method.To keep track of low price cars you could maintain a separate array for lowPrice cars instead of this bool method { cout << "Car registration number : " << cars[i].regNo << ", Price : " << cars[i].price << '\n'; } } return 0; }

+2

However, getting multiple inputs in Sololearn is not a piece of cake 😅😅

+1

Look into control structure concept... Iterations and recursion