For partial code

I am having trouble finding what is wrong in this partial code, can an

Page 1 of 1

11 Replies - 529 Views - Last Post: 12 December 2009 - 01:47 AM Rate Topic: -----

#1 cpenbutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-October 09

For partial code

Posted 12 December 2009 - 12:36 AM

I am having trouble finding the error in this partial code, can anybody help me please?
int Y, X ,I, Z;
Z=1;
X=0;
FOR(I=0, I<=5, I=I+1)
{Y=X*I + I*I;
cout<<Y<<X<<I;
X=X+Z;
}

Is This A Good Question/Topic? 0
  • +

Replies To: For partial code

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: For partial code

Posted 12 December 2009 - 12:41 AM

Welcome to DIC!

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are, then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: For partial code

Posted 12 December 2009 - 12:46 AM

Without knowing what you are trying to do, & how this code is failing, it's not possible to help you. Please provide the information Janotte has requested.
Was This Post Helpful? 0
  • +
  • -

#4 cpenbutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-October 09

Re: For partial code

Posted 12 December 2009 - 12:50 AM

View Postjanotte, on 11 Dec, 2009 - 11:41 PM, said:

Welcome to DIC!

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are, then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.

Well actually this is part of an assignment where I am just supposed to find a error in this piece of code. It can be logical or syntax. The only thin I can see is that I=I+1 could be replaced by i+1. Other than that, am I missing something? Any advice would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: For partial code

Posted 12 December 2009 - 01:07 AM

View Postcpenbutt, on 12 Dec, 2009 - 01:50 AM, said:

The only thin I can see is that I=I+1 could be replaced by i+1.

i+1 would simply perform i+1. The output would not be assigned back into the variable. i=i+1 would assign the value of the integer i, incremented by one into the variable i.

int main(void) {
  int i=0;
  printf("%i\n",i+1);
  // i still equal zero, but the number one prints
  i=i+1;
  printf("%i\n",i);
  // the number one prints, because it's the value inside of i
  return 0;
}



Make sense?
Was This Post Helpful? 0
  • +
  • -

#6 cpenbutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-October 09

Re: For partial code

Posted 12 December 2009 - 01:14 AM

View Postno2pencil, on 12 Dec, 2009 - 12:07 AM, said:

View Postcpenbutt, on 12 Dec, 2009 - 01:50 AM, said:

The only thin I can see is that I=I+1 could be replaced by i+1.

i+1 would simply perform i+1. The output would not be assigned back into the variable. i=i+1 would assign the value of the integer i, incremented by one into the variable i.

int main(void) {
  int i=0;
  printf("%i\n",i+1);
  // i still equal zero, but the number one prints
  i=i+1;
  printf("%i\n",i);
  // the number one prints, because it's the value inside of i
  return 0;
}



Make sense?

Yes that makes sense. Thank you
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: For partial code

Posted 12 December 2009 - 01:21 AM

Now we can talk about how you don't need to reply with the entire text that I posted, since yours is directly after mine it's obvious what you are replying to, & there is no need to include it :)

View Postcpenbutt, on 12 Dec, 2009 - 01:50 AM, said:

I am just supposed to find a error in this piece of code.


& again, we need a little bit of detail.

Y=X*I + I*I;

The above looks right to me. But depending on what value you are expected to get form this, there is a little pain in the ass called order of operations.
Was This Post Helpful? 0
  • +
  • -

#8 cpenbutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-October 09

Re: For partial code

Posted 12 December 2009 - 01:28 AM

Thats the problem I am having. Thats all I was given. That little code and told there is an error. I am learning c++ in an engineering class. Its hard to see an error when you really dont know where the program is going/
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: For partial code

Posted 12 December 2009 - 01:34 AM

View Postcpenbutt, on 12 Dec, 2009 - 02:28 AM, said:

Its hard to see an error when you really dont know where the program is going/

You truly are preaching to the quire :)

I wish I could help you more, but I don't see anything wrong with that code, other than the spacing is atrocious & the variables are in caps.

Neither of those would cause an error, but it's read like a train wreck.
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: For partial code

Posted 12 December 2009 - 01:38 AM

Have a look at the for() loop.

Is there a difference between "," and ";" in C/C++ syntax?

Is that important here?
Was This Post Helpful? 1
  • +
  • -

#11 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: For partial code

Posted 12 December 2009 - 01:39 AM

Ahhhh, nice catch Janotte!!!
Was This Post Helpful? 0
  • +
  • -

#12 cpenbutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-October 09

Re: For partial code

Posted 12 December 2009 - 01:47 AM

Oh my, its always something simple and takes forever to find! Thanks guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1