Here are collected the task to the article Data types, variables and constants. Try to solve problems on their own and only need to look at the decision. So you will learn much faster programming fundamentals.
1. Declare variables with which you can calculate the total amount of the purchase of several products. For example a chocolate bar, coffee and packets of milk.
View code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intchocolate=2;// хранит количество упаковок
intmilk=3;
intcoffee=1;
floatpriceOfChocolate=11.04;// хранит цены за одну упаковку
2. Declare three variables of type int, and the first to assign a numeric value, the second variable is the first variable increased by 3, and the third variable is equal to the sum of the first two.
View code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
usingnamespacestd;
intmain()
{
intfirst=4;
intsecond=first+3;
intthird=first+second;
cout<<"first = "<<first<<endl;
cout<<"second = "<<second<<endl;
cout<<"third = "<<third<<endl;
return0;
}
3. Declare variables, to count the total number of objects for the table. for example cups, the same number of saucers and spoons.
View code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intcups=6;// количество чашек
intspoons=cups;// количество ложек равно кол-тву чашек
intsaucers=cups;// блюдца
// или можно так записать
// int cups, spoons, saucers;
// cups = spoons = saucers = 6;
intamount=cups+spoons+saucers;// общее количество
cout<<"Всего "<<amount<<" предметов"<<endl;
return0;
}
4.7
155
126 thoughts on “Tasks: Data types, variables and constants”
Thank you for the opportunity to practice. It really helps in the realization of the material!
In the second assignment error, The second variable is not equal to the first increase in three, and it is issued on the same with the following expression…
ahahahah :D I am currently in a second complicated zadachu.I wrote instead 15 lines – 30 :D little protupila,but overall I left everything well.
The decision on the first task. What are the good and bad side is, in my decision. newcomer,severely beat. (Rows not Russian because he wrote in Dev`e,not at home).
#include #include
using std::cout; using std::cin; using std::endl;
int choko=0; int coffee=0; int milk=0; int tov=0; float sumch=0; float sumcof=0; float summilk=0; float sttov=0;
int tovari(); float stoimost(); float summa();
int main() { tovari(); stoimost(); summa();
system("PAUSE"); return 0; }
int tovari() { cout<<"--------------------------------------------"<<"\n";
Formulation of the task: Write program calculating circuit resistance elektriche¬skoy, consisting of two parallel-connected resistors. The recommended screen view during execution of the program (data, user Submitted, bolded). Calculation of the electrical circuit in parallel connection of resistance elements. Enter the raw data: The magnitude of the first resistance (ohm) -> 15 The magnitude of the second resistance (ohm)-> 20 circuit resistance: 8.57 ohm Developing solutions algorithm.
chart
Defining the program
L - value of the first resistance (ohm), a float variable G - a second resistance value (ohm), a float variable R - resistance of the circuit (ohm), a float variable
The full text of the program //---------------------------------------------------------------------------
The first line of the input file are two integers a and b (-109 ≤ a, b ≤ 109). Your program needs to display a single number - the result of the expression a – b.
Input data: Two integers a and b (-109 ≤ a,b ≤ 109).
Thank you for the opportunity to practice.
It really helps in the realization of the material!
Rada, that you've found useful information for you on our website! stay with us!
In the second assignment error, The second variable is not equal to the first increase in three, and it is issued on the same with the following expression…
ahahahah :D
I am currently in a second complicated zadachu.I wrote instead 15 lines – 30 :D
little protupila,but overall I left everything well.
everything is good but not enough, I want more tasks and with the ever-increasing level of complexity! :)
I enrolled in the second problem a little differently, regarding display, and in general all the same
#include
using namespace std;
int main (int x, int y, int z)
{
x = 15;
y = x * 3;
z = x + y;
cout << x << "\n" << y << "\n"<< z << "\n";
system("pause");
return 0;
}
and the third reason I also thought, you must itself determine the number of devices, and this turned out at me like this
#include
#include
using namespace std;
int total;
void totalfunc();
int main ()
{
setlocale(LC_CTYPE, "rus");
totalfunc();
system("pause");
return 0;
}
void totalfunc()
{
int cups, spoon, saucer;
cout < < cups; cout << spoon; cout << saucer; total = cups + spoon + saucer; cout << "Сумма предметов на столе равна: "; cout << total; }
The decision on the first task. What are the good and bad side is, in my decision. newcomer,severely beat. (Rows not Russian because he wrote in Dev`e,not at home).
#include
#include
using std::cout;
using std::cin;
using std::endl;
int choko=0;
int coffee=0;
int milk=0;
int tov=0;
float sumch=0;
float sumcof=0;
float summilk=0;
float sttov=0;
int tovari();
float stoimost();
float summa();
int main()
{
tovari();
stoimost();
summa();
system("PAUSE");
return 0;
}
int tovari()
{
cout<<"--------------------------------------------"<<"\n";
cout<<"Vvedite kol-vo kuplennix productov \n";
cout<<"--------------------------------------------"<<"\n";
cout<<choko;
cout<<coffee;
cout<<milk;
tov=choko+coffee+milk;
cout<<"--------------------------------------------"<<"\n";
cout<<"Obshee kol-vo kuplennix tovarov= "<<tov<<"\n";
cout<<"--------------------------------------------"<<"\n";
return 0;
}
float stoimost()
{
cout<<"--------------------------------------------"<<"\n";
cout<<"Vvedite stoimost kuplennix productov \n";
cout<<"--------------------------------------------"<<"\n";
cout<<sumch;
cout<<sumcof;
cout<<summilk;
return 0;
}
float summa()
{
cout<<"--------------------------------------------"<<"\n";
cout<<"Stoimost pokupok \n";
cout<<"--------------------------------------------"<<"\n";
cout<<"--------------------------------------------"<<"\n";
sumch*=choko;
cout<<"Stoimost plitok shokolada= "<<sumch<<"\n";
sumcof*=coffee;
cout<<"Stoimost banok cofe= "<<sumcof<<"\n";
summilk*=milk;
cout<<"Stoimost paketov moloka= "<<summilk<<"\n";
cout<<"--------------------------------------------"<<"\n";
sttov=sumch+sumcof+summilk;
cout<<"Obshya summa k oplate= "<<sttov<<"\n";
cout<<"--------------------------------------------"<<"\n";
return 0;
}
As the dofiga code and really useless.
using namespace std;
int main() {
int milk, chocolate, coffe;
float milkcost = 10.1;
float chocolatecost = 11.85;
float coffecost = 15.4;
cout << milk;
cout << chocolate;
cout << coffe;
cout << "You buy " << milk << " botles of milk " << milkcost << " * " << milk << " = " << (milk * milkcost) << endl;
cout << "You buy " << chocolate << " chocolates " << chocolatecost << " * " << chocolate << (chocolate * chocolatecost) << endl;
cout << "You buy " << coffe << " coffe's " << coffecost << " * " << coffe << (coffe * coffecost) << endl;
cout << "Total: " << (milk * milkcost) + (chocolate * chocolatecost) + (coffe * coffecost)
return 0;
system ("pause");
};
Formulation of the task:
Write program calculating circuit resistance elektriche¬skoy, consisting of two parallel-connected resistors. The recommended screen view during execution of the program (data, user Submitted, bolded).
Calculation of the electrical circuit in parallel connection of resistance elements.
Enter the raw data:
The magnitude of the first resistance (ohm) -> 15
The magnitude of the second resistance (ohm)-> 20
circuit resistance: 8.57 ohm
Developing solutions algorithm.
chart
Defining the program
L - value of the first resistance (ohm), a float variable
G - a second resistance value (ohm), a float variable
R - resistance of the circuit (ohm), a float variable
The full text of the program
//---------------------------------------------------------------------------
#include
#include
void main ()
{
float L, G, R;
printf("velichina pervogo soprotivlenia (Om) : ") ;
scanf("%f", &L) ;
printf("velichina vtorogo soprotivlenia (Om) : ") ;
scanf("%f", &G) ;
R=L*G/(L+G);
printf("\n soprotivlenie cepi %f Om",R);
scanf("%f", &L);
}
//---------------------------------------------------------------------------
The results of the work program
When the program on the screen, such results have been removed:
The magnitude of the first resistance: 15
The magnitude of the second resistance 20
circuit resistance 8,57
The first line of the input file are two integers a and b (-109 ≤ a, b ≤ 109). Your program needs to display a single number - the result of the expression a – b.
Input data:
Two integers a and b (-109 ≤ a,b ≤ 109).
Output:
the result of the expression a – b.