1) Дан массив а [7,7]. Необходимо найти количество пар элементов, расположенных ниже основной диагонали.
2) Дан одномерный массив А (10), состоящий из целых чисел. Вам нужно найти среднее арифметическое этих элементов массива.
3) Дан массив А (20). Необходимо найти и распечатать минимальный и максимальный элемент массива.
C/C++
3 Задачки решить на с++
2)
#include <valarray>
#include <iostream>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
valarray<int> box(10);
for (auto& x : box) x = uid(gen);
for (auto x : box) cout << x << ' ';
puts("");
const auto avg = double(box.sum()) / box.size();
cout.setf(ios::fixed);
cout.precision(1U);
cout << "Average: " << avg << '\n';
}
3)
#include <valarray>
#include <iostream>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
valarray<int> box(10);
for (auto& x : box) x = uid(gen);
for (auto x : box) cout << x << ' ';
puts("");
cout << "Min: " << box.min() << '\n';
cout << "Max: " << box.max() << '\n';
}
#include <valarray>
#include <iostream>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
valarray<int> box(10);
for (auto& x : box) x = uid(gen);
for (auto x : box) cout << x << ' ';
puts("");
const auto avg = double(box.sum()) / box.size();
cout.setf(ios::fixed);
cout.precision(1U);
cout << "Average: " << avg << '\n';
}
3)
#include <valarray>
#include <iostream>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
valarray<int> box(10);
for (auto& x : box) x = uid(gen);
for (auto x : box) cout << x << ' ';
puts("");
cout << "Min: " << box.min() << '\n';
cout << "Max: " << box.max() << '\n';
}
1) что за "пары элементов", непонятно
2)
#include "ctime"
#include "cstdlib"
#include "iostream"
#include "algorithm"
using namespace std;
int main(){
int a[10]; srand(time(NULL));
for(int &i:a)cout<<(i=rand()%198-99)<<' '; cout<< endl;
cout<<"average="<< accumulate(a,a+10,0)/10.<< endl;}
3)
#include "ctime"
#include "cstdlib"
#include "iostream"
#include "algorithm"
using namespace std;
int main(){
const int n=20; int a[n]; srand(time(NULL));
for(int &i:a)cout<<(i=rand()%198-99)<<' '; cout<< endl;
cout<<"min="<<*min_element(a,a+n)<<", max="<<*max_element(a,a+n)<< endl;}
2)
#include "ctime"
#include "cstdlib"
#include "iostream"
#include "algorithm"
using namespace std;
int main(){
int a[10]; srand(time(NULL));
for(int &i:a)cout<<(i=rand()%198-99)<<' '; cout<< endl;
cout<<"average="<< accumulate(a,a+10,0)/10.<< endl;}
3)
#include "ctime"
#include "cstdlib"
#include "iostream"
#include "algorithm"
using namespace std;
int main(){
const int n=20; int a[n]; srand(time(NULL));
for(int &i:a)cout<<(i=rand()%198-99)<<' '; cout<< endl;
cout<<"min="<<*min_element(a,a+n)<<", max="<<*max_element(a,a+n)<< endl;}
1)
#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
int main()
{
vector < int > prep;
srand(time(NULL));
int mas[7][7];
for (int i = 0; i < 49; i++) *(mas[0] + i) = rand() % 21;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) cout << mas[i][j] << "\t";
cout << endl;
}
for (int i = 1; i < 7; i++)
for (int j = 0; j < i; j++) prep.push_back(mas[i][j]);
sort(prep.begin(), prep.end());
int count = 0;
for (int i = 1; i < prep.size(); i++) if (prep[i] == prep[i - 1]) {
count++; cout << "pair: " << prep[i] << " " << prep[i - 1] << endl;
i++;
}
cout <<"\n\n" << count;
}
Никогда не пишите так)
#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
int main()
{
vector < int > prep;
srand(time(NULL));
int mas[7][7];
for (int i = 0; i < 49; i++) *(mas[0] + i) = rand() % 21;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) cout << mas[i][j] << "\t";
cout << endl;
}
for (int i = 1; i < 7; i++)
for (int j = 0; j < i; j++) prep.push_back(mas[i][j]);
sort(prep.begin(), prep.end());
int count = 0;
for (int i = 1; i < prep.size(); i++) if (prep[i] == prep[i - 1]) {
count++; cout << "pair: " << prep[i] << " " << prep[i - 1] << endl;
i++;
}
cout <<"\n\n" << count;
}
Никогда не пишите так)
Гаврил Дмитриев
Сергей, а почему < iostream > ? Попробуйте набирать без пробелов! Всё же прекрасно набирается:
#include <iostream>
#include <iostream>
Похожие вопросы
- Задачка на программирование . С++
- Есть идеи с задачкой? С++
- Задачка на программирование С++
- Помогите решить задачку пжпж условие на фото. Код необязателен, главное идею подскажите пжпж
- Недавно начали изучать масивы по с++,они пока что даются мне с трудом, поэтому прошу вас помочь решить эту задачку.
- Помогите решить задачку по программированию
- Помогите пожалуйста решить задачки c++
- Помогите решить задачки C++
- Помогите решить задачку c++.
- Помогите пожалуйста решить задачку по C++, напишите полный текст кода пожалуйста
✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿ ✿
А я вот смотрю -что-то у Вас всегда "iostream" да "algorithm". И вон у Сергея Степанова тоже почему-то
#include < iostream >
Нормально же сейчас пишется:
#include <iostream>
Это раньше надо было извращаться, а теперь уже не надо, дефект исправлен!