problem with code

I don't know what to do after this...
Can you help me little bit?
my teacher says i can't calculate the all sum in for loop

#include <bits/stdc++.h>

using namespace std;

int main()
{
int c, a, i, n;
double sum = 0;
cout<< "Enter value a: \n";
cin>> a;
cout<< "Enter value c: \n";
cin>> c;
cout<< "Enter value n: \n";
cin >> n;
if (a == 0 || a-c != 0) {
cout << "No result:";
} else {
for (i=1; i <= n; i++){
sum = sum -= ( 8-i ) * (10-a)*(10-a) / sqrt(a - c) ;

}
cout<< "Result is: \n" << sum;
}
}
What are you trying to calculate? What is c, a, i and n?

You're not being charged by the letter. Code is for humans to read, and right now, it's unreadable. Use variable names that mean something to the reader so that we can understand what you're trying to do.
Hello andrejonstv,

To add to what Repeater has said.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button, but you may still to indent your code.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//#include <bits/stdc++.h>  // <--- Not a standard C++ header file. Learn to include the proper header files.

#include <cmath>
#include <iostream>

using namespace std;

int main()
{
	int c, a, n;  // <---- "i" should be defined in the for loop.
	double sum = 0.0;  // <--- Or "double sum{};" the {}s available from C++11 on.

	cout << "Enter value a: ";
	cin >> a;

	cout << "Enter value c: ";
	cin >> c;

	cout << "Enter value n: ";
	cin >> n;

	if (a == 0 || a - c != 0)
	{
		cout << "No result:";
	}
	else
	{
		for (int i = 0; i < n; i++)
		{
			sum = sum -= (8 - i) * (10 - a)*(10 - a) / sqrt(a - c);
		}

		cout << "Result is: \n" << sum;
	}
}

The blank lines help with readability.

When defining "sum" (0) may work, but (0.0) is more proper.

Most things in C and C++ start indexing at (0) zero. Your for loop set up the way you did should be the exception not the norm. The use of the "<=" could cause the for loop to loop one extra time and you could go past the end of an array, string, vector or list. Not what you would want.

Notice I removed the "\n" at the end. This puts that "cin" on the same line as the prompt.

I have not compiled or tested the program yet, so I am not sure if it works.

It would be helpful if you would shoe some test numbers for input and expected output so everyone can get an idea of what the program should do.

Andy
Perhaps you had better explain exactly what sum you are trying to calculate. This is only what I could infer from your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   int c, a, n;
   cout << "Enter a, c, n: ";   cin >> a >> c >> n;
   if ( a <= c )    
   {
      cout << "No result\n";
   }
   else
   {     
      cout << "Result is: " << -( 8 * n - n * ( n + 1 ) / 2 ) * ( 10 - a ) * ( 10 - a ) / sqrt( a - c ) << '\n';
   }
}
Last edited on
sum = sum -= ( 8-i ) * (10-a)*(10-a) / sqrt(a - c) ;

the first sum = is redundant, it just says sum = sum. it does all the math, does the -= to sum, then that result (already in sum due to -=) is put back into sum, in other words.

sum = sum -= ( 8-i ) * (10-a)*(10-a) / sqrt(a - c) ;
Actually this is undefined behavior. You can't modify a variable more than one in an expression. Actually there are exceptions, but as a beginner, it's best to assume that it's always true.

I suspect you meant:
sum += ( 8-i ) * (10-a)*(10-a) / sqrt(a - c) ;

But looking at what your prof said and what lastchance posted, I think your prof wants you to use your pre-algebra skills to apply the formula for this sum. You're computing:
sum for i = 1 to n of (8-i)*k

where k = (10-a)2/sqrt(a-c)

Simplify that.

Topic archived. No new replies allowed.