help

Still kind of new at this, but I am trying to write code to do a few different things. I would like it to count odd and evens, display the prime numbers,and I want it to find the largest integer, all from a randomized array. I tried and I think something is amiss. Just not sure what. Any help would be great.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;

bool prime(int);

void find_largest(int[], const int &s, int &);

void count_odds_evens(int[], const int&, int&, int&);

void randomize_array(int[], const int &);

void print_array(int[], const int &);

void find_largest(int n[], const int &s, int &largest)

{
	int x = 0;
	int y = 0;

	for (int i = 0; i < 100; i++)
	{
		cin >> x;
		if (x > y)
			y = x;
	}
	
	cout << "The biggest number is:" << y << endl;
	
	
}

bool prime(int n)
{
	if (1 == n)
		return false;
	for (int i = 2; i <= sqrt(double(n)); i++)
		if (n%i == 0)
			return false;
	return true;
}

void count_odds_evens(int n[], const int &s, int &num_even, int &num_odds)
{
	int even_count = 0;
	int	odd_count = 0;
	int integer;

	while (true) {
		cout << "Please enter an integer\n";
		cin >> integer;
		if (integer == 0) break; // break outa here 
								 // determine if integer is even or odd 
		if (integer % 2 == 0)
			even_count++;
		else
			odd_count++;
	} // end of while loop 
	
	cout << "Number of even elements = " << even_count;
	cout << "Number of odd elements = " << odd_count << endl;
}

void randomize_array(int n[], const int &s)
{
	srand(unsigned(time(0))); // seed the random number generator
	for (int i = 0; i<s; i++)
		n[i] = rand() % 100 + 1;
}

void print_array(int n[], const int &s)
{
	int spaces = 0, i = 0;

	while (i < s)
	{
		cout << setw(4) << n[i] << " ";
		spaces += 6;

		if (spaces % 66 == 0)
			cout << endl;
		i++;
	} // while

	cout << endl;
}
Start debugging and see what is the problem,for exmple find_largest is undefined.
You don't have a main function? maybe start there?
Line 46: Why are you passing num_even and num_odd by reference? You never use them in count_odds_evens().

Line 24: Avoid the use of hard coded constants. You should be passing in the size of the array as an argument. Presumably, that's s, but you never use it.

General comment: Why are you passing arguments by reference when you don't update the values in the caller? It's not illegal, however, ints are best passed by value if you're not going to change the value in the caller (which you can't because of the const.

Last edited on
Thank you all, I will keep working on it. I appreciate it.
Topic archived. No new replies allowed.