BFMracing

General Category => General Board => Homework Haven => Topic started by: jim360 on November 07, 2009, 01:27:15 PM

Title: Does anyone know how to use C++?
Post by: jim360 on November 07, 2009, 01:27:15 PM
Which is, if you didn't know already, a computer programming language. I've got to write a few programs in it and would appreciate any help that can be given. Thanks.
Title: Re: Does anything know how to use C++?
Post by: BFM_Kiwi on November 07, 2009, 01:44:02 PM

I know C and C#.   Know a bit about C++.   You can PM me with specific questions if you like.  I'm no expert on C++ though.   
Title: Re: Does anything know how to use C++?
Post by: Miser on November 07, 2009, 07:39:59 PM
No but i can do a binary solo!!!

0000001 00000011 000000111 00001111
Title: Re: Does anything know how to use C++?
Post by: fLipSIDe on November 07, 2009, 07:44:24 PM
C++ you hack people using that format...well some of us used too :ssh:

Regards,
~fLipSIDe




║▌│█│║▌║││█║▌ ║▌║
© σяιgιиαl ρяσfιlє ®
Title: Re: Does anything know how to use C++?
Post by: Goalie on November 08, 2009, 01:30:09 PM
I took a beginner's course in C++.  I could try and help, however I took that course 2 years ago and I might not remember everything.
Title: Re: Does anything know how to use C++?
Post by: McSkittles on November 08, 2009, 01:40:20 PM
My dad is a pro in C++ and other older languages such as that. So just PM me any questions you have and I should be able to get back to you :haw:
Title: Re: Does anything know how to use C++?
Post by: fLipSIDe on November 08, 2009, 02:03:09 PM
My dad is a pro in C++ and other older languages such as that.

*The Next Day...BFM's background and the text formats are skittles* :liljawdrop:
Title: Re: Does anything know how to use C++?
Post by: jim360 on November 10, 2009, 04:23:35 AM
Well thanks guys! IF I need help I'll knwo where to go first!!

Here's the first "program" I wrote ...


// Program to print out a silly message if the user's lucky enough to pick three numbers with
// the third number being the difference of the first two, and to tell them they failed if this third number... isn't.

#include <iostream>
using namespace std;

int main()
{
  int i, n, m;
  cout << "please type in an integer:" << endl;
  cin >> i;
  cout << endl;
 cout << "Please type in a number greater than your first choice:" << endl;
  cin >> n;
  cout << endl;
 cout << "Please type in any other number between these two values" << endl;
  cin >> m;
  cout << endl;
  if (i>n) {
    cout << "Try again! you should have the first number less than the second number!!" << endl;
    cout << "Please run the program again" << endl; }
  else {
    if (m>=n) {
      cout << "Try again! Make sure the third number is between the first two!!" << endl;
      cout << "Please run the program again!" << endl;}
    else { if (m<=i) {
      cout << "Try again! Make sure the third number is between the first two!!" << endl;
      cout << "Please run the program again!" << endl;}
      else {
    if (m==n-i) { for (int j=1; j<=n; j++) {
      cout << j << "WOW!!!! that was lucky!! You picked m to be the difference between the first two numbers!!" << endl;
    }
} else {
 for (int j=1; j<=n; j++) {
      cout << j << " oh dear! you lose" << endl;
       }
   }
 }
}
}
}
Title: Re: Does anything know how to use C++?
Post by: BFM_dStruct on November 10, 2009, 04:26:12 AM
ah much to complicated for me. I would probs be able to write the code to do the same thing in PHP though :P
Title: Re: Does anyone know how to use C++?
Post by: BFM_Kiwi on November 10, 2009, 11:04:07 AM
A couple of things:

1)  I would not recommend you use variable names like i, m, n, unless they have well-understood meanings.  This helps in looking at the program to remember what "m" is.   So maybe "term1", "term2" and "difference".   Or you could have:

int expected = term1 - term2
int actual   /* (input from user) */

if ( expected == actual ) ...            <-- very obvious what that checks

rather than  ( if m==i-n )                <-- not so obvious

2) are you expecting only positive integers?  If so, say that.  Otherwise, some of your assumptions are wrong (that "m" is between the other two numbers)

3)  if (i>n)  - if you require the second number to be greater than the first, then you need  if (i >= n)

4) If you want you could combine the next checks to something like  if ( m <= i || m >= n )

5) I don't understand the "for" loops.  That will result in printing the final message (win or lose) a bunch of times.  If they win, and their numbers were   3, 7 and 4, then it will print it 7 times  (for j = 1 through 7 ).   Was that your intention?

Otherwise good!


Title: Re: Does anyone know how to use C++?
Post by: jim360 on November 10, 2009, 12:24:05 PM
A few replies:

1) 5 things isn't a couple :P.

2) Yes, it was meant to print out the message loads of times. A bit boring really but it's just a program to get me used to very basic syntax and the for, if, else commands. I like the idea of comparing the expected to actual since it might cut down on the number of for-loops needed.

3) The program's already been "handed in" so it's too late to make these changes but it's nice that there is a way to combine the checks - cuts down on the text as well as removing an "else" section.

4) I think the "if i>n" bit might be coverd by the m needing to be less than n but greater than i". Okay, I just thought of this, but it's probably true.

5) It's a first attempt so it's nice to get some feedback - I'll be writing 5 more of these other the next four months or so.
Title: Re: Does anyone know how to use C++?
Post by: jim360 on January 18, 2010, 04:05:25 PM
Hey again!!

As I was posting this I was trying to solve the problem of why this program didn't do what it was supposed to. I've now managed to fix it, so instead I'm posting the program as a curio.

To explain a little of what's going on, the program is trying to solve specifically cubic equations (with an x3 term in them) using the Newton-Raphson method. I've done this by defining the necessary functions at the bottom and using them in the main for-loop. The program returns the equation and a single root. By picking cubic equations I can guarantee at least one real root and since cubic curves are nice I should always get a reasonable answer.



// Program that will find an approximate solution to a cubic equation with coefficients defined by the user.
// The method used is the Newton-Raphson method, as this avoids the need to know where exactly the root lies.

#include <iostream>
#include <cmath>

using namespace std;

float Function(float number1, float number2, float number3, float constant, float root) ;
float Derivative(float number1, float number2, float number3, float root);

int main()
{
  float number1, number2, number3, constant, guess, iteration, newguess, root;
 
  cout << "This program will help find a root of a user-defined cubic equation." << endl;
  cout << "Please enter four coefficients for the equation." << endl << "Include a single space between each number, and hit enter only after inputting all the numbers:    ";
  cin >> number1 >> number2 >> number3 >> constant;
  cout << endl;
  cout << "Please enter an initial guess and the number of iterations to perform. " << endl ;
  cout << "Include a single space between the numbers and don't hit enter until you've input both numbers:   " ;
  cin >> guess >> iteration ;
 
  newguess=0;
  for (int i=1; i<=iteration; i++) {
    newguess = guess - (Function(number1, number2, number3, constant, guess))/(Derivative(number1, number2, number3, guess)) ;
    guess = newguess;
  }
  root = guess;   
  cout << "There is a root of the equation " << number1 << "*x^3 + " << number2 << "*x^2 + " << number3 << "*x + " << constant << " at x = " << root << "." << endl;
 
  return 0;
}



// Definitions of functions used

float Function(float number1, float number2, float number3, float constant, float root)
{
  float function;

  function =  number1*root*root*root + number2*root*root + number3*root + constant ;
 return function ;
}

float Derivative(float number1, float number2, float number3, float root)
{
  float derivative ;

  derivative = 3*number1*root*root + 2*number2*root + number1 ;

  return derivative ;
}
Title: Re: Does anyone know how to use C++?
Post by: BFM_Edison on January 18, 2010, 06:57:03 PM
I need to learn C++ at some point. Is there no else-if command like there is in Java (as in not a if within the else, but together).
Title: Re: Does anyone know how to use C++?
Post by: jim360 on January 18, 2010, 07:26:29 PM
There is one of those, there's also a switch-case function.

I used the (if, else if) function in an ealier version of that program that was using interval bisection to find square roots.