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”
#include
int main() { using namespace std;
int cups; int spoon; int saucers;
cout << "Введите кол-во чашек: " <> cups;
spoon = saucers = cups;
float sum;
sum = (cups + spoon + saucers);
cout << "Общее кол-во предметов: " << sum << endl;
#include
int main() {
using namespace std;
int cups;
int spoon;
int saucers;
cout << "Введите кол-во чашек: " <> cups;
spoon = saucers = cups;
float sum;
sum = (cups + spoon + saucers);
cout << "Общее кол-во предметов: " << sum << endl;
return 0;
}
#include
using std::cout;
void fast_sort (int * arr, int first, int last){
if (first < last){
int l_ = first;
int _r = last;
int midl = arr[(first + last) / 2];
do{
while (arr[l_] midl)
_r–;
if (l_ <= _r){
int point = arr[l_];
arr[l_] = arr[_r];
arr[_r] = point;
l_++;
_r–;
}
}while (l_ <= _r);
if (first l_)
fast_sort(arr, l_, last);
}
}
int main(){
int zxc[] = {123, 23, 444, 213, 111, 44, -9};
fast_sort(zxc, 0, (sizeof(zxc) / 4)-1);
for (int i : zxc)
cout << i << " ";
}
comment on the comment! !!!
#include
using std::cout;
void fast_sort (int * arr, int first, int last){
if (first < last){
int l_ = first;
int _r = last;
int midl = arr[(first + last) / 2];
do{
while (arr[l_] midl)
_r–;
if (l_ <= _r){
int point = arr[l_];
arr[l_] = arr[_r];
arr[_r] = point;
l_++;
_r–;
}
}while (l_ <= _r);
if (first l_)
fast_sort(arr, l_, last);
}
}
4 spaces don't work either