Help me please on making a program?

I

Irina Danilyuk

For one of the questions on the assigment sheet it said to write a
complete C++ program, including comments, to do the following:
Your program will compute the values of a formula that expresses y in
terms of x. The formula is:
where B...B means "absolute value of" and (...)1â„2 means "square root
of". (Use the built-in functions for these operations.)
1. The program should start by printing a message saying this is the
output of your first program.
2. Then, it should evaluate the formula starting with x = -3, going up
by 0.5 each time, until x reaches 4. Therefore, it will use values x:
-3, -2.5, ..., -0.5, 0, 0.5, ..., 3.5, 4. For each value of x, the
program should compute the corresponding value of y. It should print
these values together with explanations of what the values represent.
For example, it could print the string 'X = ', then the value of x,
then the string 'Y = ', then the value of y, and then a message. (It
is also possible to use column headings instead if you desire.) The
message should say one of three things:

a) If the value of y is exactly 0, the message should say 'Y IS ZERO'.
b) If the value of y is positive, the message should say 'Y IS
POSITIVE'. c) If the value of y is negative, the message should say 'Y
IS NEGATIVE'.
A typical line of output would then be:
X = -2.5 Y = 1.23456 Y IS POSITIVE (in actuality, this may not be the
value for y).

3. Once you have finished using x = 4, the program should print a
message (underneath the last line of output) stating that the program
is halting. Then, stop.

4. Have your program find which of the y values is closest to 10
(either larger or smaller). have the program print the x value that
gives this closest y value. Also, print how close the y value is to
10.
5. Have your program count how many times the formula yields positive,
negative, and zero results. Print these three values.

So far I have worked with a different program, and had a code done for
it, to help me with this assignment, but I do not understand where I
should be posting them to complete the program. because both of the
problems are similar.

/* program prob2
* create a table to evaluate a formula result = f(gpa)
*/

#include <iostream>
using namespace std;

int main()
{
double gpa,result;

cout << "Table of Function Values" << endl << endl;
cout << "Grade Point Average Value of Formula Status" <<
endl;
for (gpa = 0.00; gpa <= 4.00; gpa = gpa + 0.50)
{
result=(gpa*gpa*gpa+7*gpa-1)/(gpa*gpa - (gpa+5)/3);
cout << " " << gpa << " " << result;
if (result >= 0)
cout << " Admit";
cout << endl;
}
cout << endl << "The table is finished" << endl;

// system("pause"); //un-comment when using DevC++
return 0;
}
 
I

Irina Danilyuk

...

Irina, I will be happy to help you write your program. However, I am not
going to write it for you, nor will I take some other program that
someone wrote for you and change it so that it works in this context.

What I am willing to do is to help you attack the requirements in a
reasonable order.

So to start... Write a complete C++ program that prints the following:

   This is the output of my program:
   ...
   The program is halting.

Once you have done that, you will have finished steps 1 and 3 of your
assignment and I will direct you as to what to do next.

If you can't figure out how to write a program that outputs the three
lines of code above, post your best attempt here.






Heyyy, do you have an email that you can help me work this out with
pleaseeee ??
 
A

Alf P. Steinbach /Usenet

* Irina Danilyuk, on 07.01.2011 02:13:
Heyyy, do you have an email that you can help me work this out with
pleaseeee ??

Irina, Ian indicated that he will help you by the method of teaching you how to
fish, instead of giving you a fish. That way, you'll not go hungry after you
have eaten the single fish, because you can fish some more yourself.

Asking, with heavy girlie signalling, for a more private venue, tells me (and
others) that you hope to convince Ian, by way of Girl Power(TM), to just give
you the fish -- but believe me, nobody here will do that, except perhaps to
teach you a lesson by giving you something your prof will see is not your work.

One reason is that if somebody does give you a "solution" that you can hand in,
and somebody also does that for your next assignment, and so on, then you may
graduate as an ignorant, but with good marks. And then at your first job you'll
be a burden for your colleagues. They will have to do your work for you.

So, better do as Ian suggested.

Just give it a go, do your best, and if you can't, post what you managed to do.


Cheers & hth.,

- Alf
 
P

Pavel Lepin

Irina said:
For one of the questions on the assigment sheet it said to write a
complete C++ program, including comments, to do the following:
Your program will compute the values of a formula that expresses y in
terms of x. The formula is:
where B...B means "absolute value of" and (...)1â„2 means "square root
of". (Use the built-in functions for these operations.)
1. The program should start by printing a message saying this is the
output of your first program.
2. Then, it should evaluate the formula starting with x = -3, going up
by 0.5 each time, until x reaches 4. Therefore, it will use values x:
-3, -2.5, ..., -0.5, 0, 0.5, ..., 3.5, 4. For each value of x, the
program should compute the corresponding value of y. It should print
these values together with explanations of what the values represent.
For example, it could print the string 'X = ', then the value of x,
then the string 'Y = ', then the value of y, and then a message. (It
is also possible to use column headings instead if you desire.) The
message should say one of three things:

a) If the value of y is exactly 0, the message should say 'Y IS ZERO'.
b) If the value of y is positive, the message should say 'Y IS
POSITIVE'. c) If the value of y is negative, the message should say 'Y
IS NEGATIVE'.
A typical line of output would then be:
X = -2.5 Y = 1.23456 Y IS POSITIVE (in actuality, this may not be the
value for y).

3. Once you have finished using x = 4, the program should print a
message (underneath the last line of output) stating that the program
is halting. Then, stop.

4. Have your program find which of the y values is closest to 10
(either larger or smaller). have the program print the x value that
gives this closest y value. Also, print how close the y value is to
10.
5. Have your program count how many times the formula yields positive,
negative, and zero results. Print these three values.

So far I have worked with a different program, and had a code done for
it, to help me with this assignment, but I do not understand where I
should be posting them to complete the program. because both of the
problems are similar.

Here you go. Might be a few minor bugs in it, but nothing too bad.

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <ostream>
#include <sstream>
#include <vector>
using namespace std;

float f(float x) { return sqrt(abs(400 * x)) - 20; }

template<typename T,typename X,typename Y>struct mi:
public iterator<input_iterator_tag,Y,ptrdiff_t,const Y*,const Y&>{
T i_;Y(*f_)(X);mutable Y y_;mi(T i,Y(*f)(X)):i_(i),f_(f){}
const Y&operator*()const{y_=f_(*i_);return y_;}
mi<T,X,Y>&operator++(){++i_;return*this;}
bool operator==(const mi<T,X,Y>&r)const{return i_==r.i_;}
bool operator!=(const mi<T,X,Y>&r)const{return i_!=r.i_;}
};

float x1(float x){return x/2-3;}
pair<float,float>f0(float x){return pair<float,float>(x,f(x));}
string o1(pair<float,float>x){stringstream ss;
ss<<"x = "<<x.first<<" y = "<<x.second<<" y is "<<
(x.second==0?"zero":(x.second>0?"positive":"negative"));
return ss.str();}
template<typename T>void p(T x){cout<<x<<endl;}
pair<float,pair<float,float> >m0(pair<float,float> x){
return pair<float,pair<float,float> >(abs(10-x.second),x);}
bool cd(pair<float,pair<float,float> >a,pair<float,
pair<float,float> >b){return a<b;}
pair<float,pair<float,float> >st(pair<float,pair<float,float> >a,
pair<float,float>b){if(b.second==0)++a.first;
else if(b.second>0)++a.second.first;else++a.second.second;
return a;}

int main(){
vector<float>x0s;
cout<<"this is the output of someone's first program"<<endl;
for(float i=0;i<15;i++)x0s.push_back(i);
mi<vector<float>::const_iterator,float,float>x1l(x0s.begin(),&x1),
x1r(x0s.end(),&x1);
mi<mi<vector<float>::const_iterator,float,float>,float,
pair<float,float> >x2l(x1l,&f0),x2r(x1r,&f0);
mi<mi<mi<vector<float>::const_iterator,float,float>,float,
pair<float,float> >,pair<float,float>,string>x3l(x2l,&o1),x3r(x2r,&o1);
for_each(x3l,x3r,pointer_to_unary_function<string,void>(&p));
cout<<"the program is halting! (just kidding, it really isn't)"<<endl;
mi<mi<mi<vector<float>::const_iterator,float,float>,float,
pair<float,float> >,pair<float,float>,pair<float,pair<float,float> > >
x4l(x2l,&m0),x4r(x2r,&m0);
pair<float,pair<float,float> >m=*min_element(x4l,x4r,
pointer_to_binary_function<pair<float,pair<float,float> >,
pair<float,pair<float,float> >,bool>(&cd));
cout<<"x = "<<m.second.first<<" y = "<<m.second.second<<
" d = "<<m.first<<endl;
pair<float,pair<float,float> >s=accumulate(x2l,x2r,
pair<float,pair<float,float> >(0,pair<float,float>(0,0)),st);
cout<<"0: "<<s.first<<" >: "<<s.second.first<<
" <: "<<s.second.second<<endl;
return 0;}
 
F

Fred

I happy to announce that Irina got pretty far in her assignment. Mostly
without help from me. This is the last thing she sent me via email. It's
not what I would have done exactly, nor is it complete yet, but it shows
good effort on her part. Hopefully, she will be able to attack her next
assignment without as much trepidation.

#include <iostream>

using namespace std;

int main()
{
    double X,result;

    cout << "\t\t\tOutput of My First Program" << endl << endl;
    cout << "X\t\t\tY\tStatus" << endl;
    for (X = -3; X <= 4.00; X = X + 0.50)
<snip>

Someone should point out the danger in using a loop this way.
It may produce the correct number of lines of output for
an incrementof 0.5, but may produce an extra line if the
increment is 0.1, since 3.0+0.1 != 3.1 on a binary machine.

Also, depending on the formula, it may produce an incorrect
count of positives and negatives if the true answer is zero
but the approximate answer produced by the calculation is
not exactly zero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top