whats wrong with my c++ code

Why my programs doesn't work? it just doesn't make math. Program doesnt print a at all. and prints that "A SUM = 0". I have to make a program that counts a, m goes in interval from 2 to -2 by -0.5, and n goes from 1 to 5 by 1. formula is a = (m*m) / (n-m). I have to use for....

I mean console appears like:
MY PROGRAM
Enter N and M
N: 1
M: 2
A RESULTS
A SUM = 0.000

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
36
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;
int main()
{
    int nprad,mprad,ngal,mgal,h2;
	float n,m,h1,sum,a;
	cout << "MY PROGRAM" << "\n";
	cout << "Enter N and M" << "\n";
	cout << "N: "; cin >> nprad; "\n";
	cout << "M: "; cin >> mprad; "\n";
	sum = 0;
    h1=(-0.5);
    h2=1;
    ngal=5;
    mgal=(-2);
	cout << "A RESULTS" << "\n";
    for (m=mprad; mprad <= mgal; m=m+h1){
        for (n = nprad; n <= ngal; n = n + h2){
		if (n>=m>0)
			a = (m*m) / (n-m);
            
		else
			cout << "Check your n and m";
		cout << fixed << setprecision(2) << a << endl;

		sum = sum + a;
	}
    }
    
	cout << fixed << setprecision(3) << "A SUM= " << sum << endl;
	cout << "-------------------" << "\n";
}
if mprad is 2 it is impossible that it can be less or equal to -2 (= mgal)

I would say that the comparison on line 20 is m >= mgal

what is line 22 supposed to do?
line 22 checks if n>=m>o if true does the a = .... its in my work description it has to be included. programs has to check it.
btw the one you said helped ! but now it doesn't stop O_O counts to infinity... it shout count only while mprad is <= mgal... so 8 times i think... cuz 2+(-0.5) every time...
line 22 checks if n>=m>o if true does the a = ....
No, it makes no sense. What do you mean

(n>=m) && (m>0)
or
(n>=0) && (m>0)
or
...

?

but now it doesn't stop
show your modified code.
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 <iostream>
#include <math.h>
#include <iomanip>

using namespace std;
int main()
{
    int nprad,mprad,ngal,mgal,h2;
	float n,m,h1,sum,a;
	cout << "A REIKSMIU SUMOS SKAICIAVIMAS" << "\n";
	cout << "Pradinai duomenys" << "\n";
	cout << "N: "; cin >> nprad; "\n";
	cout << "M: "; cin >> mprad; "\n";
	sum =0;
    h1=(-0.5);
    h2=1;
    ngal=5;
    mgal=(-2);
	cout << "A REZULTATAI" << "\n";
    for (m=mprad; mprad >= mgal; m=m+h1){
        for (n = nprad; n <= ngal; n = n + h2){
            if ( (n>=m) && (m>0) )
			a = (m*m) / (n-m);
            
		else
			cout << "Salyga netenkinama";
		cout << fixed << setprecision(2) << a << endl;

		sum = sum + a;
	}
    }
    
	cout << fixed << setprecision(3) << "A REIKSMIU SUMA= " << sum << endl;

}


but it still doesn't stopped.

and question for this one:
1
2
            if ( (n>=m) && (m>0)  )
			a = (m*m) / (n-m);

if ( ( ..... ) ) = false then programs does else? or i need to set it
Last edited on
one more thing:
Should all of these lines:
1
2
3
4
	cout << "Salyga netenkinama";
		cout << fixed << setprecision(2) << a << endl;

		sum = sum + a;

be part of your 'else' condition or just the first line? You calculate the value for 'a' when the if evaluates to true, but if it doesn't you're going to print garbage out to the screeen because you are not initialising 'a'.
At line 20, the condition you have is mprad >= mgal. Since neither of these variables gets modified inside the loop, the condition never becomes false. I think the condition you want is m <= mgal.

Regarding the if/else, you are correct, if the condition in the if statement is false then the program executes the else part (if there is an else part).
be part of your 'else' condition or just the first line? You calculate the value for 'a' when the if evaluates to true, but if it doesn't you're going to print garbage out to the screeen because you are not initialising 'a'.


no just the first line, is there something wrong as well? and i don't get if what are you saying about "initialising 'a'"could u show the code for me?

and is the if right now?
1
2
3
4
5
6
7
8
9
10
11
12
 for (m=mprad; m >= mgal; m=m+h1){
        for (n = nprad; n <= ngal; n = n + h2){
            if ( (n>=m) && (m>0)  ){
                a = (m*m) / (n-m);}
            
		else
        {cout << "Salyga netenkinama";}
		cout << fixed << setprecision(2) << a << endl;

		sum = sum + a;
	}
    }


dhayden, yes but it was still m >= mgal thanks very much
Last edited on
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 <iostream>
#include <math.h>
#include <iomanip>

using namespace std;
int main()
{
    int nprad,mprad,ngal,mgal,h2;
	float n,m,h1,sum,a;
	cout << "A REIKSMIU SUMOS SKAICIAVIMAS" << "\n";
	cout << "Pradinai duomenys" << "\n";
	cout << "N: "; cin >> nprad; "\n";
	cout << "M: "; cin >> mprad; "\n";
	sum =0;
    h1=(-0.5);
    h2=1;
    ngal=5;
    mgal=(-2);
	cout << "A REZULTATAI" << "\n";
    for (m=mprad; m >= mgal; m=m+h1){
        for (n = nprad; n <= ngal; n = n + h2){
            if ( (n>=m) && (m>0)  ){
                a = (m*m) / (n-m);
                cout << fixed << setprecision(2) << a << endl;
                sum = sum + a;}
                
		else
        {cout << "Salyga netenkinama"<< "\n";}

	}
    }
    
	cout << fixed << setprecision(3) << "A REIKSMIU SUMA= " << sum << endl;

}



now everything works almost perfectly. only the result in a console appears like A REIKSMIU SUMA= inf
there should be some numbers instead of inf xD

i found this out as well, but don't know how to set it, could u help me guys?
1
2
3
4
5
6
7
8
9
10
    for (m=mprad; m >= mgal; m=m+h1){
        for (n = nprad; n <= ngal; n = n + h2){
            if ( (n>=m) && (m>0)  ){
                a = (m*m) / (n-m);
                sum = sum + a;
                cout << fixed << setprecision(2) << m << "     " << n << "     " << a << endl;
                }
                
		else
        {cout << "Salyga netenkinama"<< "\n";}


in this code i have to set , that a = (m*m) / (n-m); (n-m) can't be equal to 0


cuz i can't (m*m) / 0 = its illegal :x
sum is not initialized. Do it like so:

float n,m,h1,sum=0,a; // initialize sum

By the way: The condition n != m must be fulfilled otherwise you will get an invalid number because you divide by 0
Last edited on
code777 it is. line 14 :-) , i need to check if n-m isn't = 0, only then do the a = (m*m) / (n-m); cuz if (n-m) = 0 , it says inf... :)
Topic archived. No new replies allowed.