| New Reply |
[C++] Tutorial 1: Discussion |
Share Thread | Thread Tools |
| Jul12-07, 04:49 PM | #103 |
|
|
[C++] Tutorial 1: Discussion
First off, dduardo, thank you for what has been, so far, a most excellent tutorial.
Having said that, the pascals triangle task has seemed daunting to me, and I had to look up how to make my own functions to do it efficiently, and I came up with this: Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
long double factorial (int num)
{
if (num==1)
return 1;
return factorial(num-1)*num; // recursive call
}
int pscl (int n, int r)
{
int out;
long double nf=factorial(n), rf=factorial(r), nrf=factorial(n-r);
long double btm=rf * nrf;
long double tout=nf/btm;
out = (int)tout;
return out;
}
int main(int argc, char *argv[])
{
int rin;
int nin;
int pout;
cout << "input row: ";
cin >> rin;
cout << "input number: ";
cin >> nin;
pout=pscl(nin, rin);
cout << pout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
but this code crashes, and the debugger (when it decides to work) tells me it gets an 'access violation'. I tried breakpoints, but as I said, the debugger is a bit temperamental. Can you see anything wrong with my code? |
| Jul12-07, 08:14 PM | #104 |
|
|
No idea what the original question were, but I can tell you your program is crashing because / when the end user is entering a number for 'rin' that's larger than 'nin'. I.e. when 'n-r' < 0. (It might also crash when n-r == 0, but too lazy to try.)
|
| Jul12-07, 08:56 PM | #105 |
|
|
Could you be kind enough to explain why it's crashing when n-r==0 ?
EDIT: I changed the condition for the if statement in 'factorial' to (num==1 || num==0) and now it works fine. I'm never nicking code off the internet again. Thanks for bringing it to my attention. I can't believe I didn't spot that after going over the problem for 40 minutes. |
| Jul13-07, 01:11 AM | #106 |
|
|
Well, I've finished the tutorial. Again, great material dduardo.
But the open goal at the end seems a bit steep. A text adventure? With out object-orientation? But, I guess it's a challenge. Except i ran into a major hurdle. Text inputs. This code: Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
char inp[256];
cin >> inp ;
cout << inp << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Also, did the other parts to the tutorial get thrown in the bin? If so, is there anywhere I can look for the higher levels of C++ programming? |
| Jul13-07, 01:33 AM | #107 |
|
|
Code:
#include <iostream>
#include <string>
int main()
{
std::string s;
std::getline(std::cin, s);
std::cout << s << std::endl;
}
|
| Jul13-07, 02:03 AM | #108 |
|
|
Thanks man! I'm gonna makes me a text adventure!
And look for more C++ stuff that's got stuck in the tubes. |
| Jul25-07, 02:04 AM | #109 |
|
|
I know nothing in programming and wanted to know that is this the right place to start as i am finding it a little difficult.
|
| Sep2-07, 09:31 PM | #110 |
|
|
#include <cstdlib>
#include <iostream> #include <iomanip> using namespace std; int main(int argc, char *argv[]) { int x,j; double N,T,S,D,X; cout<<"Enter Value of x:"<<endl; cin>>x; cout<<"Enter No. of Iterations:"<<endl; cin>>j; cout<<endl; X=(x*3.1416)/180; //conversion from degrees to rad N=X; //inital value D=1; //inital value T=1; //inital value S=1; //inital value cout<<"i"<<"\t\t"<<setw(6)<<"Term"<<"\t\t"<<"cos("<<x<<")"<<endl; cout<<endl; for(int i=1;i<=j;i++) { N=(-N)*X*X; D=D*(2*i)*((2*i)+1); T=N/D; //term S=S+T; //summation cout.setf(ios::fixed|ios::showpoint|ios::left); cout<<i<<setprecision(3)<<"\t\t"<<T<<"\t\t"<<setprecision(5)<<S<<endl; } cout<<endl; cout<<endl; cout<<"sin("<<x<<")"<<"="<<S<<endl; system("PAUSE"); return EXIT_SUCCESS; } i derived the formula from the expansion of the function of sin(x) which is x-(x^3/3!)+(x^5/5!)-(x^7/7!)... pls help me..whats wrong w/ my program =(..it wont seem to display the cortrect value.. |
| Jan2-08, 08:14 AM | #111 |
|
|
Why don't u separate the code for searching the factorial of n (n!) in another function.....it will be much easier to debug.......
|
| Jan3-08, 03:07 AM | #112 |
|
|
If you write out the code for calculating factorial separately, and use cmath(for the pow() function), you could do it like this:
s=0; for(int i=1;i<=j;i++) { s+=pow(-1,i+1)*pow(x,2*i+1)/fact(2*i+1); }; cout<<"sinx= "<<s; where pow(a,b)=a to power b, and fact(x) =x! |
| Jun12-08, 07:03 AM | #113 |
|
|
Will C or C++ work better to program a pic?
|
| Jun16-08, 07:47 AM | #114 |
|
|
|
| Jul4-08, 11:53 AM | #115 |
|
Recognitions:
|
The numbers in each row are obtained by adding the two numbers above them in the previous row. The Wikipedia page has a much better explanation: http://en.wikipedia.org/wiki/Pascal%27s_triangle Maybe you should try re-writing your program not using any factorial function. Even a long double will not be big enough for you to get a triangle more than 20 rows or so. And it will also be inaccurate due to rounding errors (a double only holds 15 significant digits of accuracy; it represents higher numbers by using scientific notation). If you use a more efficient method (simply summing terms from the previous row), then you can create huge triangles using only ints, and your program will run much, much faster. |
| Aug2-08, 06:24 PM | #116 |
|
|
Assignment 3 requires use of the sqrt() function and the math.h library. The tutorial before that point doesn't mention them. Not sure whether or not you meant to leave them for the reader to find out on his own, but if not it might make sense to add them.
|
| Aug3-08, 01:36 AM | #117 |
|
|
Can anyone please try to see what's wrong with this program? I wrote it for school, to help simply radicals. It seems to work fine most of the times. However there is a problem when trying to simply perfect cubes. Try and entering 64 with an index of 3, it'll give you an unexpected answer. Now try entering 8 with an index of 3, and it'll give you the right answer. I have tried debugging it, but it's not making any sense to me. I am sorry if this shouldn't be posted here, let me know and I can move it. :) Code:
#include <iostream>
#include <math.h>
typedef struct
{
double x, y;
}pair;
bool breakcube(double radi, double inx, pair& pr)
{
double cb = 0;
for (int i = 2; i < radi; i++)
{
cb=i;
for (int j = 1; j < inx; j++)
cb*=i;
for (int j = 1; j < radi; j++)
{
if (cb*j==radi)
{
pr.x = cb;
pr.y = j;
return true;
}
}
}
return false;
}
int main()
{
double radi = 0;
double cb = 0;
double inx = 0;
char buff[500];
while(true)
{
std::cout << "\nEnter radicand ('quit' to terminate): ";
std::cin >> buff;
if (!strcmp(buff, "quit"))
return 0;
radi=atof(buff);
std::cout << "\nEnter index: ";
std::cin >> inx;
cb = pow(radi, 1.0/inx);
if (cb == (int)cb)
{
std::cout << "\nPerfect " << inx << " : ";
for (int i = 0; i < inx; i++)
{
std::cout << cb;
if (i!=inx-1)
std::cout << " * ";
}
std::cout << " = " << pow(cb, inx);
continue;
}
pair pr;
if (breakcube(radi, inx, pr))
std::cout << "\n" << radi << " can be broken by: " << pr.x << " * " << pr.y;
else
std::cout << "\n" << radi << " cannot be broken";
}
return 0;
}
|
| Aug3-08, 07:04 PM | #118 |
|
|
In Exercise 5, the do-while example code prints out HELLO only twice instead of the 3 times shown in the example output.
|
| Sep8-08, 07:56 PM | #119 |
|
|
guys can somebody help me with a java programming? It's a really short homework but I'm just stucked there. Here is the code if u can please teplay to me asap.
public class DaysPerWeek { public static void main(String[] args) { final int DAYS_PER_WEEK = 7; int days; int weeks; int totalDays; Scanner scan = new Scanner(System.in); System.out.println("This program convert days to week"); System.out.print("Enter number of days; "); days = scan.nextInt(); days = totlaDays % DAYS_PER_WEEK; weeks = totalDays / DAYS_PER_WEEK; System.out.println("Days Per Week" + totaldays); } } |
| New Reply |
| Thread Tools | |
Similar Threads for: [C++] Tutorial 1: Discussion
|
||||
| Thread | Forum | Replies | ||
| CFD tutorial | Mechanical Engineering | 4 | ||
| C# Tutorial | Programming & Comp Sci | 13 | ||
| GR Tutorial | Special & General Relativity | 2 | ||
| Probability tutorial | Academic Guidance | 0 | ||
| Tutorial La Tec | General Math | 1 | ||