Series

Hello, I'm beginner in C++ language and I tried to make a program that gives to me an value of a series formula. Later I will introduce long types, but for a first try I'm using integers variable. The code run, but windows is warning me that program has a problem and began to verify.
Note, I'm not a speak english user, so the comments below are in my native language.

What's wrong with the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  // Série

#include <stdio.h>
#include <stdlib.h>

int main ()
{
	int n,a ; //inserindo um valor n inteiro
	printf("Insira um valor adequado qualquer");
	scanf ("n");
	
	a=((-1)^(n+1))*(n^2/n+1);
	
		return (a);
}
Hey there,

I am sorry, but I don't really understand, what you actually want to do with that code...

Maybe you could give us a bit more information? That would be awesome ^^
Hi and welcome to cplusplus :+)

With your code, the ^ doesn't do what you think it does. There is the pow function for which you need to include the math header file.When you are programming, always look at the reference material for the operators and functions etc that you want to use, rather than assuming you know how they work.

http://www.cplusplus.com/reference/


The reference material has example code in it. There is also a tutorial at the top left of this page.

Some other things that will help a bit:

It is good practice to always declare and initialise one variable per line. Initialise each variable to something, even if you use scanf on the next line, or assign a value to it on the next line. This might seem a little crazy, but quite often the biggest source of errors is uninitialised variables. Also, you can put a comment to explain what each variable is used for.

scanf returns an int for the number of successfully read values, so you should always make use of this, to see that it worked. One has to be careful - try to think of what can go wrong, and write your code to catch errors and do something about them before they become a problem.

If you are going to calculate a series, you will need a loop. A for loop is good when you know how many terms you want to calculate. A while loop is good when you want to keep going until a term is less than a certain value, say.

Line 14 of your code isn't doing what you think it is either. You need to use printf to show the value.
You can return a value from a function, if you write one.

At the moment you are returning a value to the operating system. A value of zero indicates that the program ran successfully, other values means that it didn't. This is useful because one can use the success of one program to determine whether to run another program, as in this on the command line: ProgA && ProgB ProgB only runs if ProgA was successful.

I hope this helps a bit, don't be afraid to ask questions - there are plenty who can help.
MokkaTech I think TheIdeasMan has caught the idea (sorry my bad english).

I forgot the pow function. As I said, I'm beggining to programming and hurry to learn. So I began with this little challenge.

I will try to declare each variable.

This is my main difficult: see the logics behind the problem.
I think for loop is more suitable.
Hi,

I strongly suggest you use the double type rather than an int. The problem is integer division, it truncates the fraction part (int doesn't do fractions at all), so for example 3 / 4 is 0.

Try to learn (sooner rather than later) what the differences are between all the types that C uses. At least start with int char and double. With the integral types, there are short and long versions, and they can be unsigned as well.

Have you managed to read any of the tutorial at the top left of this page yet?

With a series, it is often possible to avoid completely recalculating the latest term, instead multiply the previous term by a constant to calculate the last term. For example, if your series is the sum of an / n! where a is a constant and n is a positive integer, then the next term can be found by multiplying the previous term by a / n

So have a go at applying that logic to your series.


Don't worry about your English skills, in my opinion English would be a complete pain to learn, and I wonder how I would get on if I went to your country and had to learn your language :+) Do you use Google translate ?

Any way Good Luck :+)
Topic archived. No new replies allowed.