3 Replies - 829 Views - Last Post: 01 April 2011 - 10:37 PM Rate Topic: -----

#1 hannahyancy   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 25-March 11

I need to run this program and the answer is supposed to be 7..

Posted 01 April 2011 - 09:25 PM

I need to run this program and the answer is supposed to be7 but its coming out to be 5 and the code isn't working..
please help! thanks!

F(int z)
{
  int x = 2;
  z +=x;
}

int main()
{
  int z = 5;
  F(z);
  cout << "z = " << z << endl;
  return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: I need to run this program and the answer is supposed to be 7..

#2 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: I need to run this program and the answer is supposed to be 7..

Posted 01 April 2011 - 10:01 PM

your function isnt correct
I am not sure how it even runs...

try this
void F(int &z)
{
  int x = 2;
  z +=x;

}



Was This Post Helpful? 1
  • +
  • -

#3 sbm2097   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-March 11

Re: I need to run this program and the answer is supposed to be 7..

Posted 01 April 2011 - 10:09 PM

d code u posted is with some errors,
so m correcting the code

1.the function F must return some value to the main function,
so that the value gets updated.

2.use F(z) in cout.

3.use getch() to hold the output.

F(int z)
{
  int x = 2;
  z +=x;
  return z;
}

int main()
{
  int z = 5;
  F(z);
  cout << "z = " << F(z) << endl;
  getch();
}


[/quote]



check this out

it will surely works.
Was This Post Helpful? 0
  • +
  • -

#4 TMKCodes   User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: I need to run this program and the answer is supposed to be 7..

Posted 01 April 2011 - 10:37 PM

@smb2097 well it does not help to replace return statement with getch and every one does not need the getch eitherway. His function is incorrect because he either does not return the added value or he is thinking of using pointer/reference stuff and does not know how, but @snoopy11 fixed that.

Snooby added the missing function type and decided it to be void because the function is not returning anything and made the z to be passed as reference, which will make it possible to edit directly the value of z which has been declared in the main function.

Another thing @smb2097 your new code is calling F twice which us unnecessary.

#include <iostream>
void F(int &z)
{
  int x = 2;
  z +=x;
}

int main()
{
  int z = 5;
  F(z);
  cout << "z = " << z << endl;
  return 0;
}



or without pointer/reference stuff like @smb2097 suggested

#include <iostream>
int F(int z)
{
  int x = 2;
  return z + x;
}

int main()
{
  int z = 5;
  cout << "z = " << F(z) << endl;
  return 0;
}


This post has been edited by TMKCodes: 01 April 2011 - 10:39 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1