Count

In this program I need to count the amount zeroes but I think I got it but every time I run it, it goes into a infinite loop. What am I doing wrong?

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
  const int MAXSIZE =100;
/* Function Prototypes */
void readdata(int [], int);
int countzeros(int[], int);
void append(int [], int);
int main()
{
int number[MAXSIZE];
int a, n;
cout<<"Enter the number of elements in the array: "<<endl;

 readdata(number, n);



cout << "Enter a number into the array: ";
readdata(number, n);


cout << endl << "Original Data" << endl;
append(number,n);


cout<< "The amount of zeroes in this array are: "<<endl;
countzeros(number, a);
return 0;
}

/* Function to print an array */
void append(int vals[], int n)
{
for (int i = 0; i < n; i++)
cout << vals[i] << endl;
return;
}



void readdata(int vals[], int n)
{

cin>>n;
for(int i=0; i<n; i++)

{
    cin>>vals[i];
    cout<<vals[i]<<endl;
}





return ;
}

int countzeros(int vals[], int n)
{
  int  count=0;
for (int position = 0; position < n; position++)
if (vals[position] == 0)
count++;
return count;
}
Inside your countzeros function your position variable never changes its value so it will never be greater than n.


I get what you're saying, but being a little slow as I am today, I have no idea what to do.
I was wrong about that, it does change, I was the slow one, below is what I think is wrong.


remove line 12 and 42, put line 42 in line 12.

I think in this statement the second parameter should be 'n', not 'a':

 
countzeros(number, a);



in line 25 replace with this:

 
cout << countzeros(number, n);


Last edited on
I did it. But it doesn't show how many zeroes are in the array in fact, it just says "The amount of zeroes in this array are: " and that's it. I want to be able to show the amount of zeroes that I put in the array.
You are calling the function but you aren't actually printing out the value it returns, change it to something like this.

cout<< "The amount of zeroes in this array are: " << countzeros(number, a) <<endl;

You are also passing it the variable 'a' which doesn't ever seem to be assigned to anything.
here it shows how many zeros the user inputs.
did you replace the line 25?
Topic archived. No new replies allowed.