using a subprogram sub

Hi. I have another BAC problem that left me with no ideas, the last one on the paper.

So there is this subprogram sub which has 3 parameters:
-an array, int v[100]
-int n<=100 (number of elements from the array)
-int a

sub returns the number of elements in the array that are equal with a

I had to write sub, and then a program in which you cin>>n and also cin>>(n numbers) and using sub, it must display "YES" if any two consecutive numbers entered are equal, or display "NO" in the other case. Here is what I did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int sub (int v[],int n, int a)
{ int i, c=0;
    for(i=1;i<=n;i++)
    if(v[i]==a)
    c++;       //c is the counter, any time the program finds an element from the array equal to a, one unit is added to the counter
    return c;
}


main ()
{
    int v[100],n,i;
    cin>>n;
    for(i=1;i<=n;i++)
    cin>>v[i];
    if(sub(v[i],n,v[i+1])>0)   //HERE IS THE PROBLEM!!!
    cout<<"\n NO";
    else
    cout<<"\n YES";
}


I marked the spot where I get the error. I know what's wrong in sub(v[i],n,v[i+1]), but I wrote that anyway to show you the idea that would work for doing what the exercise asks, it's just not the right form.
Last edited on
subprogram
C++ calls them functions. They are not programs, or subprograms.

main ()
This is wrong. It should be int main(). If your compiler doesn't complain, get a new one (for free).

As it is, you've chosen a particularly ugly way of solving this problem. Are you forced to solve it this way?

Indexing of elements in array starts from zero. So if your array is declared as

int v[100];

then to access elements of the array you should use indexes from 0 to 99.

So your function shall be defined as

1
2
3
4
5
6
7
8
int sub ( const int v[], int n, int a )
{
   int count = 0;

   for ( int i = 0; i< n; i++ ) if ( v[i] == a ) count ++;

   return ( count );
}


To call it use the syntax

(instead of your if(sub(v[i],n,v[i+1])>0) )

if ( sub( v, n, v[0] ) >0 )

This is if you are counting elements equal to the first element of the array.


Last edited on
I'm using codeblocks (mingw). As for subprograms, I've translated directly from my native language, I'll use functions from now on.
Using sub function is necessary.
Last edited on
@vlad It doesn't work. The problem with your version is that a=v[0] always. I must compare each two consecutive numbers: v[0] with v[1], v[1] with v[2], ... , v[n-1] with v[n]. If all 2 consecutive numbers are distinct then I must cout<<"YES". Your program couts<<"NO" no matter what I enter.
so I guess you have to use
1
2
3
4
5
for(int i=0;i<n;i++) 
{
    if(i+1<n&&v[i]==v[i+1])
        count++;
}
Even if I'd use if(sub(v,n,v[i+1])>0) it wouldn't matter, because when sub is called, i is initialized with 0 in that for(). (or at least I think so).

Anyway, I see that writing this makes the program work in some cases.
@FlashDrive I can't do that, in the text it is specified that sub function must be constructed so that it returns how many elements from the array are equal to a. If I'd do it your way, the initial purpose would be lost.
Last edited on
so, only add a &&v[i]==a into the if-clause.
call it count_if
if any two consecutive numbers entered are equal,
If you do use count_if you will not actually use it.
I'm sorry, I really can't do these little tricks. In the end, this is just a paper exam, so my program would only be on paper, and if my program isn't exactly as the exercise requests (only use sub function written on point a) and a main function) I won't get any points for it (even if it would run correctly). I'll ask my teacher tomorrow, she should have experience solving these kind of exam problems. Thank you for your efforts.
no problem
Problem solved.

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
#include <iostream>
using namespace std;

int sub (int v[],int n, int a)
{ int i, c=0;
    for(i=1;i<=n;i++)
    if(v[i]==a)
    c++;       //c is the counter, any time the program finds an element from the array equal to a, one unit is added to the counter
    return c;
}


main ()
{
    int v[100],n,i,ok=0;
    cin>>n;
    for(i=1;i<=n;i++)
    cin>>v[i];

    for(i=2;i<=n;i++)
    if(sub(v,i-1,v[i])) 
    ok=1;
    if(ok==0)
    cout<<"\n DA";
    else
    cout<<"\n NO";
}
Last edited on
it must display "YES" if any two consecutive numbers entered are equal, or display "NO" in the other case
You are not checking for consecutive elements there, just for repeated.
I suppose that 'DA' is 'yes', in that case your condition is reversed

main must return int
Indent your code
Topic archived. No new replies allowed.