corect my code, please!

Data on the learning outcome of each of the 15 students in the group in five subjects is set by the user. Determine the number of students who did not receive a grade of "uncluttered" (2)
Constant result 0. Why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <cstdio>
#include<cmath>
#include <iostream>
using namespace std;
int main()
{
	float e, x;
	cin>> e >> x;
	float r = 0, ca = x;
	x *= x;
	int i = 0, sgn = 1;
	while (fabs(ca / (2 * i - 1)) > e)
	{
		r += sgn * ca / (2 * ++i - 1);
		ca *= x;
		sgn *= -1;
	}
	cout << r;
	 return 0;
None of your code has anything to do with 'students', 'subjects' or 'grades'.

Write the code as you see fit. The main thing for me is that the result is displayed correctly.
The point is, you're asking why the output is a "constant result of 0", but you're not explaining what output you want to be happening, or how it relates to student grades.

The result is not always 0, by the way. For example, an input of (e = 0.1, x = 0.2) outputs 0.2.
f(0.1, 0.2) = 0.2
f(0.1, 0.3) = 0.3
f(0.9, 1.1) = 0.656333
f(0.9, 0.99) = 0.666567
f(0.99, 0.999) = 0.666666
f(0.01, 1) = 0.7903
f(0.1, 0.9) = 0.77098
f(1, 2) appears to not halt

The most obvious way for the output r to be 0 is when the while loop is not entered.
The condition is (fabs(ca / (2 * i - 1)) > e).
i = 0 initially, so this means
(fabs(-ca) > e)
Obviously, the absolute value of a negative number is positive, so this means
(fabs(ca) > e), where ca = x.

So any time that e < |x| will produce something non-zero, or not halt.
Last edited on
I understand your point, I need to get an integer that fully indicates the number of students who do not get grades of "uncluttered" (2). It must be an integer. Maybe you have another opinion about this task.
How can I fix this?
I don't know. Maybe scrap the code you have, start over, and write something that actually resembles what your prompt says?

The assignment probably wants you to have some sort of array of grades or subjects, have user input fill in those values, and then do calculations with those values. Maybe int grades[15][5]; where grade at grades[student_index][subject_index] is filled in by user input.
Explain more fully what it is supposed to do. 'number of students who do not get grades of "uncluttered" (2)' doesn't really mean much. Explain with examples. What do the 2 inputs supposed to represent? How would this be done using pen/paper?
Yes, thanks for the advice. I'll start all over again.
We have 15 students and 5 subjects. The user enters grades. We must display the number of students who received 2. In total, there are grades 1,2,3,4,5. 5 - the highest, 1 - the lowest. All student who get 3,4,5 we must display.
Hello chebyrek,

It looks to me that you need to do some planning of this program first.

As part of the Ganado's suggestion is worth considering in the planning.

If you choose to use a 2D array this is something to consider:
1
2
3
4
constexpr int MAXSTUDENTS{ 15 }, MAXSUBJECTS{ 5 };

int studentGrades[MAXSTUDENTS][MAXSUBJECTS]{};

Depending on your program line 1 could go above "main" or be the first line of "main".

Using the "MAX..." variables if say the number of students should change you only have 1 place that needs to be changed.

When it comes time to write the code start small like a few necessary variables and the code to get the input. Once you get that part working the rest is easier to work with. DO NOT try to write the whole program at once. This will increase your time in debugging and trying to figure out what is wrong.

Andy
Hi, Andy! Thanks for the help. I started developing a program. Your tips will help make it better and easier.
Topic archived. No new replies allowed.