Help correct this program.

Hello Everyone,
I am new to this forum/site.I have done an attempt to create a error free code, but I'm getting a wrong output.Please correct me.


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long int rpm, maxRPM, finalRPM,sum=0,rem,temp,count=0,temp1,rem1,shift,k;
int years = 0;
cout << "Enter the RPM of harddisk. Value should be inbetween 3524 and 8524,both inclusive \n";
cin >> rpm;
maxRPM = rpm * 8;
cout << "The maximum limit of the RPM is " << maxRPM << endl;

while(years<10)
{
temp=rpm;
temp1=rpm;
while(temp!=0)
{
rem=(temp%10)*(temp%10);
sum=sum+rem;
temp=temp/10;
count++;
}

sum=sum*323;
rem1=temp1%10;
temp1=temp1/10;
k=count-1;
shift=rem1*pow(10,k) + temp1;
shift=shift%100;
finalRPM=shift+sum;
if(finalRPM>=maxRPM)
break;
else
rpm=finalRPM;

years++;
}

if (years >= 10)
cout << "The harddisk is not affected with the bug.Tested for " << years << " Years. Final RPM is: " << finalRPM << endl;
else
cout << "The harddisk is likely to crash after " << years << " years, with RPM of " << finalRPM << endl;
return 0;
}


FlipDigital: FlipDigital is a company that manufactures harddisks of various RPM (Revolutions Per Minute) to cater to the needs of all individuals. Usually, the life span of their harddisks is more than 10 years, i.e. It should work for atleast 10 years. Unfortunately, a serious bug was detected, due to which the RPM increases or decreases every year in the following manner: (a) multiply the number 323 to sum of the squares of the digits of the RPM, (b) shift the digits of the RPM to the right by 1 positon in a cyclic way, and finally (c) extract the last two digits of the new number obtained and add to the result obtained in 'a'. The final result (c) is the new RPM.

This issue is that if the RPM increases by a certain limit, then the hard disk crashes, which is also dangerous to the person who is using it. The maximum limit of the RPM of the hard disk is 8 times of the RPM, i.e. if RPM of harddisk is 1000, then the maximum RPM can be 1000 * 8 = 8000. This issue needs to be resolved before they release the harddisks to the general public. We have been given a contract to find out (based on the RPM) which harddisks will last for more than 10 years, and which will not.

Assume that the RPM is between 3524 and 8524 only, both inclusive.

Task: In this programming assignment, the value of RPM is taken from the user using 'cin >> rpm' statement. You are required to write code that does the following:-

1)Find out the sum of the squares of the digits of the RPM
2)Multiply the number obtained in (1) by 323
3)Do a cyclic right shift of digits of the RPM i.e. if Number is 1234, after cyclic right shift, the number will be 4123. Thereafter, take the last two digits of the number obtained just now, and add it to the number obtained in point 2. Thus, obtaining a new RPM value.
4)Do these steps till the number of years are 10 or the harddisk has reached the maximum RPM


A correct output console screen:

RPM = 7024
Enter the RPM of harddisk. Value should be inbetween 3524 and 8524, both inclusive 7024
The maximum limit of the RPM is 56192
The harddisk is not affected with the bug. Tested for 10 Years. Final RPM is: 45253
Last edited on
First I recommend using code tags.
http://www.cplusplus.com/articles/jEywvCM9/

A good place to start is to initialize all your values when you declare them.
Next put in some cout statements to make sure your getting desired results and then to calculate where the expected value is off.
Good practice advises:
Always put the {} for the if and else
Don't use break in a if clause. It's for the switch only.
long long int, why not just int or double
choose between endl or \n, don't use both
Don't use break in a if clause. It's for the switch only.
No, it is not. It is commongly used in loops. Here it breaks outer loop when parameters are exceeding critical values.

long long int, why not just int or double
Do not use floating point when your variable is going to be integer. Also doubles will have serious precision problems long before long long is going to overflow.

However in this case a simple long should be enough.

choose between endl or \n, don't use both
Using both are fine. It might not look good from code beauty standpoint, but it is not going to get you in trouble.
Topic archived. No new replies allowed.