using a subprogram sub

Feb 28, 2012 at 4:58pm
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 Feb 28, 2012 at 4:59pm
Feb 28, 2012 at 5:21pm
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?

Feb 28, 2012 at 5:41pm
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 Feb 28, 2012 at 5:42pm
Feb 28, 2012 at 5:44pm
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 Feb 28, 2012 at 5:44pm
Feb 28, 2012 at 5:56pm
@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.
Feb 28, 2012 at 6:01pm
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++;
}
Feb 28, 2012 at 6:01pm
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.
Feb 28, 2012 at 6:06pm
@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 Feb 28, 2012 at 6:06pm
Feb 28, 2012 at 6:08pm
so, only add a &&v[i]==a into the if-clause.
Feb 28, 2012 at 6:14pm
call it count_if
if any two consecutive numbers entered are equal,
If you do use count_if you will not actually use it.
Feb 28, 2012 at 6:22pm
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.
Feb 28, 2012 at 6:58pm
no problem
Feb 29, 2012 at 1:09pm
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 Feb 29, 2012 at 1:09pm
Feb 29, 2012 at 3:10pm
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.