-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42CountOddNumberinArray.cpp
73 lines (54 loc) · 1.29 KB
/
42CountOddNumberinArray.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstdlib>
#include <iostream>
using namespace std;
// Function to add an element to an array
// Function to generate a random number within a specified range
int RandomNumber(int from, int to) { return rand() % (to - from + 1) + from; }
// Function to fill an array with random numbers
void FillArrayWithRandomNumbers(int arr[], int &length)
{
cout << "Enter the number of elements in the array: ";
cin >> length;
if (length > 100)
{ // Check array bounds
cout << "Maximum array size is 100. Please enter a smaller value." << endl;
length = 100; // Set length to maximum allowed
}
for (int i = 0; i < length; i++)
{
arr[i] = RandomNumber(1, 100);
}
}
// Function to print an array
void PrintArray(int arr[], int length)
{
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
// Function to copy one array to another
int OddCount(int arr[100], int length)
{
short count = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] % 2 != 0)
{
count++;
}
}
return count;
}
// Main function
int main()
{
srand((unsigned)time(NULL));
int arr[100], length = 0;
FillArrayWithRandomNumbers(arr, length);
PrintArray(arr, length);
int NumbersOdd = OddCount(arr, length);
cout << NumbersOdd << endl;
return 0;
}