Предмет: Информатика,
автор: alinakotik213
Где ошибка в коде С++ ? Помогите, пжл.
Задание: Динамически создать массив размера n (n вводится с клавиатуры) и заполнить его случайными числами из диапазона [a, b] (a, b вводятся с клавиатуры). Заполнение массива реализовать с помощью функции.
Вычислить количество элементов массива, лежащих в диапазоне от number_А до number_В (number_А и number_В вводятся с клавиатуры), с помощью функции.
#include
#pragma hdrstop
#include
#include
#include
using namespace std;
void z(int *m, int n, int a, int b)
{
srand(time(NULL));
for(int i=0;i< n;i++)m[i]=a+rand()%(b-a+1);}
int k(int *m, int n, int a, int b)
{
int s=0; for(int i=0;i< n;i++)s+=m[i]>=a&&m[i]<=b;
return s;
}
int main()
{
int n,a,b,na,nb; cout<<" Vvedite razmer massiva n: "; cin>>n;
int *m=new int[n];
cout<<"Vvedite diapazon ot a do b: "; cin>>a>>b;
z(m,n,a,b);
for(int i=0;i< n;i++)cout<
cout<< endl;
cout<<"number_A number_B: ";
cin>>na>>nb; cout<<"k="<< k(m,n,na,nb);
delete []m; cout<<"\nPress Enter for exit\n";
cin.get();
cin.get();}
Считает разность между А и Б, а не количество элементов
MrMaks1096:
не возможно что бы считало разность
Ответы
Автор ответа:
0
#include <iostream>
#include <ctime>
using namespace std;
void FillArray( int *mainArray, int arraySize,
int leftRandBorder, int rightRandBorder )
{
srand(time(NULL));
for (int i = 0; i < arraySize; i++)
mainArray[i] = leftRandBorder + rand() % (rightRandBorder - leftRandBorder + 1);
}
int SearchArray( int *mainArray, int arraySize, int number_A, int number_B )
{
int res = 0;
for (int i = 0; i < arraySize; i++)
res += mainArray[i] >= number_A && mainArray[i] <= number_B;
return res;
}
int main()
{
int
*mainArray,
arraySize;
cout << "Enter the array size: ";
cin >> arraySize;
mainArray = new int[arraySize];
int
leftRandBorder,
rightRandBorder;
cout << "Enter the rage from 'a' to 'b': ";
cin >> leftRandBorder >> rightRandBorder;
FillArray(mainArray, arraySize, leftRandBorder, rightRandBorder);
int
number_A,
number_B;
cout << "Enter the 'number_A' and the 'number_B': ";
cin >> number_A >> number_B;
cout << "Result =
<< SearchArray(mainArray, arraySize, number_A, number_B)
<< endl;
delete[] mainArray;
cout << "\nPress 'Enter' for exit\n";
cin.get();
return 0;
}
#include <ctime>
using namespace std;
void FillArray( int *mainArray, int arraySize,
int leftRandBorder, int rightRandBorder )
{
srand(time(NULL));
for (int i = 0; i < arraySize; i++)
mainArray[i] = leftRandBorder + rand() % (rightRandBorder - leftRandBorder + 1);
}
int SearchArray( int *mainArray, int arraySize, int number_A, int number_B )
{
int res = 0;
for (int i = 0; i < arraySize; i++)
res += mainArray[i] >= number_A && mainArray[i] <= number_B;
return res;
}
int main()
{
int
*mainArray,
arraySize;
cout << "Enter the array size: ";
cin >> arraySize;
mainArray = new int[arraySize];
int
leftRandBorder,
rightRandBorder;
cout << "Enter the rage from 'a' to 'b': ";
cin >> leftRandBorder >> rightRandBorder;
FillArray(mainArray, arraySize, leftRandBorder, rightRandBorder);
int
number_A,
number_B;
cout << "Enter the 'number_A' and the 'number_B': ";
cin >> number_A >> number_B;
cout << "Result =
<< SearchArray(mainArray, arraySize, number_A, number_B)
<< endl;
delete[] mainArray;
cout << "\nPress 'Enter' for exit\n";
cin.get();
return 0;
}
Похожие вопросы
Предмет: Литература,
автор: kanatovadinara108
Предмет: Українська мова,
автор: virhenkoy
Предмет: Литература,
автор: rybchaklyudmila25048
Предмет: Физика,
автор: adelosss