[C++] Tutorial 1: Discussion

by dduardo
Tags: discussion, tutorial
i.mehrzad is offline
Jul25-07, 02:04 AM
P: 83
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.
archermcr is offline
Sep2-07, 09:31 PM
P: 1
#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;
cout<<"Enter No. of Iterations:"<<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
for(int i=1;i<=j;i++)
T=N/D; //term
S=S+T; //summation
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..
Dendi is offline
Jan2-08, 08:14 AM
P: 4
Why don't u separate the code for searching the factorial of n (n!) in another function.....it will be much easier to debug.......
chaoseverlasting is offline
Jan3-08, 03:07 AM
P: 1,017
If you write out the code for calculating factorial separately, and use cmath(for the pow() function), you could do it like this:

for(int i=1;i<=j;i++)

cout<<"sinx= "<<s;

where pow(a,b)=a to power b, and fact(x) =x!
bleeker is offline
Jun12-08, 07:03 AM
P: 13
Will C or C++ work better to program a pic?
soul is offline
Jun16-08, 07:47 AM
P: 62
Quote Quote by bleeker View Post
Will C or C++ work better to program a pic?
Well, It can be wrong but I think C would be better, since it is an event driven programming language and it work faster, in more efficient way.
Ben Niehoff
Ben Niehoff is offline
Jul4-08, 11:53 AM
Sci Advisor
P: 1,563
Quote Quote by Yersinia Pestis View Post
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:

#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;
    return EXIT_SUCCESS;
the function 'factorial' is fairly obvious, and is a double float because factorials can be big. 'pascal' uses the binomial coefficient thing to calculate the number at a given place and row (n, r).

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?
Ack! There are FAR more efficient ways to calculate Pascal's triangle than to use a factorial function. I think the Mathworld page failed to explain the key property of Pascal's Triangle, which was Pascal's original motivation in the first place:

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.
Malvenuto is offline
Aug2-08, 06:24 PM
P: 2
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.
computerex is offline
Aug3-08, 01:36 AM
P: 68
Quote Quote by soul View Post
Well, It can be wrong but I think C would be better, since it is an event driven programming language and it work faster, in more efficient way.
C is procedural, not event driven.

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. :)

#include <iostream>
#include <math.h>

typedef struct 
	double x, y;

bool breakcube(double radi, double inx, pair& pr)
	double cb = 0;
	for (int i = 2; i < radi; i++)
		for (int j = 1; j < inx; j++)
		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];
		std::cout << "\nEnter radicand ('quit' to terminate): ";
		std::cin >> buff;
		if (!strcmp(buff, "quit"))
		    return 0;
		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);
		pair pr;
		if (breakcube(radi, inx, pr))
		   std::cout << "\n" << radi << " can be broken by: " << pr.x << " * " << pr.y;
		   std::cout << "\n" << radi << " cannot be broken";
	return 0;
Malvenuto is offline
Aug3-08, 07:04 PM
P: 2
In Exercise 5, the do-while example code prints out HELLO only twice instead of the 3 times shown in the example output.
robertkurti is offline
Sep8-08, 07:56 PM
P: 2
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);

dtsylvas is offline
Sep24-08, 10:28 PM
P: 1
I have a question about my C++ program....I have to compute the area of a rectangle.....I have to write this program using functions....I have my instructions correctly output to the user in the command prompt and I am allowing the user to enter in a value for the length and the width....that's all fine....I wrote the computation for the solution to the problem but the cout info to appear on the screen but that part does not show up along with the answer.....can anyone help me?
Attached Files
File Type: doc area.doc (28.0 KB, 11 views)
ehrenfest is offline
Oct3-08, 09:56 PM
P: 1,998
My C++ is a little rusty. I want to have a class that has a 3 by 3 array of integers as a member variable. I want the class to return the array through a member function. I am having trouble getting the initialization right. I think you can tell what I want to do from the following program although of course the compiler would go crazy if I tried to compile this. Can someone please tell me how to rewrite this so it is correct?

class MyClass


//this function should return the array
int * MyFunction(){

return my_array;


//default constructor

my_array = {{1,2,3},{4,5,6},{7,8,9}};



int my_array [3][3];

ehrenfest is offline
Oct3-08, 10:51 PM
P: 1,998
Hmmm. I think they kind of provide a solution at the MSDN website:


I have a few questions about that...does anyone understand what they are doing there?
ammenme is offline
Oct25-08, 10:39 AM
P: 2
hi please i download textpad for my computer to use it for c++ but in the tool box i cant find the compile tool.please any help.thanks
John_Phillips is offline
Oct28-08, 11:47 PM
P: 12
For ehrenfest - For your first question, the trouble you have is about the return type. You are returning a pointer to integers. That will work for one dimensional arrays, but not for higher dimensional arrays. Your 3 by 3 array is actually a pointer to pointers to integers. That is, the first pointer has an array of other pointers. The lower level pointers each have arrays of three integers. So, you could make the return type usable by making it a pointer to pointers.

That said, I would suggest against doing it. What you are returning is a direct access into the memory held by the class. Anyone who makes a change to this return value is changing the contents of the object, and not just some local version of the array. Even worse, the object could be destroyed (by going out of scope, for example) and leave you with a pointer that no longer points where you think it points. This is a seg fault or other corruption waiting to happen. Reconsider why you want to do this. It may be that the return doesn't help you and you really want a copy constructor or an accessor function. It may be that you don't really want the safety of encapsulating the data. In which case, it is a better design idea not to try and fake safety that isn't really there.

As for your second question - they are working with managed memory containers in the visual compilers. Only try to follow them if you do not ever need to use this for anything other than the visual platform.

ammenme is offline
Oct29-08, 02:15 AM
P: 2
so am i to download the visual c++ separately and add it to the text pad or i can use the visual c++ without text pad?thanks
BCox is offline
Nov3-08, 03:55 AM
P: 16

I am using MS Visual Studio as the compiler. I have a c++ routine that I needed in my current project, which I saved under my Header Files. This file is needed for the compiler to interpret certain variable declarations. However, when I F7 to compile I get the following error:

Cannot open include file: 'nr3.h': No such file or directory

But I can clearly see the nr3.h file in my project. What is wrong? Please find my code below:

#pragma once
#include "nr3.h"
class gamma
Doub gammln(const Doub xx) {

Main program:
#include "gamma.h"
#include "incgammabeta.h"
#include "nr3.h"
using namespace std;

int main()
double a, b, x, incompleteBeta;
return 0;

Register to reply

Related Discussions
CFD tutorial Mechanical Engineering 4
C# Tutorial Programming & Computer Science 13
GR Tutorial Special & General Relativity 2
Probability tutorial Academic Guidance 0
Tutorial La Tec General Math 1