HELP ASAP: Finding odd numbers in array

I have to display the number of odd numbers in my array. So the user inputs 2 index's from 1 to 14 and the function in my code outputs the number of odd numbers in that array.

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
 int numberOfOddsInARange(int arrayA[], int size, int lowIndex, int highIndex) //function(argument) with parameters inside. Also passing an array inside a function. Starting index = low, ending index = high
{
	int sumOfOddNumbers = 0;
	int odd = arrayA[lowIndex]; //Minimum set to arrayA[low] because your starting in the beggining 
	if (lowIndex < 0 || highIndex >= size || lowIndex > highIndex) //Basically checks whether the array is out of bound, which results into an invalid input  
	{
		cout << "Invalid range";
		return -1;
	}
	for (int a = lowIndex + 1; a++;) //loop that checks every index in the array
	{ 
		if (arrayA[a] == 1) //if statement to check if the number is an odd number
		{
			sumOfOddNumbers += arrayA[a]; //Adds 1 if the number is an odd number
		}
	}
	return sumOfOddNumbers;
}

int main()
{
	srand(static_cast<int> (time(0))); //to generate random number
	int size = 15;
	int arrRand[15] = { 0 };
	int lowIndex = 0;
	int highIndex = 0;


	for (int i = 0; i < 15; i++)
	{
		arrRand[i] = 50 + rand() % (100 - 55 + 1); //min + [rand() % (max - min + 1)] ----- for loop to populate array size of 15 with random numbers
	}

	int i = 0;
	while (i < 15)
	{
		cout << arrRand[i] << ' '; //While lopp displaying Array
		i++;
	}


	cout << endl;
	cout << "Enter a minimum and maximum Index: ";
	cin >> lowIndex >> highIndex; //user inputting the there choice of lowest and highest index between 15
	cout << numberOfOddsInARange(arrRand, size, lowIndex, highIndex); //calling and outputting the function above 

	return 0;
}
closed account (18hRX9L8)
You need to check if a number is divisible by two. You can use the modulus operator for that. See the example on this page: http://www.cprogramming.com/tutorial/modulus.html
here is my new submitted code with the modules operator but its not giving me the right number. Please someone help me quick, I am very short on time.

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
#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int numberOfOddsInARange(int arrayA[], int size, int lowIndex, int highIndex) //function(argument) with parameters inside. Also passing an array inside a function. Starting index = low, ending index = high
{
	int sumOfOddNumbers = 0;
	int odd = arrayA[lowIndex]; //Minimum set to arrayA[low] because your starting in the beggining 
	if (lowIndex < 0 || highIndex >= size || lowIndex > highIndex) //Basically checks whether the array is out of bound, which results into an invalid input  
	{
		cout << "Invalid range";
		return -1;
	}
	for (int a = lowIndex + 1; a < size; a++) //loop that checks every index in the array
	{ 
		if (arrayA[a] % 2 != 0) //if statement to check if the number is an odd number
		{
			sumOfOddNumbers = sumOfOddNumbers + 1; //Adds 1 if the number is an odd number
		}
	}
	return sumOfOddNumbers;
}

int main()
{
	srand(static_cast<int> (time(0))); //to generate random number
	int size = 15;
	int arrRand[15] = { 0 };
	int lowIndex = 0;
	int highIndex = 0;


	for (int i = 0; i < 15; i++)
	{
		arrRand[i] = 50 + rand() % (100 - 55 + 1); //min + [rand() % (max - min + 1)] ----- for loop to populate array size of 15 with random numbers
	}

	int i = 0;
	while (i < 15)
	{
		cout << arrRand[i] << ' '; //While lopp displaying Array
		i++;
	}


	cout << endl;
	cout << "Enter a minimum and maximum Index: ";
	cin >> lowIndex >> highIndex; //user inputting the there choice of lowest and highest index between 15
	cout << numberOfOddsInARange(arrRand, size, lowIndex, highIndex); //calling and outputting the function above 

	return 0;
}
Line 18:
int a = lowIndex + 1
is meant to be
int a = lowIndex - 1
as arrays are zero-indexed, meaning the 1st element is actually at index 0, the 2nd at index 1, etc.

a < size
Might want to reconsider this. (highIndex)

Line 22:
sumOfOddNumbers = sumOfOddNumbers + 1;
can be shortened to
sumOfOddNumbers++;
The name is misleading though. Might want to rename it to something like noOfOddNumbers.

for (int i = 0; i < 15; i++)
It's better to write
for (int i = 0; i < size; i++)
instead of using a magic number. Likewise with line 43.
Similarly, when using constants be sure to declare them as const.

1
2
int lowIndex = 0;
	int highIndex = 0;

You can declare variables of the same type on the same line.
int lowIndex = 0, highIndex = 0;

cin >> lowIndex >> highIndex;
What if the user enters a string?
Last edited on
Sweet.Thank you so much!
Topic archived. No new replies allowed.