PDA

View Full Version : C++ homework - I'm stuck, can you help?


adamzx
10-10-2006, 06:53 AM
do any of you know C++ enough to help a brotha out?
my homework is pissing me off

simon
10-10-2006, 10:47 AM
I'll help if you show you've tried ;)

jimmyjones
10-10-2006, 04:55 PM
ahh c++, the only class that i got over 90% in college, whats the problem? i can give it a shot. Anyone wanna do my Poli Sci paper, its only 2000 words, i know you all want to!

noonebutme
10-10-2006, 06:17 PM
ahh c++, the only class that i got over 90% in college, whats the problem? i can give it a shot. Anyone wanna do my Poli Sci paper, its only 2000 words, i know you all want to!
Post the assignment - even if im not gonna do it for ya, i'll try & point ya in the right direction :)

adamzx
10-13-2006, 06:39 PM
I don't have my code with me on this PC - It's all on my gf's laptop, I'll post it on here later when I see her.
Thanks for your help guys

Heres the problem:

1. Write a program which displays the following pattern:

*
**
***
****
*****
******
*******
……
(other rows follow)

The number of lines can vary from 1 to N where N is the total number of lines to be displayed. Prompt the user for the value of N. The first line always has 1 star, and all subsequent lines have 1 star more than the number of stars on the previous line. Allow the user to run this program as many times as desired:

Example Input/Output

Enter number of rows: 3

*
**
***

Run program again? (y/n): y

Enter number of rows: 7

*
**
***
****
*****
******
*******

Run program again? (y/n): n

Program exiting….

2. Write a program that computes the approximate sine of an angle (θ) by using the following infinite series:

Approximate sine of θ = θ – (θ3/3!) + (θ5/5!) - (θ7/7!) + (θ9/9!) .. (other terms follow)

Where θ is in radians and the factorial computations are given as

3! = 3*2*1
5! = 5*4*3*2*1
7! = 7*6*5*4*3*2*1
9! = 9*8*7*6*5*4*3*2*1

N! = N*(N-1)*(N-2)…..*1

The user is to input the number of terms used in the computation. For example, the equation below:

Approximate sine of θ = θ – (θ3/3!) + (θ5/5!) - (θ7/7!) + (θ9/9!)

Computes the sin(θ) to 5 terms.

Perform the following steps:

1.Prompt the user for an angle between 0 and 360 degrees
2.Convert this input value to radians
3.Prompt the user for the number of terms to be used for approximating the sine of this angle.
4.Compute the approximate sine using nested for loops using the equation above.
5.Use the C++ library function to compute the sine of theta (sin(θ)).
6.Compute the Percent Error between the approximate value of sine computed in step 4 and the sine library function computation in step 5. The Percent Error formula is given below:

%error = ((1.0 – sin(θ))/approximate_sine_ θ)*100

Where sin(θ) is the C++ library function used for computing sine of an angle


7.Allow the user to run the program as many times as desired

Example Input/Output

Enter theta: 45
Appx. Sine of theta: 0.706106
Computed Sine of theta: 0.707106
%error = 0.10% error

Enter another angle? (y/n): n

Program exiting….

adamzx
10-13-2006, 09:32 PM
#include <iostream>
#include <cctype>
#define VIEW '*'
using namespace std;


int main()
{

int i,
j,
x;
bool repeat=true;

while ( true ){
cout << "Enter the number of star lines (1 to 20) to be printed: " << endl;
cin >> x;
cout <<endl;
if (!(cin>>x)) {
cin.clear();
cin.ignore(INT_MAX,'\n');
cout << "Enter a number dummy." << endl;
continue;
}
if ( x<=20 && x>0 ){
repeat=false;

for ( i=1; i<=x; i++ )
{
for ( j=1; j<=x-i; j++ )
cout<<" ";

for ( j=1; j<=2*i-1; j++ )
cout<<VIEW;
cout<<"\n";
}
}

//else if ( isdigit (x)){
// cout<<"Please enter an integer."<<endl;
// }


if ( x<=20 && x>0 )
break;
}

cout<<"\a"<<endl;
if (!(cin>>x)) {
cin.clear();
cin.ignore(INT_MAX,'\n');
cout << "Enter a number dummy." << endl;
continue;
}

system ("PAUSE");
return 0;

}

pamich
10-14-2006, 12:00 AM
The first one can be done with a for where you increase the number of *s printed until they reach the entered value.

The second can be done with a recursive function in which you have θ*n/n! (factorial function would be good) where it is added to another call of the function with n+2 until a counter reaches the requested number of terms.



Note: I'm learning C, so that stuff may not 100% apply to C++.

adamzx
10-14-2006, 02:58 AM
bump
i need help
:(

wastern
10-14-2006, 03:30 AM
Not to be a dick, but do you're own bloody homework. Everyone is always looking for the easy way out and think all their problems should just be answered like magic on the internet

read the book, go to class, study, do the work. You're there to learn. Having someone hand you over an answer on some forum isn't going teach you anything and that will be shown clearly when exam time comes

klitzy
10-14-2006, 03:36 AM
Not to be a dick, but do you're own bloody homework. Everyone is always looking for the easy way out and think all their problems should just be answered like magic on the internet

read the book, go to class, study, do the work. You're there to learn. Having someone hand you over an answer on some forum isn't going teach you anything and that will be shown clearly when exam time comes

Not to be a dick? Too late....No JK and I agree with you. People, as myself sometimes, think all answers are on the internet. Most are. But....Yeah, better to just do the work.

adamzx
10-14-2006, 04:28 AM
I didn't ask you to tell me all of the answers, I asked for help.
there is a difference

I was asking for someone to take a look at what I have for an answer and let me know if I'm on the right track - and what I need to adjust to make it come out right.

wastern, thanks for your input jackass.

pamich
10-14-2006, 04:31 AM
Play nice kids.

sevver
10-14-2006, 04:33 AM
I think it is better for you to post what code you have for analysis before you ask for help. You will get a better responce.

wastern
10-14-2006, 06:02 AM
I think it is better for you to post what code you have for analysis before you ask for help. You will get a better responce.

exactly. just asking for help and posting the what you need to accomplish isn't asking for advice on where you went wrong. The code didn't come into play until much later after the thread was posted.

not trying to be a "jackass", but I've been seeing more and more kids trying to take the easy road out getting all their answers from the internet.

kyro
10-14-2006, 10:14 AM
All answers can be found on the internet.

Thanks, Al Gore.

synack
10-14-2006, 11:24 AM
I didn't ask you to tell me all of the answers, I asked for help.
there is a difference


Sup,
I'm hearing you on FM :) This text *might* help you out;

Thinking in C++, Eckel, Bruce

I think that some background in vanilla C may also help you understand some programming concepts such as data structures and algorithms (essential for understanding operating system source code).

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Try 'The Joy of C'. Here is a few code examples from the book.

http://www.ee.hawaii.edu/~alex/TextBooks/JoyOfC/progchaps.html

I found the best way to learn C/C++ was to read source code, then try and write your own. If you plan on using OpenGL, you really should get your head around C/C++

synack.

adamzx
10-14-2006, 06:41 PM
Sup,
I'm hearing you on FM :) This text *might* help you out;

Thinking in C++, Eckel, Bruce

I think that some background in vanilla C may also help you understand some programming concepts such as data structures and algorithms (essential for understanding operating system source code).

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Try 'The Joy of C'. Here is a few code examples from the book.

http://www.ee.hawaii.edu/~alex/TextBooks/JoyOfC/progchaps.html

I found the best way to learn C/C++ was to read source code, then try and write your own. If you plan on using OpenGL, you really should get your head around C/C++

synack.

Thanks, Synack - I appreciate your help!!
:)