This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
The assignment is to write a computer program that will add 1/3 to itself a large number of times and to compare the result to multiplying 1/3 by the number of times 1/3 was added to itself. It is also to do the same thing with ½.The program is to do this arithmetic twice, once using single precision (float) and once using double precision (double). Both of these will be in one program. Make certain you use a type for your counter that works with these large numbers.
Your program will do these additions 109 (1 billion) times.
#include<iostream>
#include<conio.h>
#include<math.h>
#include <limits>
using namespace std;
typedef std::numeric_limits< double > dbl;
int main()
{
long size=1000000000;
int count=0;
long N=10;
float nAdd=1;
float nMul=1;
cout.precision(dbl::digits10);
cout<<"Iterration #\t\tAdd\t\t\tMul"<<endl;
for(long i=1; i<=size; i++)
{
nAdd+=1.0/3.0;
nMul*=1.0/3.0;
count++;
if(count%N==0 && count!=0)
{
N*=10;
cout<<i<<"\t\t"<<fixed <<nAdd<<"\t\t"<<fixed <<nMul<<endl;
}
if(count==size)
{
cout<<"Difference : "<<fixed <<nAdd<<" - "<<fixed <<nMul<<" = "<<fixed <<nAdd-nMul<<endl;
}
}
getch();
return 0;
}
so for i have done this
i don't get it properly
what number i have to use which will be multiply by 1/3 or 1/3 will be added into it
can you guyz explain me this a lil
thanks alot
add 1/3 to itself a large number of times
This means that you should add ⅓ a bunch of times: ⅓ + ⅓ + ⅓ + ⅓ + ...
Then you're supposed to compare the result of that calculation with the result of multiplying ⅓ by the number of times you added it. So for example if you add it together four times (⅓ + ⅓ + ⅓ + ⅓) then you compare that result with the result of ⅓ × 4.
Mathematically the results should be the same, but the purpose of this assignment is to teach you something about how the computer performs the calculation.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was trying to make a C++ program to calculate value of Pi up to 'n' decimal places using the Leibniz formula.
The number 'n' would be entered by the user.
I was successful in creating the Leibniz formula but I am having trouble with the latter part i.e value precise up to 'n' places.
The trouble :-
As more and more terms will continue to add to it, it's digits will keep on changing so how to tell if a particular digit has stopped changing despite the addition of more terms.
The code written so far :-
#include<iostream>
using namespace std;
int main()
{
float s=0;
int w=-1;
for(float i=1;;i=i+2)
{
w=w*(-1);
s=s+w*(1/i);
cout<<s<<endl;
}
return 0;
}
It would be great if things would be kept simple since I am just a beginner at C++.
Thank you very much :)
Since you want to compute Pi up to arbitrary nth digit, you want a library for working with big float numbers; see
C++ library for big float numbers
the real trouble is that Leibniz formula is not useful in the context. The actual precision achieved can be estimated as the last term in the formula
Pi / 4 = 1/1 - 1/3 + 1/5 - 1/7 + ... + (-1)**(n + 1) * 1 / (2 * n - 1) + ...
If, for intance, you want Pi up to 100th digit it means that the residual (and the last term) should be less than 1e-100:
1 / (2 * n - 1) < 1e-100
2 * n - 1 > 1e100
n > 5e99
And as you can see 5e99 loops is by far too much for the modern (super-)computers
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Description:
Jack is growing some peanuts in his garden. The garden is a L * W rectangle and every unit area has its independent number of how much peanuts he will harvest. He wants to find out what the maximum amount of peanuts is in a given-sized region. This region has fixed size as a*b.
Input Format:
Line 1 : 2 integer length L and width W
Line 2 - L+1 : W integer per line, representing the amount of peanuts in unit area, A (0<=A<=10)
Line L+2 : 2 integer, a and b, representing the length and width of the selected region
Output Format:
One integer m, which is the maximum sum of the amount of peanuts in the selected a*b region
Sample input:
4 5
1 2 3 4 5
6 7 8 0 0
0 9 2 2 3
3 0 0 0 1
3 3
Sample output
38
Data satisfying: 1≤a≤L,1≤b≤W
I think this problem can be abstracted to:
Return the MAXIMUM SUM of numbers in a*b sub-array of a L*W array
I consider there are (L-a+1)*(W-b+1) possible sums and I have to find the maximum.
My code is here, which cannot give the correct result. My code always returns the sum of the upper-left region of the area, but actually the problem need to return the MAXIMUM SUM of the a*b region among all possible positions. Thanks a lot in advance!
#include <iostream>
#include <cstring>
using namespace std;
int L,W,a,b,i,j,x,y,p,q,r,s;
int main()
{
cin>>L>>W;
int peanut[L][W];
for (i=0;i<L;i++)
for (j=0;j<W;j++)
cin>>peanut[i][j];
cin>>a>>b;
int sumArray[L-a+1][W-b+1];
memset(sumArray,0,(L-a+1)*(W-b+1)*sizeof(int));
for (p=0;p<L-a+1;p++)
for (q=0;q<W-b+1;q++)
{
for (r=p;r<a;r++)
for (s=q;s<b;s++) sumArray[p][q]+=peanut[r][s];
}
int max = 0;
for (x=0;x<L-a+1;x++)
for (y=0;y<W-b+1;y++)
if (sumArray[x][y]>max) max=sumArray[x][y];
cout << max;
return 0;
}
The problem is that you did not index elements carefully to calculate the sumArray. I only changed two lines of your code to make it work.
for (r=0;r<a;r++)
for (s=0;s<b;s++) sumArray[p][q]+=peanut[p+r][q+s];
The following is the full code. Please fix it if there are other problems.
#include <iostream>
#include <cstring>
using namespace std;
int L,W,a,b,i,j,x,y,p,q,r,s;
int main()
{
cin>>L>>W;
int peanut[L][W];
for (i=0;i<L;i++)
for (j=0;j<W;j++)
cin>>peanut[i][j];
cin>>a>>b;
int sumArray[L-a+1][W-b+1];
memset(sumArray,0,(L-a+1)*(W-b+1)*sizeof(int));
for (p=0;p<L-a+1;p++)
for (q=0;q<W-b+1;q++)
{
for (r=0;r<a;r++)
for (s=0;s<b;s++) sumArray[p][q]+=peanut[p+r][q+s];
}
int max = 0;
for (x=0;x<L-a+1;x++)
for (y=0;y<W-b+1;y++)
if (sumArray[x][y]>max) max=sumArray[x][y];
cout << max;
return 0;
}
There are two issues. First of all, a and b are the test region's size.
for (r=p;r<a;r++)
for (s=q;s<b;s++)
sumArray[p][q]+=peanut[r][s];
Here, p and q represent initial indecies, however you are checking the index against the subreigion dimension limits. Instead, it should be:
for (r=p;r<p+a;r++)
for (s=q;s<q+b;s++)
sumArray[p][q]+=peanut[r][s];
The second issue is that 38 is the answer; the topleft-most region contains the maximum of all the subrectangles. Here is my solution that prints all of the values. It should be noted that I'm not familiar with this int arr[X][Y] business when X and Y are not const (couldn't get it to compile), so I changed it to something I'm familiar with.
#include <iostream>
#include <cstring>
using namespace std;
int L,W,a,b,i,j,x,y,p,q,r,s;
int main()
{
cin>>L>>W;
int** peanut=new int*[L]; //a new row pointer for each column element
for (i=0;i<L;i++){
peanut[i]=new int[W]; //the actual row for each column element
for (j=0;j<W;j++)
cin>>peanut[i][j];
}
cin>>a>>b;
//don't need to remember results; just compare maximum after each evaluation.
//int sumArray[L-a+1][W-b+1];
//memset(sumArray,0,(L-a+1)*(W-b+1)*sizeof(int));
int max = 0;
for (p=0;p<L-a+1;++p){
for (q=0;q<W-b+1;++q){
int sum=0;
for (r=p;r<p+a;++r) //r<p ==> r<p+a, prefix increment is always better
for (s=q;s<q+b;++s){ //s<q ==> s<q+b, prefix increment is always better
cout << "(" << r << "," << s << "," << peanut[r][s] << ") ";
sum+=peanut[r][s];
}
cout << sum << endl;
if(sum>max) max=sum;
}
}
/* Merged in previous loop
int max = 0;
for (x=0;x<L-a+1;x++)
for (y=0;y<W-b+1;y++)
if (sumArray[x][y]>max) max=sumArray[x][y];
*/
cout << "Answer: " << max;
//Since I'm in Visual Studio, I need to pause and see the result before quitting...
int s;
cin >> s;
return 0;
}
The problem is this part:
for (r=p;r<a;r++)
for (s=q;s<b;s++) sumArray[p][q]+=peanut[r][s];
You are not iterating a and b times. Change it to:
for (r=p;r<p+a;r++)
for (s=q;s<q+b;s++) sumArray[p][q]+=peanut[r][s];
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Mr. Little Z is looking at a piece of paper and unsuccessfully trying to find the square root of a number written on the paper.
Help Mr. Little Z find the number B which is the square root of BIG number A. The number A has 1000 digits at most and the square root of A will always be an integer.
INPUT:
The first line of the standard input contains the number 0
OUTPUT:
To the standard output write number M, where M represents the length of number B (where B is the square-root of the number A) and in the next M lines write the digits of the number B (from the most significant digit to the least significant).
Input:
3
6
2
5
Output:
2
2
5
Explanation:
The number written on the paper was 625, and its square-root is 25.
My Code :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,m,k,i;
int niza[1001];
cin>>m;
for(int i=0;i<m;i++)
cin>>niza[i];
niza[i]=k;
cout<<sqrt(k);
return 0;
}
Most likely this is because you never set k, and the random value it contains at the start of the program is negative, so sqrt(k) crashes. Also, if m is entered as greater than 1000, you will go outside the array bounds.
Your program will crash at: niza[i] = k because i is past the end of the array.
k is not initialized. You are outputting the square root of an uninitialized variable.
Like #Vilx- said, you will have to check the value of m before your loop.
Also, the sqrt function does not work on an array of numbers, so you will have to come up with a different method to calculate square root based on digits.
You will need to check the status of cin after cin >> m and cin >> niza[i]. Users can and will enter anything, such as letters, which cause failure when a number is expected.
Also, try putting spaces between operators and function names. They don't add anything to the size or speed of an executable, but make reading and maintenance easier.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Question: input an int number ex: ABCD, verify that is ABCD = AB*CD or not
(note that we don't how many digits this number got, just know it is an positive integer
and yes, it the number of digits is odd we can conclude result is No, immediately by... eye lol)
for example:
Enter a number: 88
Output: No
Enter a number: 12600
Output: No
Enter a number: 116725
Output: Yes, 116 * 725 = 116725 (**this is just example,
not actual result, just help to understand how output look like**)
Problem is that, you can't use array, jump and bitwise in order to solve this. Yes, if we can use array then not much to say, put input number in an array, check first half multiply other half....bla..bla... I need help with IDEA without using array to solve this problem, right now I'm stuck! Thank you guy very much!
Your program can safely output No for every input. Proof:
You are looking for integers A and B such that A*B = A*10^k + B, with A and B > 0 and B < 10^k.
If A*B = A*10^k + B, then B = 10^k + B/A > 10^k. But B had to be less than 10^k, so this is a contradiction. Therefore no such A and B exist.
A longer proof:
You are looking for integers A and B such that A*B = A*10^k + B, with A and B > 0 and B < 10^k.
Subtract B from both sides to get (A-1)*B = A*10^k.
Since A is a factor in the right hand side it is also a factor in the left hand side. But A and A-1 are coprime, so A must divide B. So, B = n*A for some integer n.
Now we have A*B = A*10^k + n*A, or A*B = (10^k + n)*A. Since A > 0 we can divide both sides by A to get B = 10^k+n. But this is impossible since B was supposed to be less than 10^k!
A little hint to your 6 digits number:
to get last 3 digits use % 1000
to get first 3 digits use (int) X/1000.
Notice that 1000 == 10^3.
Write a program that asks for the input and then prints "No".
Done.
It's not too clear what you're trying to do. ABCD == AB*CD suggests
four digit numbers. For a four digit number x, a test for the above
would be x == (x / 100) * (x % 100). For a six digit number, replace
100 with 1000, and more generally, for an n digit number, where
n is even, use 10^(n/2). If n is odd, however, I'm not sure what you're looking for, and the last example you give doesn't meet the criteria you mention; if you can permutate the digits in each half, the problem then becomes more complex.
You can read a string, split it in the middle and convert the both parts to an int.
You could also read an int, calculate the number of digits (writing your own loop or using the log function) and split the int after the calculated number of digits.
You can also write a loop that is taking an int ABCD, splitting it into ABC and D and moving the digits from ABC to D while the both have not the same number of digits (you do not need to calculate the number of digits here, there is a quite easy comparison you can do).
To get the number of digits, count how many times you had to divide the number by 10 until it gets smaller than 1.
For 116725, you would need to divide by 10 six times. After that you can print no if the number is odd or calculate the result like James Kanze and ProblemFactory described.
Here's a complete "C/C++" solution:
#include <stdio.h>
int main(void) {
while (fgetc(stdin) != '\n');
return puts("No.");
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I have the following numbers in v_samples (1.4, 2.21, 4.21, 2.1, 5.1)
I get deviance = 2.45122
but when I checked on some deviance calculator websites. they all gave me different answers.
double variance(){
double variance =0 ;
double average = mean();
double size = v_samples.size();
for (size_t i = 0, max = size; i != max; ++i){
variance += (v_samples[i]-average) * (v_samples[i]-average) / size;
}
return variance;
}
edit: variance not deviance
edit: mean() returns 2.804
edit: mean now returns the correct value 3.004
the sample variance now returns 1.97362. is this correct
Sum of
1.4, 2.21, 4.21, 2.1, 5.1
is 15.02 and Mean is 3.004
if you are getting the mean to be 2.804.
2.804 * 5 = 14.02
15.02 and 14.02 are off by 1. You have an off by one error somewhere :)
It should work, so you have a problem in mean().
The confusion in your on-line sources is almost certainly between the "sample variance" and the "bias-corrected sample variance". The latter needs size-1 in the denominator instead of size.
http://mathworld.wolfram.com/Variance.html