[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

spliting a C++ program into a separate function.

Posted on 2007-03-23
9
Medium Priority
?
218 Views
Last Modified: 2012-08-14
c++
below you will find an entire code to calculate depreciation based on the age of a property. now,
i want to split the depreciation calculation only into a separate function, where the age is neither zero nor greater than 15. however , i am lost here. i would appreciate if someone could explain to me of how to solve this problem


#include <iostream.h>



int main()
{
   
    int YearPurchased,Current_Year = 2007,age,count = 0,value,number,start;
      double percent,depreciation,depreciation_part;
      
      
      cout <<"Enter First Property Value:  ";
      cin >> value;
      while (value != -1)
      {
            number=count+1;
            cout <<"Enter year purchsed: ";
            cin >> YearPurchased;
            age= Current_Year - YearPurchased;
            if (age >15)
            {
                  depreciation = double(value);
            }            
            else if (age == 0)
            {
                  depreciation =0;
            }
            else
            {
                  depreciation =0;
                  for (int start=1;start<= age;start++)
                  {
                  
                        switch (start)
                        {
                        case 1:
                              percent=0.12;
                              break;
                        case 2:
                              percent=0.10;
                              break;
                        case 3:
                              percent=0.09;
                              break;
                        case 4:
                              percent=0.08;
                        case 5:
                              percent=0.07;
                              break;
                        case 6:
                              percent=0.06;
                              break;
                        case 7:
                              percent=0.06;
                              break;
                        case 8:
                              percent=0.06;
                              break;
                        case 9:
                              percent=0.06;
                              break;
                        default:
                              percent=0.05;
                        }

                        depreciation_part= double(value) * percent;
                        depreciation = depreciation + depreciation_part;
                        
                  }
            }
            cout <<"Property Number = ";
            cout << number << "\n";

            cout <<"Property Value = ";
            cout << value << "\n";

            cout <<"Depreciation = ";
            cout << depreciation << "\n";
            
            cout <<"Enter Property Value: ";
            cin >> value;
      }
      return 0;
}
0
Comment
Question by:FRANCOIS_DUQUESNE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 18784264
Hi Francois,

This entire program computes depreciation.  It's minimalistic.


What, exactly, do you want the function to do?



Kent
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18784582
you could do like this:
double Depreciation(int age,int value)
{

       double percent,depreciation,depreciation_part;

         if (age >15)
            {
                  depreciation = double(value);
            }            
            else if (age == 0)
            {
                  depreciation =0;
            }
            else
            {
                  depreciation =0;
                  for (int start=1;start<= age;start++)
                  {
                 
                        switch (start)
                        {
                        case 1:
                              percent=0.12;
                              break;
                        case 2:
                              percent=0.10;
                              break;
                        case 3:
                              percent=0.09;
                              break;
                        case 4:
                              percent=0.08;
                        case 5:
                              percent=0.07;
                              break;
                        case 6:
                              percent=0.06;
                              break;
                        case 7:
                              percent=0.06;
                              break;
                        case 8:
                              percent=0.06;
                              break;
                        case 9:
                              percent=0.06;
                              break;
                        default:
                              percent=0.05;
                        }

                        depreciation_part= double(value) * percent;
                        depreciation = depreciation + depreciation_part;
                       
                  }
            }

 return depreciation;
}

0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18784588
Is this a homework?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:MacroLand
ID: 18784627
I would agree with jkr and DeepAbrahamK...

Only one value is calculated within the main function so splitting it into something >>double Depreciation(int age,int value);

However I would prefer with pass by reference,

double Depreciation(int& age, int& value)
{
// rest of DeepAbrahamK's function
}

notice that in case of value=-1 the function can not handle this situation so you should call the function in the main procedure if (value!=-1)

Regards,




0
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 150 total points
ID: 18784656
Yeah sorry it has to be passed by ref. at least int & value. Thanks for correcting

double Depreciation(int age,int& value)
{
double percent,depreciation_part;
.....
...
}

int main()
{
       int YearPurchased,Current_Year = 2007,age,count = 0,value,number,start;
      double depreciation;
     cout <<"Enter First Property Value:  ";
      cin >> value;
      while (value !=0)
      {
            number=count+1;
            cout <<"Enter year purchsed: ";
            cin >> YearPurchased;
            age= Current_Year - YearPurchased;
            depreciation = Depreciation(age,value,number);
           cout <<"Property Number = ";
            cout << number << "\n";

            cout <<"Property Value = ";
            cout << value << "\n";

            cout <<"Depreciation = ";
            cout << depreciation << "\n";
           
            cout <<"Enter Property Value: (Enter 0 to exit the calculation) ";
            cin >> value;
      }
      return 0;
}

Best Regards,
DeepuAbrahamK
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18787043
Howdy guys. I am sorry it took me a while to see the answers.
this is not a homework, i am just trying to solve some problems so i can understand more about C++.

I really appreciate your effort. but i think there is something missing here. for example, there should be at least 2 returns functions: one return depreciation;
and another for return 0;
so for example:
#include <iostream.h>

int Get_Number();         //Prototypes
int Add_1_to_N(int);

void main(void)
{
      int x,sum;
      x = Get_Number();                  //call to function to get the user's number
      sum = Add_1_to_N(x);               //call to adder function, it returns the sum
      cout << "\n The result from adding 1 + 2 + ... + " << x << 
            " is " << sum << endl;
}

int Get_Number()         //Function header line
{
      int number;
      cout <<"\n Enter a number ";
      cin >> number;
      return number;
}

int Add_1_to_N(int n)
{
      int total = 0,i;

      for(i = 1; i <= n; ++i)
      {
            total = total + i;
      }
      return total;
}
so i think that the solution for my problem should be on the same way of writing the program above.
the reason i couldn't solve it because i got confused about how it goes. can you explain to me briefly about the logic here?
thanks guys. i really appreciate your efforts.
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18787598
int Get_Number()         //Function header line
{
      //int number;
      int* number=new int;
      cout <<"\n Enter a number ";
      cin >> *number;
      //return number;
      return *number;
}

int Add_1_to_N(int n)
{

      //int total = 0,i;
      int* total=new int;
      for(i = 1; i <= n; ++i)
      {
            *total = *total + i;
      }
      return *total;
}

creating a variable like int total=0; within the function and trying to return its value gives an error. The reason is this variables are created on the function stack and when the function goes out of scope the variable is automatically deleted so you can not refer it anymore. By int* total=new int; you create the variable on the heap so you can use it whenever you want.
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18787601
int Add_1_to_N(int n)
{  
       int i;
      int* total=new int;
      *total=0;
      for(i = 1; i <= n; ++i)
      {
            *total = *total + i;
      }
      return *total;
}
0
 

Author Comment

by:FRANCOIS_DUQUESNE
ID: 18788349
thanls maco_land. what you are saying is very valuable . but what i was trying to say here is that the "int get_number " problem is just an example of how the "depreciation "problem should be coded. so basically  i would like  to implement the same system ( or the same way the code is written) to the depreciation problem.
thanks a lot.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question