This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]help user-defined function /maths(solved)  (Read 2304 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]help user-defined function /maths(solved)
« on: April 05, 2011, 03:29:31 PM »
Code: [Select]
#include <iostream>
#include <cmath>

using namespace std;

int espaceconversion(long int);

int main()
{
    long int lightyears;
    long int x;
   
    cout << "enter the lightyear number: ";
    cout << endl;
    cin >> lightyears;
    1*astronomicalunit=15*10000000;
    1*lightyears=1000000000000000000;
    int astronomicalunit = espaceconversion(astronomicalunit);
    cout << lightyears << " light years = ";
    cout << astronomicalunit << " astronomical units. ";
    system("pause");
    return 0;
}
int espaceconversion(long int astronomicalunit)
{
    return astronomicalunit*1000000000000000000/15*10000000;
}
   

5. Write a program that has main() call a user-defined function that takes a distance in
light years as an argument and then returns the distance in astronomical units. The program
should request the light year value as input from the user and display the result, as
shown in the following code:

Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.

An astronomical unit is the average distance from the earth to the sun (about
150,000,000 km or 93,000,000 miles), and a light year is the distance light travels in a
year (about 10 trillion kilometers or 6 trillion miles). (The nearest star after the sun is
about 4.2 light years away.) Use type double (as in Listing 2.4) and this conversion
factor:

1 light year = 63,240 astronomical units.

hello guys Im just making the exercises from the book and Ive found a good problems I just need a bit of help cause the coded that I did adobe is almost complete I just need to deterimine a long int variable if Im not wrong and do the last function something named double function right? well first I want to solve the problem with the main function so plz give me some help and I will an award.

awards: +2 of karma  ;)
« Last Edit: April 13, 2011, 04:44:04 PM by gh0st »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: User-defined function C++ maths
« Reply #1 on: April 05, 2011, 05:40:46 PM »
I don't see what's the point of these lines:
Code: [Select]
    1*astronomicalunit=15*10000000;
    1*lightyears=1000000000000000000;

All you have to do is make the conversion. So you get the number of light years from the user, then use this number as an argument for a function that simply multiplies that number by 63,240 and returns the equivalent in astronomical units. So basically just like this:
Code: [Select]
double espaceconversion(long int lightyears)
{
    return lightyears*63240;
}

See I change the int to double because you are dealing with big numbers here. Scroll down a bit on this page: http://en.wikipedia.org/wiki/C_variable_types_and_declarations - To see the different variables type and what numbers they can handle.
Satan911
Evilzone Network Administrator

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: User-defined function C++ maths
« Reply #2 on: April 05, 2011, 10:56:21 PM »
Code: [Select]
#include <iostream>
#include <cmath>

using namespace std;

double espaceconversion(double);

int main()
{
    double lightyears;
    cout << "enter the lightyear number: ";
    cout << endl;
    cin >> lightyears;
    double astronomicalunit = espaceconversion(lightyears);
    cout << lightyears << " light years = ";
    cout << astronomicalunit << " astronomical units. ";
    system("pause");
    return 0;
}
double espaceconversion(double lightyears)
{
    return lightyears*63240;
}
   

oh Im so dumb I was more focused onto doing a math operation instead of the main problem thank you again Satan +2 of karma ;)

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.