| View previous topic :: View next topic |
| Author |
Message |
wylun Expert Cheater
Reputation: 0
Joined: 27 Jun 2006 Posts: 101
|
Posted: Thu May 14, 2009 2:16 pm Post subject: HELP ME (C++ building a sin() table for calculation) |
|
|
Description:
For this assignment, you will be building a sin() table, and then using the sin() table to perform some calculations.
Task:
Create a C++ program that will do the following:
a) Create and initialize a sin() table. The size of this table should be 360, with each element of the table storing the sin() value. Hence, the index into the array represents degrees.
b) Prompt the user for 2 numbers which represents a vector using Polar coordinates.
c) Convert this Polar coordinate into Cartesian coordinate in the following way:
i. Using the sin() table you created, determine the Y-coordinate of this Polar coordinate.
ii. Once you have the Y-coordinate, use Pythagoras’ theorem to determine the X-coordinate.
iii. Output the Cartesian coordinate that you just computed with an appropriate message.
E.g The Polar coordinates you enter 10@30 degrees
has Cartesian coordinates (5, 8.6)
d) The program should repeat this process of allowing the user to input 2 numbers and converting them into Cartesian coordinates. However, if the input numbers are -99, -99, the program should exit with an appropriate message.
| Code: | #include <iostream>
#include <math.h>
using namespace std;
double sin_table[360];
double x, y;
void main()
{
for(int i = 0; i < 360; i++)
{
sin_table[ i ] = sin( i * (3.142/180));
}
float mag, angle;
int st = 5;
do
{
cout << " Please enter -99 into both input to exit the program "<<endl<<endl;
cout << " Please enter 1st number for magnitude :";
cin >> mag;
cout << " Please enter 2nd number for degrees :";
cin >> i;
cout << endl<<endl;
// program will start calculate if both key in not -99
if ( mag != -99 && angle != -99 )
{
// do the calculation here
y = mag * sin_table[ i ];
x = sqrt(pow(mag,2) - pow(y,2));
cout << " The Program will ask for input again in few second "<<endl;
// server as time count down to restart the whole program
for ( int i=0; i<99 ; i++ ){
for (int y=0; y<999 ; y++){
for(int a=0; a<999; a++){};
};};
// screen clear
system("cls");
}
//when the magnitude and angle both equal as -99, the program will go off
} while(mag != -99 && angle != -99);
cout << "y = " << y << endl;
cout << "x = " << x << endl;
cout << "The Cartesian coordinate for the Polar is " << y << "," << x << endl;
cout << "Thank you for using the program " << endl;
cout << "The program going to end now " << endl;
cout << endl;
return;
} |
What is my programme problems? anyone help me pls! Urgent
_________________
OWE REV 791 ,REV800,REV822,REV833
(quit maple) Currently playing MaplePrivateServer. |
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Fri May 15, 2009 12:43 pm Post subject: |
|
|
Why are you using an if and a do/while for the same thing?
Your array sin_table[360] has length 361 (remember to count the 0) you only need sin_table[359]. The for is fine though.
Edit: I don't even see why you need a while there. The only loop you'd need is to make the sine array.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri May 15, 2009 1:09 pm Post subject: |
|
|
@OP: tl;dr. :D Just wanted to comment the other reply.
| shhac wrote: | | Your array sin_table[360] has length 361 (remember to count the 0) you only need sin_table[359]. | Actually. sin_table[360] has the length of 360. sin_table[0] would have zero "slots." With 359 slots, you'll have an overflow.
You totally mixed up reserving space for an array and referencing to an array. Reserving space, the number means the amount of memory reserved and referencing, the number is an offset, which means how many slots do you count forward from the start of the array. | Code: | int arrayA[1];
arrayA[0] = 0; // OK!
arrayA[1] = 0; // Overflow!
int arrayB[0];
arrayB[0] = 0; //Overflow! |
|
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Sat May 16, 2009 4:57 am Post subject: |
|
|
That's what I get from never learning C++.
At least my point about not needing the while is valid... right?
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat May 16, 2009 7:32 am Post subject: |
|
|
| Code: | int * arrayA = malloc(sizeof(int)*512);
free(arrayA);
|
|
|
| Back to top |
|
 |
wylun Expert Cheater
Reputation: 0
Joined: 27 Jun 2006 Posts: 101
|
Posted: Sun May 17, 2009 3:53 am Post subject: Here is my final answer. |
|
|
| Code: | #include <iostream>
#include <math.h>
using namespace std;
double sin_table[360];
double x, y;
void main()
{
for(int a = 0; a < 360; a++)
{
sin_table[ a ] = sin( a * (3.142/180));
}
float tude, angle;
cout << " To Exit the program please kindly enter (-99) into both input."<<endl<<endl;
do
{
cout << " Please enter a number for magnitude :";
cin >> tude;
cout << " Please enter a number for degrees :";
cin >> angle;
cout << endl<<endl;
if ( tude > 0 && angle > 0 )
{
y = tude * sin_table[ a ];
x = sqrt(pow(tude,2) - pow(y,2));
for ( int a=0; a<99 ; a++ ){
for (int y=0; y<999 ; y++){
for(int a=0; a<999; a++){};
};};
cout << " y = " << y << endl;
cout << " x = " << x << endl;
cout << " The Cartesian coordinate for the Polar is " << y << "," << x << endl;
}
} while(tude != -99 && angle != -99);
cout << " Thank you for using my program " << endl;
cout << " Bye Bye! " << endl;
cout << endl;
return;
} |
_________________
OWE REV 791 ,REV800,REV822,REV833
(quit maple) Currently playing MaplePrivateServer. |
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Fri May 22, 2009 10:04 am Post subject: Re: Here is my final answer. |
|
|
| wylun wrote: | | Code: | for ( int a=0; a<99 ; a++ ){
for (int y=0; y<999 ; y++){
for(int a=0; a<999; a++){};
}; |
|
...yaknow, there's this sexy API called Sleep().. AND YOU CAN ACTUALLY CONTROL THE AMOUNT OF TIME TO SLEEP!
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sun May 24, 2009 10:47 pm Post subject: Re: Here is my final answer. |
|
|
| talker0 wrote: | | wylun wrote: | | Code: | for ( int a=0; a<99 ; a++ ){
for (int y=0; y<999 ; y++){
for(int a=0; a<999; a++){};
}; |
|
...yaknow, there's this sexy API called Sleep().. AND YOU CAN ACTUALLY CONTROL THE AMOUNT OF TIME TO SLEEP! |
I personally dont like to hold the main thread with sleep. Is that just me?
_________________
|
|
| Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Mon May 25, 2009 5:02 pm Post subject: Re: Here is my final answer. |
|
|
| blankrider wrote: | | talker0 wrote: | | wylun wrote: | | Code: | for ( int a=0; a<99 ; a++ ){
for (int y=0; y<999 ; y++){
for(int a=0; a<999; a++){};
}; |
|
...yaknow, there's this sexy API called Sleep().. AND YOU CAN ACTUALLY CONTROL THE AMOUNT OF TIME TO SLEEP! |
I personally dont like to hold the main thread with sleep. Is that just me? |
Well, for GUI applications then yea holding the main thread sucks. For console its alright.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue May 26, 2009 7:48 pm Post subject: Re: Here is my final answer. |
|
|
| dnsi0 wrote: | | blankrider wrote: | | talker0 wrote: | | wylun wrote: | | Code: | for ( int a=0; a<99 ; a++ ){
for (int y=0; y<999 ; y++){
for(int a=0; a<999; a++){};
}; |
|
...yaknow, there's this sexy API called Sleep().. AND YOU CAN ACTUALLY CONTROL THE AMOUNT OF TIME TO SLEEP! |
I personally dont like to hold the main thread with sleep. Is that just me? |
Well, for GUI applications then yea holding the main thread sucks. For console its alright. |
I never read the context of how he used the for statements and i justwanted input on what people think of using it compared to sleep.
The immediate downfall is it could be very fast on some comps (like most of ours) and slow on others. Whereas sleep is universal.
_________________
|
|
| Back to top |
|
 |
blackmorpheus Expert Cheater
Reputation: 0
Joined: 05 Apr 2008 Posts: 159
|
Posted: Fri May 29, 2009 12:55 pm Post subject: |
|
|
Yeah or you could use an algorithm for the taylor series.
straight from my head:
float getSinus(a)
{
for ( int i = 0; i < 100; i++)
{
for ( n = 1; n <= i; n++ )
{
int factorial * = (2 *n + 1) ;
}
float answer += pow(-1 , i) * pow(a , 2*i + 1) / factorial
}
return answer;
}
|
|
| Back to top |
|
 |
|