Modularity Using functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Caballero3736
    New Member
    • Oct 2006
    • 7

    #1

    Modularity Using functions

    can some one please help me I need to write a program with 3 function, findaverage, howfarapart and whichisclosest. I got the first one (findaverage for three #) but im having problem with calling howfarapart cause it supposed to receive only two parameters. average and one of the three integer value.
    example, 5.40 and 7 it returns the value 1.60; 5.40 and 5 it returns the value0.40
    here is a copy of the program so far.



    #include<iostre am>
    using namespace std;
    //void introduction();
    void findaverage(dou ble,double,doub le);
    // void howfarapart(dou ble, double);
    int main()
    {
    double numone,numsec,n umthird,average ,num;
    cout<<"Misael Caballero, Project #3 \n"
    <<"This is a program that computes the average of three given numbers \n\n";

    cout<<"Type in three integer values from 1 to 100! \n";
    cin>>numone>>nu msec>>numthird;
    cout<<"the three original integers are:"<<numone<< ","<<numsec<<", "<<numthird<<"\ n"<< endl;

    findaverage( numone, numsec, numthird); //function is call here


    return 0;
    }
    void findaverage(dou ble numone, double numsec, double numthird)

    {
    double average,num,how farapart;

    average=(numone +numsec+numthir d)/3;
    cout<<"the average is "<<average<<"\n \n";

    //num=howfarapart (numone, numsec, numthird);

    }
    void howfarapart(dou ble average, double num)
    {
    double first,second,th ird;
    double numone,numsec,n umthird;

    first=average-num;
    cout<<"the first is "<<first<<" \n";
    second=average-num;
    cout<<"the second is "<<second<<"\n" ;
    third=average-num;
    cout<<"the third is "<<third<<"\n\n ";
    return;
    }
  • teddarr
    New Member
    • Oct 2006
    • 143

    #2
    I've done one similar to this. How is your data going to be input from the user?

    I'll work something up to help you out. Later today!

    Comment

    • Caballero3736
      New Member
      • Oct 2006
      • 7

      #3
      Originally posted by teddarr
      I've done one similar to this. How is your data going to be input from the user?

      I'll work something up to help you out. Later today!
      First the program will take three integer values then the main program call a function named "findaverag e"

      cout<<"Type in three integer values from 1 to 100! \n";
      cin>>numone>>nu msec>>numthird;

      now the function "findaverag e" is called

      void findaverage(dou ble& numone, double& numsec, double& numthird)
      {
      double average;

      average=(numone +numsec+numthir d)/3;
      cout<<"the average is "<<average<<"\n \n";

      So now I need to call a function named "howfarapar t", sending it two parameters the average which I got above and plus one of the three integer values.(the main program calls this function three times, once for each of the three integer values.) If the function determines how far apart the two parameters are.
      e.g. if 5.40 and 7, it returns 1.60; if5.40 and 5, it returns 0.40. the answer returned by the function is always positive or zero.

      Thanks for your help

      Comment

      • teddarr
        New Member
        • Oct 2006
        • 143

        #4
        OK, I have a program that will do the calculations on the 3 numbers, bit I don't have one that will return a value from "findaverag e()" to "main()" and use it. If "findaverag e()" is a void fuction it will not return a vaue to main() to be passed to the next function.

        I'll modify my program and post it. Sorry it's taking so long. more in a few hours.

        Comment

        • Caballero3736
          New Member
          • Oct 2006
          • 7

          #5
          Originally posted by teddarr
          OK, I have a program that will do the calculations on the 3 numbers, bit I don't have one that will return a value from "findaverag e()" to "main()" and use it. If "findaverag e()" is a void fuction it will not return a vaue to main() to be passed to the next function.

          I'll modify my program and post it. Sorry it's taking so long. more in a few hours.



          Here is where im having problems, this function only takes two parameters
          average and num, but now it has to be called three time that is because num has three different values.

          void howfarapart(dou ble& average, double& num)
          {
          double first,second,th ird;

          double numsec,numthird ;
          first=average-numone;
          cout<<"the first is "<<abs(first)<< " from the average"<<"\n";
          second=average-numsec;
          cout<<"the second is "<<abs(second)< <" from the average"<<"\n";
          third=average-numthird;
          cout<<"the third is "<<abs(third)<< " from the average"<<"\n";
          }

          Comment

          • teddarr
            New Member
            • Oct 2006
            • 143

            #6
            Are you allowed to use loops yet? if statements, while statements and such?

            Comment

            • Caballero3736
              New Member
              • Oct 2006
              • 7

              #7
              yes im allow to use any loop, but remenber that the parameters in the function are average and num. The function "howfarapar t" will be call three times so the average will be the same for all thre time thefunction is call,and num is going to change three times with three different integer values.
              average num far apart
              5.40 7 1.60
              5.40 5 .40

              Comment

              • teddarr
                New Member
                • Oct 2006
                • 143

                #8
                Try This!!


                #include <iostream>
                #include <cmath> //for abs()
                using namespace std;

                //function prototypes
                double findaverage(dou ble numone, double numtwo, double numthree);
                double howfarapart(dou ble avg, double num);

                /**************m ain()********** *******/
                void main()
                {
                cout<<"Intro Statement"<<end l;

                double numone = 0;
                double numtwo = 0;
                double numthree = 0;
                double avg = 0;
                double num = 0;
                double how = 0;

                cout<<"Please enter a number: "<<endl;
                cin>>numone;
                cout<<"Please enter another number: "<<endl;
                cin>>numtwo;
                cout<<"Please enter a third number: "<<endl;
                cin>>numthree;

                avg = findaverage(num one, numtwo, numthree);
                cout<<"the average of the 3 numbers entered is: "<<avg<<endl<<e ndl;

                num = numone;
                how = howfarapart(avg , num);
                cout<<numone<<" is "<<how<<" from "<<avg<<"."<<en dl<<endl;

                num = numtwo;
                how = howfarapart(avg , num);
                cout<<numtwo<<" is "<<how<<" from "<<avg<<"."<<en dl<<endl;

                num = numthree;
                how = howfarapart(avg , num);
                cout<<numthree< <" is "<<how<<" from "<<avg<<"."<<en dl<<endl;

                }
                /************fin daverage()***** **********/
                double findaverage(dou ble numone, double numtwo, double numthree)
                {
                int sum = 0;
                int av = 0;

                sum = numone+numtwo+n umthree;
                av = sum/3;

                return av;
                }
                /**************h owfarapart()*** ******/
                double howfarapart(dou ble avg, double num)
                {
                double howfar = 0;

                howfar = abs(avg-num);

                return howfar;
                }


                I think the purpose of this exercise is to teach you to use vaule returning functions. Void functions will not do this. Notice the function prototypes, function definitions, and the return statements of each function.

                Once you understand these concepts you are home free. This was the hardest part for me to grasp. Once I got it...it was like a lightbulb came on. Your on the right track. Just fill in the blank spots and make the jump from void functions to value returning functions and you've got it.

                Thanks for the practice.

                Comment

                • teddarr
                  New Member
                  • Oct 2006
                  • 143

                  #9
                  alternate find average()

                  /************fin daverage()***** **********/
                  double findaverage(dou ble numone, double numtwo, double numthree)
                  {
                  int av = 0;

                  av = (numone+numtwo+ numthree)/3;

                  return av;
                  }


                  Gice whichisclosest( ) a try. I've got a structure in my head that might help if you get stuck.

                  Good Luck.
                  Last edited by teddarr; Oct 30 '06, 05:32 AM. Reason: more info

                  Comment

                  • Caballero3736
                    New Member
                    • Oct 2006
                    • 7

                    #10
                    Thank you very much, you wer right I needed to stay away from the void and take the return value part.
                    for the "whichisclosest " function I will just use a if else statement.
                    one more thing if i need to print out at least 8 sets of data values at the end of the program I need a counter right? but where do I place it? and do I need to ask the user to enter y/n to end the program?


                    Originally posted by teddarr
                    Try This!!


                    #include <iostream>
                    #include <cmath> //for abs()
                    using namespace std;

                    //function prototypes
                    double findaverage(dou ble numone, double numtwo, double numthree);
                    double howfarapart(dou ble avg, double num);

                    /**************m ain()********** *******/
                    void main()
                    {
                    cout<<"Intro Statement"<<end l;

                    double numone = 0;
                    double numtwo = 0;
                    double numthree = 0;
                    double avg = 0;
                    double num = 0;
                    double how = 0;

                    cout<<"Please enter a number: "<<endl;
                    cin>>numone;
                    cout<<"Please enter another number: "<<endl;
                    cin>>numtwo;
                    cout<<"Please enter a third number: "<<endl;
                    cin>>numthree;

                    avg = findaverage(num one, numtwo, numthree);
                    cout<<"the average of the 3 numbers entered is: "<<avg<<endl<<e ndl;

                    num = numone;
                    how = howfarapart(avg , num);
                    cout<<numone<<" is "<<how<<" from "<<avg<<"."<<en dl<<endl;

                    num = numtwo;
                    how = howfarapart(avg , num);
                    cout<<numtwo<<" is "<<how<<" from "<<avg<<"."<<en dl<<endl;

                    num = numthree;
                    how = howfarapart(avg , num);
                    cout<<numthree< <" is "<<how<<" from "<<avg<<"."<<en dl<<endl;

                    }
                    /************fin daverage()***** **********/
                    double findaverage(dou ble numone, double numtwo, double numthree)
                    {
                    int sum = 0;
                    int av = 0;

                    sum = numone+numtwo+n umthree;
                    av = sum/3;

                    return av;
                    }
                    /**************h owfarapart()*** ******/
                    double howfarapart(dou ble avg, double num)
                    {
                    double howfar = 0;

                    howfar = abs(avg-num);

                    return howfar;
                    }


                    I think the purpose of this exercise is to teach you to use vaule returning functions. Void functions will not do this. Notice the function prototypes, function definitions, and the return statements of each function.

                    Once you understand these concepts you are home free. This was the hardest part for me to grasp. Once I got it...it was like a lightbulb came on. Your on the right track. Just fill in the blank spots and make the jump from void functions to value returning functions and you've got it.

                    Thanks for the practice.

                    Comment

                    • teddarr
                      New Member
                      • Oct 2006
                      • 143

                      #11
                      I see 2 ways to look at this problem. If you don't know how many data sets you are going to use then you can place everything in main() after the variable declaration in a while loop "while (repeat =='y' || repeat== 'Y')" and repeat the program infinately. Key here is to declare repeat as a char type variable.

                      char repeat = y;

                      while (repeat =='y' || repeat== 'Y')
                      {
                      .
                      .
                      .
                      }

                      second, for only 8 data sets, place everything above in a while loop "while(coun t <= 8)" and put a "count++" statement just about anywhere inside the while loop. Don't forget to initialize count to 0.

                      int count = 0;

                      while (count <=8)
                      {
                      .
                      .
                      .
                      count++;
                      }

                      that should work or at least get you close.

                      Comment

                      • teddarr
                        New Member
                        • Oct 2006
                        • 143

                        #12
                        Oops, I think you need to initialize count to 1. try it both ways and see which works.

                        Comment

                        • Caballero3736
                          New Member
                          • Oct 2006
                          • 7

                          #13
                          thank you for opening my eyes to so many things I can see now where I was making the mistake. just one last thing this is my last code.
                          this function determines which one is the smallest number out of the three given, and prints a nessage saying that the smallest is closest to the average.

                          I try doing it with void and double. I dont need to print out a value just a message saying which one is closest to the average.

                          thanks

                          /*****whichisclo sest******/
                          double whichisclosest( double& numone,double& numtwo, double& numthree)
                          {
                          double avg;
                          if(avg<=numone)
                          cout<<"the first one is closest to the average";
                          else if(avg<=numtwo)
                          cout<<"the second one is closest to the average";
                          else if(avg<=numthre e)
                          cout<<"the third one is closest to the average";
                          return 0;

                          Originally posted by teddarr
                          alternate find average()

                          /************fin daverage()***** **********/
                          double findaverage(dou ble numone, double numtwo, double numthree)
                          {
                          int av = 0;

                          av = (numone+numtwo+ numthree)/3;

                          return av;
                          }


                          Gice whichisclosest( ) a try. I've got a structure in my head that might help if you get stuck.

                          Good Luck.

                          Comment

                          • Caballero3736
                            New Member
                            • Oct 2006
                            • 7

                            #14
                            the three numbers are from the howfarapart result.


                            Originally posted by Caballero3736
                            thank you for opening my eyes to so many things I can see now where I was making the mistake. just one last thing this is my last code.
                            this function determines which one is the smallest number out of the three given, and prints a nessage saying that the smallest is closest to the average.

                            I try doing it with void and double. I dont need to print out a value just a message saying which one is closest to the average.

                            thanks

                            /*****whichisclo sest******/
                            double whichisclosest( double& numone,double& numtwo, double& numthree)
                            {
                            double avg;
                            if(avg<=numone)
                            cout<<"the first one is closest to the average";
                            else if(avg<=numtwo)
                            cout<<"the second one is closest to the average";
                            else if(avg<=numthre e)
                            cout<<"the third one is closest to the average";
                            return 0;

                            Comment

                            Working...