Showing posts with label Commented Codes. Show all posts
Showing posts with label Commented Codes. Show all posts

C++ Array Exercises

Using your knowledge so far (basic, loops, conditional tests, functions and mainly arrays), solve the following exercises in C ++ and put your code in the comments.

C ++ Array exercises

01. Write a program that will receive and store 10 numbers in an array. Then, you must create two functions that will find the highest and lowest value of that array.

02. You were hired by a TV station to make C++ software to work with rain data in your city. Your 7-position array should store how much it rained each day. Then, you must say which day it rained more, the day it rained less, how much it rained the week and the average, to show on TV.

03. Create an array of 10 positions, to store the average grades of 10 students. Your program must then go to each grade and check if the student has passed (grade higher than 7), failed (grade less than 5) or will be left for recovery (grade between 5 and 7).

04. Create a lottery number generator program. That is, create an array of 6 positions and in each position draw a number from 1 to 60. Oh, your numbers cannot be repeated.


05. Make a program in which you will supply 10 numbers for an array, including repeated values. Your code must search this array to create a new array that does not have repeated numbers.


06. Program the tic-tac-toe, in C++. Two players on your computer must be able to play. Each player says a line and a number, on the 3x3 board, to play (can only play in free spots on the board). At the end of each game, it must say who won or drew, as well as the score of the game, and whether they want to play another game or close the program.
Tic-Tac-Toe in C++


07. On an NxN board (you choose N), the computer must draw N positions on that board, without the user knowing. These positions are the ships locations. Now you must make attempt (shots in the water) to try to hit the ships. With each shot you take, the program should tell you whether you hit or miss, and if you miss, it should tell you how many ships are there around that point.



More exercises, with solutions, on C ++ arrays:
https://www.w3resource.com/cpp-exercises/array/index.php

+30 C++ Loop Exercise

Congratulations on completing the C++ Repetition Statements section, famous loops, one of the most important topics in any programming language.

Now is the time to put your knowledge into practice, this is where you will evolve, become a real programmer.

Solve the exercises, try, try, try again and again, before you see the solution, combined?
Just reading or watching videos will never make you a programmer, even the worser.

It is in the race, trying, struggling, breaking one's head, sweeping nights, and crying in a fetal position that one forges a true C++ Progressive programmer.

Left? Say goodbye to friends, girlfriend or boyfriend, family, social networks ... time to hide from society and exercise!

Ah ... and post there in the comments your solutions!

WHILE, DO WHILE, and FOR exercises in C++

0. Make a C++ program that asks the user for an integer, and displays their multiplication table.
Multiplication table with FOR, WHILE and DO WHILE

1. Make a program that takes two integers and generates the integers that fall within their range.

2. Make a program that asks for a grade between zero and ten. Show a message if the value is invalid and keep asking until the user enters a valid value.

3. Make a program that prints numbers 1 to 20 on the screen, one below the other. Then modify the program so that it shows the numbers next to each other.

4. Write programs that display the following patterns on the screen according to the number you provide, which will always be the number of lines:
(Solutions in e-book)
4.1
* 
** 
*** 
**** 
*****

4.2
1
12 
123 
1234 
12345

4.3
1                                                                                                             
22                                                                                                      
333                                                                                                         
4444                                                                                                         
55555 


4.4
1                                                                                  
2 3                                                                                                         
4 5 6                                                                                                       
7 8 9 10 


4.5
       1                                                           
      2 3                                                           
     4 5 6                                                         
    7 8 9 10

4.6
        *                                                           
       * *                                                         
      * * *                                                         
     * * * *                                                       
    * * * * *

4.7
1                                                                      
01                                                                     
101                                                                    
0101                                                                   
10101

4.8
    *                                                                  
   ***                                                                 
  *****                                                                
 *******                                                               
*********                                                              
 *******                                                               
  *****                                                                
   ***                                                                 
    *


4.9
          1                                                            
        1   1                                                          
      1   2   1                                                        
    1   3   3   1                                                      
  1   4   6   4   1 

4.10                                                                      
1   1                                                                  
1   2   1                                                              
1   3   3   1                                                          
1   4   6   4   1                                                      
1   5   10   10   5   1                                                
1   6   15   20   15   6   1

4.11
12345                                                                                                         
2345                                                                                                          
345                                                                                                           
45                                                                                                            
5

4.12
12345                                                                                                         
 1234                                                                                                         
  123                                                                                                         
   12                                                                                                         
    1

4.13
5 4 3 2 1                                                              
4 3 2 1                                                                
3 2 1                                                                  
2 1                                                                    
1

4.14
    1                                                                  
   21                                                                  
  321                                                                  
 4321                                                                  
54321

4.15
     1234567654321                                                                                            
      12345654321                                                                                             
       123454321                                                                                              
        1234321                                                                                               
         12321                                                                                                
          121                                                                                                 
           1

5. Make a program that reads 5 numbers and reports the largest number.

6. Make a program that reads 5 numbers and gives the sum and average of the numbers.

7. Make a program that calculates the show the arithmetic average of N grades.

8. Make a program that prints only odd numbers between 1 and 50 on the screen. At the end, also show the sum of the numbers.

9. Create a program that asks the user for a number and calculates the summation to that value.

10. Create a program that asks the user for a number, and calculates its factorial. Ex .: 5! = 5.4.3.2.1 = 120
Summation and Factorial

11. The Fibonacci series is formed by the sequence 0,1,1,2,3,5,8,13,21,34,55, ... where the next term is always the sum of the previous two. Make a program capable of generating the series to the nth term that the user must supply.
Fibonacci in C++ with loopings

12. Make a program that asks for two numbers, base and exponent, calculate and display the first number raised to the second number. Do not use the power function of language.
Exponentiation with loopings in C++

13. Make a program that asks for an integer and determines whether or not it is a prime number. A prime number is one that is divisible only by itself and by 1.

14. Make a program that shows all primes between 1 and N where N is a user-supplied integer.
Prime numbers with C++

15. Make a program that displays all possible Powerball game combinations.
Powerball lottery in C++

16. Program software that receives a user number and tells you if it is a perfect number or not. Search Google for a perfect number.

17. Program software that receives two integers from the user, and tell them the greatest common divisor of these numbers.

18. Program a software that receives a number less than 1000, and say the value of the unit, the dozen, and the hundred digits.

19. Program software that calculates the sum of the digits of a number.

20. Make a program that shows the following Series terms:

  S = 1/1 + 2/3 + 3/5 + 4/7 + 5/9 + ... + n / m.

Print at the end the sum of the series.


21. Let the harmonic series H = 1 + 1/2 + 1/3 + 1/4 + ... + 1 / N, Make a program that calculates the value of H with N terms, where N is input by the user.

22. Make a program that shows the following terms of the Series:

  S = 1/1 + 2/3 + 3/5 + 4/7 + 5/9 + ... + n / m.

Print at the end the sum of the series.


23. The value of PI can be approximated by the following infinite sequence:
Infinite series to calculate pi
Create a program that calculates the value of this series with 10 terms, then 100 terms, and finally using a thousand terms. What values did you get?

Simple C++ Calculator: How to Program

To wrap up our C++ Getting Started section, let's make a very simple, but very useful and interesting program: a calculator.

Before you see the code, try to solve it.

It must receive two user numbers, and display the operations of:
  • Sum
  • Subtraction
  • Multiplication
  • Division of the first by the second
  • Rest of the division of the first by the second
  • Percentage of the first relative to the second
  • Arithmetic average

Your program should look like:
How to make create a simple calculator in C++


Try it!

How to make a calculator in C++

See how our code looked:
#include <iostream>
using namespace std;

int main()
{
    float num1, num2;

    //Receiving data
    cout << "First number   : ";
    cin >> num1;

    cout << "Second number  : ";
    cin >> num2;

    //Displaying operations
    cout << "Sum            : " << num1 + num2 << endl;
    cout << "Subtraction    : " << num1 - num2 << endl;
    cout << "Multiplication : " << num1 * num2 << endl;
    cout << "Division       : " << num1 / num2 << endl;
    cout << "Modulus        : " << (int)num1 % (int)num2 << endl;
    cout << "Percentage     : " << 100.0*(num1/num2) << endl;
    cout << "Average        : " << (num1 + num2)/2 << endl;


    return 0;
}
Yours, how was it? Different?
Put it in the comments!

See that the only thing different was the excerpt:
(int) num1% (int) num2

This is called casting, that is, since the module operator (rest of division) only makes sense with integer values and our variables are float, we put (int) before the variables to tell C ++ that we want to handle those variables, at that time, as integers.

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.

How to calculate Percentage in C++

Now, let's learn how to use our basic C++ knowledge to make really useful programs. First, let's look at how to calculate the most diverse types of percentages.

Percentage in C++


  • "Make a program that calculates 12% of 2112"
The name gives us a clue: percentage ... per cent ... per hundred
That is, we must divide by 100.

12 divided by 100 is 0.12, so 12% of 2100 is:
0.12 * 2112

#include <iostream>
using namespace std;

int main()
{
    cout << "12% of 2112= " << 0.12*2112;
    return 0;
}


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

#include <iostream>
using namespace std;

int main()
{
    float num;
    cout << "Type a value: ";
    cin >> num;
    cout << "12% of "<< num <<" = "
         << 0.12 * num;
    return 0;
}

  • "Make a program that gets a percentage value from the user, and calculates how much that represents from a second value that he typed. For example, if he types 4 and 400, his program should calculate how much is 4% of 400"

#include <iostream>
using namespace std;

int main()
{
    float percentage, num;
    cout << "Percentage value: ";
    cin >> percentage;
    percentage = percentage/100;

    cout << "Second value: ";
    cin >> num;

    cout << percentage <<"% of "
         << num << " = "<< percentage*num;
    return 0;
}

Note the user will enter a value such as 12 to calculate 12%.
But in the calculation, we do not use 12 but 0.12 (this value by percent, that is, this value divided by 100).

So, we see this news:
percentage = percentage / 100;

A priori, it sounds strange, but we are just dividing the percentage variable by 100.
This means: the new value of percentage is equal to the old value divided by 100.

You could have done this by declaring another aux variable:
aux = percentage / 100;
percentage = aux;

But then I'd allocate memory for nothing, better do what we did in the code, it's simpler, faster and more efficient (yes, programmers are lazy, always want to write as little code as possible).

Percentages in C++


  • "Program one 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 "

#include <iostream>
using namespace std;

int main()
{
    float first, second;
    cout << "First number: ";
    cin >> first;

    cout << "Second number: ";
    cin >> second;

    cout << first <<"% of "
         << second << " = "<< (first/second)*100.0;
    return 0;
}

Here, the only thing different is to remember to multiply by 100 to display the result as a percentage.

How to calculate percentage in C++


  • "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 taxes). "

#include <iostream>
using namespace std;

int main()
{
    float salary;
    cout << "Type your salary: ";
    cin >> salary;

    cout << "Gross salary: $ " << salary << endl;
    cout << "7% from taxes: $ " << 0.07*salary << endl;
    cout << "Net salary: $ " << 0.93*salary << endl;

    return 0;
}

If it is discounted 7%, it means that you get 93% of gross salary, which is the net.

How to make percentage in C++


  • "Due to inflation, every year your salary must 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 the previous salary, the increase due to inflation and the new salary. "

The salary increase corresponds to the previous value multiplied by 0.025 (2.5%).
The new salary is the previous one plus this 2.5%: 1 + 0.025 = 1.025 * (previous salary).

Look:
#include <iostream>
using namespace std;

int main()
{
    float salary;
    cout << "What's your salary: ";
    cin >> salary;

    cout << "Previous salario: $ " << salary << endl;
    cout << "2.5% increase: $ " << 0.025*salary << endl;
    cout << "New salary: $ " << 1.025*salary << endl;

    return 0;
}

And take percentage

"In the city of C++land there is a tolerance of 15% of the speed limit,to not to get a fine. 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."

If the maximum speed of the road is speed, then the maximum speed with tolerance not to be fined is: 100% + 15% = 115% = 1.15 * speed

#include <iostream>
using namespace std;

int main()
{
    float speed;
    cout << "Maximum speed: ";
    cin >> speed;

    cout << "Maximum speed: " << speed << endl;
    cout << "15% tolerance: " << 0.15*speed << endl;
    cout << "Maximum speed without penalty: " << 1.15*speed << endl;

    return 0;
}