Showing posts with label Average. Show all posts
Showing posts with label Average. Show all posts

How to Calculate C ++ Average: Arithmetic and Weighted

Now that we've learned how to receive user data with the cin command and Math operations, let's use the knowledge of these tutorials to learn how to calculate C ++ arithmetic and weighted averages by solving questions from the C++ basic exercise list.

Simple Arithmetic Average in C++

The most basic average of all is the so-called simple arithmetic, where you basically add up all the terms and divide by the total of terms.

To average two numbers:
(a + b) / 2

From three numbers:
(a + b + c) / 3

From four numbers:
(a + b + c + d) / 4

From n numbers:
(a + b + c ...) / n

Simple Arithmetic Average Exercises

  • "Create a program that asks the user for two grades, and returns their average."

Our code is:

#include <iostream>
using namespace std;

int main()
{
    float grade1, grade2, average;

    cout << "Grade 1: ";
    cin >> grade1;

    cout << "Grade 2: ";
    cin >> grade2;

    average = (grade1+grade2)/2;

    cout << "Average: " << average;

    return 0;
}

Note that while it is a very simple formula, it is good practice to put the sum within parentheses to avoid making mistakes like:
a + b/2

(In this case, we would be adding a with b/2)

  • "Do the same as the previous exercise, but for 3 grades."

Our code:

#include <iostream>
using namespace std;

int main()
{
    float grade1, grade2, 
          grade3, average;

    cout << "Grade 1: ";
    cin >> grade1;

    cout << "Grade 2: ";
    cin >> grade2;
    
    cout << "Grade 3: ";
    cin >> grade3;

    average = (grade1+grade2+grade3)/3;

    cout << "Average: " << average;

    return 0;
}

Weighted Average with C++

In the arithmetic average, all terms have the same 'weight', that is, they contribute equally to the final value of the average.

In the weighted, each term has a weight, see the formula:
How to calculate average in C++

The terms are x1, x2, x3, ...
The respective weights are p1, p2, p3, ...
In this case, summing all terms multiplied each by their weight, and we divide by the sum of the weights.

  • "The MIT entrance exam has a weight of 3 for Mathematics, 2.5 for Physics, 2.5 for Chemistry, 1.0 for Portuguese and also 1.0 for English. Create a system that asks for user grades and returns their average."

Look our code:

#include <iostream>
using namespace std;

int main()
{
    float math, phy, chem,
          port, eng, average;

    cout << "Math grade ";
    cin >> math;

    cout << "Physics grade: ";
    cin >> phy;

    cout << "Chemistry grade: ";
    cin >> chem;

    cout << "Portuguese grade: ";
    cin >> port;

    cout << "English grade: ";
    cin >> eng;

    average = (3*math + 2.5*phy +
             2.5*chem+port+eng) / 10;

    cout << "Average: " << average;

    return 0;
}

Note that the sum of the weights is 10.

Average Exercises in C++

Solve the exercises below and post your solutions in the comments.

01. Solve the previous exercises, now without using the average variable.

02. Make a program that receives the amount of gallons a person has fueled in the car and the amount of miles they have traveled with that fuel, then calculate the average (ie how many miles per kilometer) they make.

03. Make a program that asks for the size of a downloadable file (in MB) and the speed of an Internet link (in Mbps), calculate and enter the approximate file download time using this link (in minutes).

04. A new super-economical car model has been launched.
He makes 20 km with 1 liter of fuel.
Each liter of fuel costs $ 5.00.

Make a program that asks the user how much money they have, and then tells how many gallons of fuel they can buy and how many miles the car can handle on that much fuel.
Your script will be used on the car's onboard computer.

C++ Basic Exercises

Congratulations, you have completed the basic part of our C++ Course.
Now let's practice.

Try with all your heart, with all your calm, effort and concentration, to do the exercises below.
Leave a comment with your solutions, ok?

C++ questions

01. Write a program that asks for the radius of a circle, and then displays the circle's perimeter and area.

02. Make a program that takes the radius of a sphere and calculates its volume.

03. Make a program that receives an integer, represented a value in years. Show how many days this time interval has, assuming a year has 365 days.

04. Make a program that receives two variables: the value of hours and minutes. Then convert everything to minutes and also to seconds.

05. Create a program that receives the temperature in degrees Celsius and converts it to Farenheit.

06. Do the opposite of the previous exercises.
Conversion between Celsius and Fahrenheit

07. Make a program that asks for the current year and your age, then displays your year of birth.

Percentage Exercises in C++

08. Make a program that calculates 12% of 2112

09. Make a program that receives a user value and calculates 12% of that total.

10. Make a program that receives a percentage value from the user, and calculates how much it represents from a second value that he typed.

11. Program software that receives two numbers, where the first must be less than the second.
It then calculates the percentage that the first represents from the second.
For example, if you entered 12 and 21, that means 12 represents 57.14% of 21

12. You have become a C ++ programmer, and now you are earning super good. But you will still have to pay taxes.
Create software that receives your salary and calculates the 7% income tax.

The output of your program should be gross salary (no rebate), the amount of tax you will pay and your net salary (after deducting IR).

13. Due to inflation, every year your salary should be adjusted based on it.
For example, if inflation was 2.5% then your salary should grow by the same amount so as not to lose value.
Create a C ++ program that asks for a person's salary, last year's inflation, and apply that inflation. Show previous salary, inflation increase, and new salary.

14. In the city of C ++ lland there is a tolerance of 15% of the speed limit so as not to be fined. Make a program that asks the user for the maximum speed of a road and calculates how fast the car can travel without being fined. Your code will be embedded in the car's GPS system to warn the speed limit the car must travel.
Solutions: Percentage in C++

Average exercises in C++

15. Create a program that asks the user for two grades, and returns their average.

16. Do the same as the previous exercise, but for 3 grades.

17. The MIT entrance exam weights are 3 for Mathematics, 2.5 for Physics, 2.5 for Chemistry, 1.0 for Foreign language and 1.0 for English. Create a system that asks for user grades and returns their average.