Есть ответ 👍

решить задачи по программированию.Язык c++

207
218
Посмотреть ответы 2

Ответы на вопрос:


Відповідь:

1.

#include <iostream>

#include <cstdlib>

#include <time.h>

using namespace std;

void randarr(int *A, int N){

for(int i = 0; i < N; i++){

 A[i] = rand() % 10 + 1;

}

}

void printarr(int *A, int N){

for(int i = 0; i < N; i++){

 cout << A[i] << " ";

}

}

void sortarr(int *A, int N){

for(int i = 0; i < N; ++i){

 int smallest = i;

 for(int j = i + 1; j < N; ++j){

  if(A[j] < A[smallest]){

   smallest = j;

  }

 }

 swap(A[i] , A[smallest]);

}

}

int main(){

srand(time(NULL));

setlocale(LC_ALL, "Rus");

int N;

cout << "Введите кол-во елементов массива: ";

cin >> N;

int *A = new int[N];

randarr(A,N);

printarr(A,N);

sortarr(A,N);

cout << "\n";

printarr(A,N);

delete[] A;

return 0;

}

2.

#include <iostream>

#include <cstdlib>

#include <time.h>

using namespace std;

void randarr(int *B, int N){

for(int i = 0; i < N; i++){

 B[i] = rand() % 10 + 1;

}

}

void printarr(int *B, int N){

for(int i = 0; i < N; i++){

 cout << B[i] << " ";

}

}

void sortarr(int *B, int N){

 int twoelements;

 for(int j = 0; j < N - 1; j++){

  twoelements = j;

 }

swap(B[0] , B[twoelements]);

}

int main(){

srand(time(NULL));

setlocale(LC_ALL, "Rus");

int N;

cout << "Введите кол-во елементов массива: ";

cin >> N;

int *B = new int[N];

randarr(B,N);

printarr(B,N);

sortarr(B,N);

cout << endl;

printarr(B,N);

delete[] B;

return 0;

}

3.

#include <iostream>

#include <cstdlib>

#include <time.h>

using namespace std;

void randarr(int *A, int N){

for(int i = 0; i < N; i++){

 A[i] = rand() % 10 + 1;

}

}

void printarr(int *A, int N){

for(int i = 0; i < N; i++){

 cout << A[i] << " ";

}

}

void twoarr(int *A, int *B, int N){

for(int i = 0; i < N; i++){

 B[i] = A[i] * (-1);

}

}

void sortarrtobig(int *A, int N){

for(int i = 0; i < N; ++i){

 int smallest = i;

 for(int j = i + 1; j < N; ++j){

  if(A[j] < A[smallest]){

   smallest = j;

  }

 }

 swap(A[i] , A[smallest]);

}

}

void sortarrtosmall(int *A, int N){

for(int i = 0; i < N; ++i){

 int smallest = i;

 for(int j = i + 1; j < N; ++j){

  if(A[j] > A[smallest]){

   smallest = j;

  }

 }

 swap(A[i] , A[smallest]);

}

}

int main(){

srand(time(NULL));

setlocale(LC_ALL, "Rus");

int N;

cout << "Введите кол-во елементов массива: ";

cin >> N;

int *A = new int[N];

int *B = new int[N];

randarr(A,N);

cout << "Первый массив: ";

printarr(A,N);

cout << "\nВторой массив: ";

twoarr(A,B,N);

printarr(B,N);

cout << "\nСортируем первый массив по убыванию" << endl;

sortarrtosmall(A,N);

cout << "Первый массив: ";

printarr(A,N);

cout << "\nСортируем второй массив по возрастанию" << endl;

sortarrtobig(B,N);

cout << "Второй массив: ";    

printarr(B,N);

delete[] A;

delete[] B;

return 0;

}


PascalABC.NET 3.6.2

begin

 var func : real -> real := k ->(k + 1)/(k**2 + 1);

 var s: real := 0;

 var n := readinteger();

 for var i := 1 to n do s += func(i);

 s.Println;

end.

input/output:

>>> 9

>>> 3.13448882499098

Популярно: Информатика