Print 

Author Topic: Does anyone know how to use C++?  (Read 4109 times)

Offline jim360

  • Posts Too Much
  • *****
  • Posts: 6847
Does anyone know how to use C++?
« 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.
« Last Edit: November 10, 2009, 04:26:03 AM by BFM_three60 »
Check out my Short introduction... corner and my "Historical figures who should perhaps be better-known" thread!!

Exciting videos: 1.1 / 1.2 / 2 / 3 / 4 / 5 / 6



              

Offline BFM_Kiwi

  • Major
  • *
  • Posts: 9174
Re: Does anything know how to use C++?
« Reply #1 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.   

Offline Miser

  • Senior Poster
  • ****
  • Posts: 851
  • This is Jimmy
Re: Does anything know how to use C++?
« Reply #2 on: November 07, 2009, 07:39:59 PM »
No but i can do a binary solo!!!

0000001 00000011 000000111 00001111

Offline fLipSIDe

  • Regular Poster
  • ***
  • Posts: 351
  • Hmm...wat u lookin' at mate!!??
    • Drum & Bass / Breakbeat / Dubstep Arena
Re: Does anything know how to use C++?
« Reply #3 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є ®
Drum & Bass / Breakbeat / Dubstep / Grime Arena - http://download.breakbeat.co.uk/



Thankyou to Arya for this Sig

Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
Re: Does anything know how to use C++?
« Reply #4 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.
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Offline McSkittles

  • Senior Poster
  • ****
  • Posts: 717
Re: Does anything know how to use C++?
« Reply #5 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:

^^ Done by me(freshly updated :))

^^ Done by SPiDEY

^^ Big thanks to bl!Tz'

Offline fLipSIDe

  • Regular Poster
  • ***
  • Posts: 351
  • Hmm...wat u lookin' at mate!!??
    • Drum & Bass / Breakbeat / Dubstep Arena
Re: Does anything know how to use C++?
« Reply #6 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:
Drum & Bass / Breakbeat / Dubstep / Grime Arena - http://download.breakbeat.co.uk/



Thankyou to Arya for this Sig

Offline jim360

  • Posts Too Much
  • *****
  • Posts: 6847
Re: Does anything know how to use C++?
« Reply #7 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;
       }
   }
 }
}
}
}
Check out my Short introduction... corner and my "Historical figures who should perhaps be better-known" thread!!

Exciting videos: 1.1 / 1.2 / 2 / 3 / 4 / 5 / 6



              

Offline BFM_dStruct

  • BFM Admin
  • *
  • Posts: 4909
  • Australian ~ ~
Re: Does anything know how to use C++?
« Reply #8 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

Offline BFM_Kiwi

  • Major
  • *
  • Posts: 9174
Re: Does anyone know how to use C++?
« Reply #9 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!



Offline jim360

  • Posts Too Much
  • *****
  • Posts: 6847
Re: Does anyone know how to use C++?
« Reply #10 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.
Check out my Short introduction... corner and my "Historical figures who should perhaps be better-known" thread!!

Exciting videos: 1.1 / 1.2 / 2 / 3 / 4 / 5 / 6



              

Offline jim360

  • Posts Too Much
  • *****
  • Posts: 6847
Re: Does anyone know how to use C++?
« Reply #11 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 ;
}
Check out my Short introduction... corner and my "Historical figures who should perhaps be better-known" thread!!

Exciting videos: 1.1 / 1.2 / 2 / 3 / 4 / 5 / 6



              

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: Does anyone know how to use C++?
« Reply #12 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).
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline jim360

  • Posts Too Much
  • *****
  • Posts: 6847
Re: Does anyone know how to use C++?
« Reply #13 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.
Check out my Short introduction... corner and my "Historical figures who should perhaps be better-known" thread!!

Exciting videos: 1.1 / 1.2 / 2 / 3 / 4 / 5 / 6



              

Print