Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Request: C++ Help  (Read 1118 times)

0 Members and 1 Guest are viewing this topic.
Request: C++ Help
« on: March 05, 2008, 05:17:19 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I have an assignment that is due on Wednesday, March 5, 2008, at 3:00 PM CST.  I really need help as my life is pretty stressful right now and I have been preoccupied with a lot.

http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm5.htm

Source code (the functions are what need to be programmed, the rest is just the structure):
Code: [Select]
/******************************************************************************
CSCI 240 - Program 5 - Spring 2008

Name:
Z-Number:
Section:
TA:

Purpose:
******************************************************************************/

#include <iostream>
#include <iomanip>

using namespace std;

/* Function Prototypes */

string Menu();

int getValue( string, int, int );

void doAverage();
double calcAverage( int, int );

void doFactorial();
int calcFactorial( int );

void doMtoNth();
int calcMtoNth( int, int );

void doRange();
int calcRange( int, int, int, int );

int main()
  {
  string userChoice;
 
  userChoice = Menu();
 
  while ( userChoice != "Q" && userChoice != "q" )
    {
    if ( userChoice == "1" )
      {
      doAverage();
      }
    else if ( userChoice == "2" )
      {
      doFactorial();
      }
    else if ( userChoice == "3" )
      {
      doMtoNth();
      }
    else //option "4" was selected
      {
      doRange();
      }

    userChoice = Menu();
    }//end of the while loop

//  system("pause");     //un-comment this line if you are using Dev
  return 0;
  }

/**********  Code your Functions below this line  **********/

Here is all I have done, and I am assuming that I did quite a bit wrong, gah:
Code: [Select]
#include <iostream>
#include <iomanip>

using namespace std;

string Menu();

int getValue( string, int, int );

void doAverage();
double calcAverage( int, int );

void doFactorial();
int calcFactorial( int );

void doMtoNth();
int calcMtoNth( int, int );

void doRange();
int calcRange( int, int, int, int );

int main()
  {
  string userChoice;
 
  userChoice = Menu();
 
  while ( userChoice != "Q" && userChoice != "q" )
    {
    if ( userChoice == "1" )
      {
      doAverage();
      }
    else if ( userChoice == "2" )
      {
      doFactorial();
      }
    else if ( userChoice == "3" )
      {
      doMtoNth();
      }
    else
      {
      doRange();
      }

    userChoice = Menu();
    }

  system("pause");   
  return 0;
  }

string Menu()
{
  cout << "\nSpecial Purpose Calculator"
       << "\n\n1) Average"
       << "\n2) Factorial"
       << "\n3) M to the Nth Power"
       << "\n4) Range"
       << "\n\nQ) Quit"
       << "\n\nEnter your choice: ";
 
  string choice;
 
  cin >> choice;
 
  while ( (choice != "1") && (choice != "2") && (choice != "3") && (choice != "4") && (choice != "Q") && (choice != "q") )
  {
    cout << "Error: "
         << choice
         << " is invalid. Try again: ";
    cin >> choice;
  }
 
  return choice;
}

I could really use some help. I think that my most prevalent problem is understanding fully just how functions return and that I do not know how to do the getValue function done right.  To the user that helps me the most, I will give all of my rupees.
Logged
Re: Request: C++ Help
« Reply #1 on: March 05, 2008, 06:47:50 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
I've just woken up so I can't give you much advice right now. But if you give me a couple of hours to get to college I'll try and explain how to do it.
Logged
Re: Request: C++ Help
« Reply #2 on: March 05, 2008, 07:07:25 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I am probably going to be up all night studying for a quiz, but like I said I have until 3 PM, and I can wait for help too just not forever, lol. But much appreciated.
Logged
Re: Request: C++ Help
« Reply #3 on: March 05, 2008, 10:19:28 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I think I figured everything out, so all I have to do now is make it look neater and add the comments back in:

Code: [Select]
#include <iostream>
#include <iomanip>

using namespace std;

string Menu();

int getValue( string, int, int );

void doAverage();
double calcAverage( int, int );

void doFactorial();
int calcFactorial( int );

void doMtoNth();
int calcMtoNth( int, int );

void doRange();
int calcRange( int, int, int, int );

int main()
  {
  string userChoice;
 
  userChoice = Menu();
 
  while ( userChoice != "Q" && userChoice != "q" )
    {
    if ( userChoice == "1" )
      {
      doAverage();
      }
    else if ( userChoice == "2" )
      {
      doFactorial();
      }
    else if ( userChoice == "3" )
      {
      doMtoNth();
      }
    else
      {
      doRange();
      }

    userChoice = Menu();
    }

  system("pause");   
  return 0;
  }

string Menu()
{
  cout << "\nSpecial Purpose Calculator"
       << "\n\n1) Average"
       << "\n2) Factorial"
       << "\n3) M to the Nth Power"
       << "\n4) Range"
       << "\n\nQ) Quit"
       << "\n\nEnter your choice: ";
 
  string choice;
 
  cin >> choice;
 
  while ( (choice != "1") && (choice != "2") && (choice != "3") && (choice != "4") && (choice != "Q") && (choice != "q") )
  {
    cout << "Error: "
         << choice
         << " is invalid. Try again: ";
    cin >> choice;
  }
 
  return choice;
}

int getValue( string prompt, int lowerBound, int upperBound)
{
  int value;
 
  cout << prompt
       << " between "
       << lowerBound
       << " and "
       << upperBound
       << ": ";
  cin >> value;
 
  while ( (value < lowerBound) || (value > upperBound) )
  {
    cout << "Error. "
         << prompt
         << " between "
         << lowerBound
         << " and "
         << upperBound
         << ": ";
    cin >> value;
  }
 
  return value;
}

void doAverage()
{
  int sum;
  int count;
  double average;
 
  sum = getValue("Enter a sum", -1000, 1000);
  count = getValue ("Enter a count", 0, 1000);
 
  average = calcAverage(sum, count);
 
  cout << "The average is: "
       << average
       << ".\n";
}

double calcAverage(int s, int c)
{
  double average;
 
  average = (s / c);
 
  return average;
}

void doFactorial()
{
  int facvalue;
  int finvalue;
 
  facvalue = getValue("Enter a value", 0, 10);
 
  finvalue = calcFactorial(facvalue);
 
  cout << "The factorial of "
       << facvalue
       << " is: "
       << finvalue
       << ".\n";
 
}

int calcFactorial(int f)
{
    int i = 1;
    int total = f;
   
    for (i = 1; i < f; i++)
      total *= i;
   
    return total;
}

void doMtoNth()
{
  int initm;
  int initn;
  int final;
 
  initm = getValue("Enter the M value", 1, 10);
  initn = getValue("Enter the N value", 1, 8);
 
  final = calcMtoNth(initm, initn);
 
  cout << initm
       << " raised to the "
       << initn
       << "th power is: "
       << final
       << ".\n";
}

int calcMtoNth(int m, int n)
{
  int temp = 1;
  int i = 0;
 
  for(i = 0; i < n; i++)
    temp *= m;
 
  return temp;
}

void doRange()
{
  int a, b, c, d, ranger;
 
  a = getValue("Enter a number", 0, 10000);
  b = getValue("Enter a number", 0, 10000);
  c = getValue("Enter a number", 0, 10000);
  d = getValue("Enter a number", 0, 10000);
 
  ranger = calcRange(a, b, c, d);
 
  cout << "The range of "
       << a
       << ", "
       << b
       << ", "
       << c
       << ", "
       << d
       << " is: "
       << ranger
       << ".\n";
}

int calcRange(int fi, int se, int th, int fo)
{
  int min = fi, max = fi, range;
 
  if (se <= min)
    min = se;
  if (th <= min)
    min = th;
  if (fo <= min)
    min = fo;
   
  if (se >= max)
    max = se;
  if (th >= max)
    max = th;
  if (fo >= max)
    max = fo;
   
  range = max - min;
 
  return range;
}
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.039 seconds with 42 queries.