moe_3_moe
- 72
- 0
"assignment 1.cpp": W8057 Parameter 'argv' is never used in function main(int,char * *) at line 10
#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;
}
#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;
}
#include <iostream>
#include <string>
int main()
{
std::string s;
std::getline(std::cin, s);
std::cout << s << std::endl;
}
bleeker said:Will C or C++ work better to program a pic?
Yersinia Pestis said: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; }
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?
soul said: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.
#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;
}
class MyClass
{
public:
//this function should return the array
int * MyFunction(){
return my_array;
}
//default constructor
MyClass(){
my_array = {{1,2,3},{4,5,6},{7,8,9}};
}
private:
int my_array [3][3];
}
John_Phillips said:No, you don't.
Any programming requires you to think algorithmically, and that is a skill mathematicians tend to have, but you don't have to be a mathematician to program.
John
samski said:hey, i don't think you need any maths beyond the basics at school for programming, its more about being able to plan your program out in your head and work out how your going to do it, then you can get into the nitty gritty and google most of the stuff you have trouble with :P
you don't even have to understand what a mathematical array is. you can just think of one as like an excel sheet :P
logical laying of stuff out i guess is why it suits mathematicians :P
what kinda things do you want to learn from your programming? I've done a fair bit now in some pretty different languages, and C++ may not necessarily be the best thing to start with for you... if you want to make applications with windows and things, VB or C# could be a good place to start; its pretty and easy. i don't know what everyone else thinks?
Rancour said:Okay, that makes sense as well :D what about Python as a first language? I also dug up an old book on QBasic. is it good to learn this as a first language for organization etc...? or is Python better for a first?
EDIT: Also I heard that Ruby is making improvements with speed, would this be better over python for a first?
pep_i said:god i am getting into computer science myself.um can i learn the basics of c++here
lulu f said:i want help in Numerical integration programing by C
Is this homework? what have you already tried? Please start a new thread, 'cause this is a standalone thing.lulu f said:how we can write aprogram that solve integration by C in rectangular method ,trapezoid ,and simpson
lulu f said:how we can write aprogram that solve integration by C in rectangular method ,trapezoid ,and simpson
dduardo said:Please post your C++ questions or comments here for Tutorial 1.
Smartguy5000 said:I'd like to start learning C++ and I have a dreamspark version of MSVisual Studio 2010 beta, will that be good for a beginner, or should I have a go at something simpler?
Simpler. Visual studio is a really powerful tool, but you spend more time learning visual studio than learning how to code. You want to stick with a really barebones ide ( I like eclipse) or something command line based like MinGW.Smartguy5000 said:will that be good for a beginner, or should I have a go at something simpler?
story645 said:Simpler. Visual studio is a really powerful tool, but you spend more time learning visual studio than learning how to code. You want to stick with a really barebones ide ( I like eclipse) or something command line based like MinGW.