Plus Minus in C++ Program
Plus minus in C++ programming language.
In this article, you will learn how to make a plus-minus in C++ programming language.
Example
Enter the size of the array::
8
Enter the 8 elements of the array::
4 1 -4 -5 3 -9 0 -9
0.375000
0.500000
0.125000
You should have knowledge of the following topics in c++ programming to understand these programs:
- C++
main()
function - C++
while
loop
Source Code
// Plus Minus in C++ Program
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int s=0;
cout << "Enter the size of the array::\n";
cin >> s;
float arr[s];
cout << "Enter the " << s << " elements of the array::\n";
// It will read the inputs of the array's elements + calculate the plus-minus values
int i=0;
float r1=0,r2=0,r3=0;
while (i<s) {
cin >> arr[i];
if (arr[i] > 0) r1++;
if (arr[i] < 0) r2++;
if (arr[i] == 0) r3++;
i++;
}
r1 = r1/s;
r2 = r2/s;
r3 = r3/s;
// It will print the final output
cout << fixed << setprecision(6) << "\n\n" << r1 << endl << r2 << endl << r3 << endl;
return 0;
}
Output
Enter the size of the array::
8
Enter the 8 elements of the array::
4 1 -4 -5 3 -9 0 -9
0.375000
0.500000
0.125000
Explanation
In this given program we have taken the inputs size of the array 8
and the array's elements 4, 1, -4, -5, 3, -9, 0, -9
. Then we applied the standard formula to calculate the plus-minus values of the given inputs.
After the whole calculation above program will return the 0.375000, 0.500000, 0.125000
output of the program.