
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to find out the total amount of sales we made
Suppose, we are selling 4 items and the price of the i-th item is given in the array 'cost[i]'. Now we sell the items in the order given in the string 'items'. We have to find out the total amount of sales that we have made. The string 'items' contain integer numbers from 1 to 4, duplicates can be present and they can be in any order.
So, if the input is like cost = {10, 15, 10, 5}, items = "14214331", then the output will be 75.
Steps
To solve this, we will follow these steps −
total := 0 for initialize i := 0, when i < size of items, update (increase i by 1), do: total := total + cost[items[i] - '0' - 1] return total
Example
Let us see the following implementation to get better understanding
#include <bits/stdc++.h> using namespace std; #define N 100 int solve(int cost[], string items) { int total = 0; for(int i = 0; i < items.size(); i++) total += cost[items[i] -'0' - 1]; return total; } int main() { int cost[] = {10, 15, 10, 5}; string items = "14214331"; cout<< solve(cost, items); return 0; }
Input
{10, 15, 10, 5}, "14214331"
Output
75
- Related Articles
- Find out the minimum number of coins required to pay total amount in C++
- Program to find total amount of money we have in bank in Python
- C++ Program to find out the maximum amount of money that can be made from selling cars
- Program to find how many total amount of rain we can catch in Python
- C++ Program to find out the total price
- C++ code to find out number of battery combos
- C++ code to find out the sum of the special matrix elements
- C++ code to find total number of digits in special numbers
- C++ code to find total elements in a matrix
- How to Find out the source code of a transaction in SAP?
- C++ code to find total time to pull the box by rabbit
- Program to Find Out the Amount of Rain to be Caught between the Valleys in C++
- C++ Program to find out the least amount of money required to subscribe to the OTT services
- C++ program to find out the number of coordinate pairs that can be made
- C++ code to find out which number can be greater

Advertisements