#returning wrong output

20 messages · Page 1 of 1 (latest)

visual tartan
#

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main(){
int i;
int a[3];
int b[3];
for (i=0; i<=2; i++ ) {
cin>>a[i];
cin>>b[i];
}
int q = 0;
int w = 0;
for (i=0; i<=2; i++ ) {

if (a[i]>b[i]) {
   q++;
}
else if (a[i]<b[i]) {
   w++;
}
else {
q=q;
w=w;
}
}
cout<<q<<" "<<w;
return 0;

}

scenic turtleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

boreal zenith
#

Not only a VLA.
But a VLA using an uninitialised length variable.

#

I think you were meant to declare them as:

int a[3];
int b[3];
visual tartan
#

still wrong answer

boreal zenith
#
for (i=0; i<=2; i++ ) {
    cin>>a[i];
    cin>>b[i];
}

You are alternating between a and b here

If you enter on the command line "5 6 7" then:
a[0] becomes 5
b[0] becomes 6
a [1] becomes 7

You should simplify this to:

cin >> a[0] >> a[1] >> a[2];
cin >> b[0] >> b[1] >> b[2];

Or you can loop twice. First one for just a.
Second time for just b.

visual tartan
#

Thanks @boreal zenith

#

!solved

scenic turtleBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] returning wrong output

boreal zenith
#

Suggestion (points for style):
rename variable q to alice.
rename variable w to bob.

Up to you.

#

And this:

else {
    q=q;
    w=w;
}

Is pointless code.
You could just delete this without worry.

visual tartan
#

okk buddy

#

actually i am still learning basics

#

i thought it is necessary to write else statement in a if else loop

#

thank you for helping

visual tartan
boreal zenith
#
if (x)
{
}
else if (y)
{
}

Is perfectly fine.

visual tartan