C++/C++

Advertisement


Question
i need help in C++...im taking the C++ course at university..some stuff i know..but sometimes i have no clue about whats going on..the prof's website is http://www.me.concordia.ca/~dolat/courses.htm
..im working on assignment 4 posted there..theres 2 errors in my code..i cant tell how fix them..any help would be really appreciated..how can i be as good as u in C++..this is the most interesting course i have...
my code is:

#include <iostream> /*Standard library for i/o functions.*/
//That's a library with mathematical functions.

using namespace std; //This line allows simple use of the standard library.

float function1(float)
float function2(float)

void main() /*This statement declares the main function.*/


{/*Start of the program code.*/


float i,temp,y,z;
int n;

temp=0;
n=0;


cin>>i;
while(z!=0)
{

  y=i+(function1(i)/function2(i));
  z=temp-y;
  i=y;
  temp=y;
  n=n+1;
}


cout<<"temp is: "<<temp<<endl;
cout<<"i is: "<<i<<endl;
cout<<"z is: "<<z<<endl;
cout<<"y is: "<<y<<endl;
cout<<"n is: "<<n<<endl;



float function1(float i)

{

float result1;

result1=-4821+ 4821*((2.7)^(-i/703)) +i;
return result1;

}

float function2(float i)
{
  float result2;
  
result2=-6.85*( (2.7)^(-i/703))  + 1;
return result2;
}


}//end of program


Answer
Hello alan, thank you for the question.

The first problem that stands out to me is that in your while loop condition, you are using the variable z. Z has not been initialized to a value the first time this loop is executed. Z is undefined. Different compilers handle things like that differently. I know on Linux, typically it will take an unitialized variable like that and set it equal to 0, which would make your loop never execute. On Windows it typically sets the value to some huge negative number, usually around 2 billion. Another potential problem I see off the bat is this statement:

y=i+(function1(i)/function2(i));

If the expression function2(i) happens to just somehow come out to be 0, your application will crash and burn. Dividing by 0 is a huge no-no. You could simply check for that in an if statement:

if(function2(i)== 0)
cout << "Cannot divide by 0";
else
y=i+(function1(i)/function2(i));

I haven't had enough time yet to actually see what the goal of your assignment is, so I'm not sure what the code is actually supposed to do. If either one of those instances I pointed out happens to be your problem, then great. If not, feel free to ask me another question and I'll be more than happy to sit down and go over the assignment doc.

Oh, and for your remark, becoming a good programmer really just takes time. Once you've been doing it for a long time, it starts to become a lot easier on your brain. I had problems understanding when I started out myself.

If you have any other questions, please do not hesitate to ask.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.