while cicle

Hi everyone, here a program to evaluate the weighted average of a given set of marks and cretits. I would really appreciate a feedback on this, I want to understand if it's written properly. Thanks in advance!

p.s. I've just started C++ programming, maybe this program would be better with dynamics arrays or other stuff, but for now I'm sticking with the basics.

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
  int main () {
	double average=0;
	double num=0, den=0;
	unsigned int m, cfu;

	while(true){
			cout << "Insert a mark (zero if you want to close the program): " << endl;
		cin >> m;
		
		if(m==0) {
			cout << "You've just insert 0! " << endl;
			break;
			}
			
		cout << "insert the related credits: " << endl;
		cin >> cfu;

		num+=m*cfu;
		den+=cfu;
		
	}

	average=num/den;
	cout << "The average is: " << average;

	return 0;
}
Last edited on
...better with dynamics arrays

Be glad that you haven't learned them. They should not be used in modern C++, there are better ways, but as a beginner you shouldn't worry about it.
great, thanks!
Perhaps:

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
#include <iostream>

int main() {
	double num {}, den {};

	while (true) {
		unsigned m {}, cfu {};

		std::cout << "Insert a mark (zero if you want to close the program): ";
		std::cin >> m;

		if (m == 0) {
			std::cout << "You've just insert 0!\n";
			break;
		}

		std::cout << "insert the related credits: ";
		std::cin >> cfu;

		num += m * cfu;
		den += cfu;
	}

	std::cout << "The average is: " << num / den;
}

Wow, thanks, I don't know how the "curly brackets's variables" works, I'll do some research about them. Thanks for the feedback man, I very much appreciate it.
Meantime, I've ugraded the program to this:

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

int main () {
	double average=0;
	double num=0, den=0;
	unsigned int m, cfu;

	while(true){
		cout << "Insert a mark (zero if you want to close the program): " << endl;
		cin >> m;
		
		if(m==0) {
			cout << "You've just insert 0! " << endl;
			break;
			}
			
		if((m<18)||(m>30)){
			cout << endl <<  "Mark not valid! Try to insert a number between 18 and 30 " << endl;
			continue;
		}
			
		cout << "insert the related credits: " << endl;
		cin >> cfu;
		
		num+=m*cfu;
		den+=cfu;
		
	}

	average=num/den;
	cout << "The average is: " << average << endl;

	return 0;
}

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

int main ()
{
   const int N = 5;
   valarray<double> marks( N ), credits( N );
   
   cout << "Enter " << N << " marks: ";
   for ( auto &e : marks ) cin >> e;
   
   cout << "Enter " << N << " credits: ";
   for ( auto &e :credits ) cin >> e;
   
   cout << "Credit-weighted average is " << ( marks * credits ).sum() / credits.sum() << '\n';
}


Enter 5 marks: 54 65 80 61 50
Enter 5 credits: 10 5 5 20 10
Credit-weighted average is 59.7
Last edited on
wow, there are things obscure to me(like that for function), but it's beautiful! I would problably let the user choose the number of marks. Thanks man!
Last edited on
@zhylian. C++ is a large language and is considered a somewhat complex language. There are often more than one way to accomplish the requirement. As you continue your journey into learning C++, you'll start to understand more and more re the language. It's important to make sure you understand what you are being taught before moving on to new topics.
I don't know how the "curly brackets's variables" works

It's called "uniform initialization."
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
<3
Topic archived. No new replies allowed.