Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am working with matrices in c++ and the type of elements is double. My limit for matrix dimentions is 10,000. I want to know what is the logical value to set as limit for elements, considering the fact that I am doing many sum and multiplication actions on them. I want this number to be as high as possible but without having infinity number problems.
The range of double is
1.7976931348623158e+308
Lets say maximum value of element as X;
for multiplication the maximum answer will be
X*X + X*X + X*X .....10,000 times(maximum row and column count)
i.e
10000*X*X
Therefore
`
1.7976931348623158e+308 = 10000*X*X
1.7976931348623158e+304 = X*X
X ~ 1.7976931348623158e+150
But you will lose precision.
This value is if you are going to multiply once.
You can always use std::numeric_limits.
For example:
#include <limits>
#include <iostream>
int main()
{
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
let's assume that I have the sum of the integer in my variable mean and the sum of the scarred integer in my variable std. I want to have the mean and the standard deviation with respectively 3 and 4 numbers after the decimals point. But I don't have the expected value at all for the mean and the std :
int main(){
mean = double(int((double(mean)/(n-1))*1000))/1000;
std = double(int(sqrt(double(std)/(n-1) - mean*mean)*10000))/10000;
return 0;
}
I put double(int(....*1000))/1000 to have the 3 numbers after the decimal point.
The first issue was to correct the formulas:
you need to divide by n for the mean, and by n-1 for the variance.
To get exactly the expected number of figures after the decimal point, it is better to
perform the calculations with best possible precision, and then for the final display to combine
<< std::fixed << set precision (3) << ...
With this solution, you get some additional zeros at the end if necessary.
Output:
mean = 1.090
std deviation = 0.0265
The code:
#include <iostream>
#include <cmath>
#include <iomanip>
int main(){
double sum_x = 5.45;
double sum_x2 = 5.9433;
int n = 5;
double mean = sum_x/n;
double variance = sum_x2/n - mean*mean;
variance *= double (n)/(n-1); // correction to get an unbiased estimation
double std_deviation = std::sqrt (variance);
std::cout << "mean = " << std::fixed << std::setprecision(3) << mean << "\n";
std::cout << "std deviation = " << std::fixed << std::setprecision(4) << std_deviation << "\n";
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If I have two arrays, one storing the name of clients, and the other storing the amount they have spent, how can I separately output their individual spending?
For example,
Array1:[peter,mary,peter,may,edward]
Array2:[300,400,500,300,400,500]
The position of the money spent are corresponding to the names, and each one have spent positive dollars.
I know the size of the arrays, but how can I output their individual spending and calculate the number of clients?
Since the clients may have more than one record in the arrays, I am a bit confused on how to count the numbers and output separately.
Expected:
**Spendings:**
peter:600
mary:400
...
...
**Number of people**:4
Here's my previous idea (sorry for forgetting to include this in my original question):
int Array_amount_store[5]; //For storing each clients' amount
for (int i=0; i<=5; i=i+1) // Initializing
Array_amount_store[i]=0;
for (int i=0; i<=5; i=i+1)
for (int j=0; j<=5; j=j+1)
if (Array1[j]==Array1[i])
Array_amount_store[i]=Array_amount_store[i]+Array2[i];
I just calculated the total amount but am stuck on how to output it.
Have a look at std::map or std::unordered_map. Use the client names as its key, and their monetary sums as its value. Then you can simply loop through the arrays adding up the purchases for each name, and then when finished, loop through the map to output the results. For example:
#include <iostream>
#include <map>
#include <iomanip>
std::map<std::string, double> ClientSpending;
for(int i = 0; i < NumberOfArrayElements; ++i)
ClientSpending[Array1[i]] += Array2[i];
std::cout << "Spendings:" << std::endl;
for (auto &client : ClientSpending)
std::cout << client.first << ":" << std::put_money(client.second) << std::endl;
std::cout << std::endl;
std::cout << "Number of people:" << ClientSpending.size() << std::endl;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
The following code
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool qualities(double number){
//function qualities, irrelevant to question
}
int main(){
double pandigital = 123456789;
double sum = 0;
string strpan = to_string(pandigital);
while (next_permutation(strpan.begin(), strpan.end())){
cout << strpan << endl;
if (qualities(pandigital)){
sum += pandigital;
}
next_permutation(strpan.begin(), strpan.end());
break;
}
cout << "sum is " << sum << endl;
cin.get();
}
has the following problems beginning at this piece of code
double pandigital = 123456789;
double sum = 0;
string strpan = to_string(pandigital);
while (next_permutation(strpan.begin(), strpan.end())){
cout << strpan << endl;
The problem is strpan has an extra zero appended to it, which is an error. If I add a zero to pandigital so we have 1234567890, it will still append an extra zero.
Zero comes from std::to_string converting a double to string: rather than producing "123456789", it makes "123456789.0".
To fix this problem, declare pandigital as int, or add a cast when converting it to a string:
string strpan = to_string((int)pandigital);
With this issue out of the way, you have another fix to make: each iteration of your while loop calls next_permutation twice - once in the header of the loop, and once in the body. You should remove the invocation that you perform in the body of the loop.
Finally, recall that in order to go through all possible permutations the initial call of next_permutation should be made when the range is sorted. It is not a problem for your number, because the digits are arranged in ascending order, but it may become an issue if you start with a different number.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
int main()
{
long long x,y,z,result;
char f,g;
cin >>x>>y>>z;
**result** =
cout << result ;
return 0;
}
How to make result = x (+ or - or / or *) y (+ or - or / or *) z !?
Reading the operators in between the numbers is simple:
long long x,y,z;
char f,g;
cin >>x>>f>>y>>g>>z;
// See what you've got
cout << x << " " << f << endl;
cout << y << " " << g << endl;
cout << z endl;
However, figuring out the result of the operation is trickier: you need to check the values you've got in f and g, and perform the operations as needed. Note that there must be no space between your numbers and the operators, otherwise the input would be processed incorrectly.
Demo.
This is probably at the core of the exercise that you are solving, so I will suggest that you write a function like this:
long long compute(long long a, long long b, char op) {
... // Check the operator, and return the result
}
With this function in hand, you can produce the result in one simple call:
long long result = compute(compute(x, y, f), z, g);
Once you write the compute function, this should give the result that you expect.
You can do cin>>astring. And separate the string by delimiter and convert them to integer.
For example:
1,2,3
will become '1','2','3'.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to declare
int x = it must be more or equal to 1 but less or equal to 100;
How can I do it? I dont want to use if condition, Im looking for something short and clear, if possible.
The x number is input, so program should accept only numbers in this limit.
It seems that you're looking to error check on initialization.
If I were you I'd do something along the lines of.
int x;
cout << "Enter a value: " << flush;
cin >> x;
while(!((x>=1)&&(x<=100))) {
cout << "Try Again: " << flush;
cin >> x;
}