Solve it....

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Start of Local Declarations
    double numberOfPages;
    double beforeTenPages;
    double afterTenPages;
   //End Of Local Declarations

    //prompt message for user
    cout << "Please enter the number of pages to be faxed :: ";
    cin  >> numberOfPages;
    if (numberOfPages >=0 && numberOfPages <=10)
    {
        beforeTenPages = 3.0 + 0.20 * numberOfPages;
        cout << "The cost of sending international fax is     :: "<<beforeTenPages;
    }
    else
    {
        afterTenPages = (3.0 + 0.20 * 10 )* (numberOfPages - 10 + 0.10);
        cout << "The cost of sending international fax is     :: "<<afterTenPages;
    }

Last edited on
You need to utilize loop for this.

Do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Number of Pages is always integer. 
// There is no such thing as 1.2 pages. So you don't use double type.
Declare int type for the variable, numberOfPages.
Declare double type for the variable, cost. cost must be initialized to 0.

Prompt user to enter the number of pages to be faxed.
Store user input in numberOfPages.

// If the number of pages is greater than 0, do the following
if ( numberOfPages > 0 )
    Add 3.0 to the cost variable

    // Use for-loop to run iteration for each page
    for ( Each Page, run the loop. Keep a counter variable, iter )
        // For the first 10 pages
        if ( iter is less than or equal to 10 )
            Add 0.20 to cost
        if ( iter is greater than 10 )
            Add 0.10 to cost
// If the number of pages is 0 or less
else
    cost equals 0 Rs

Display the result to user



Try to implement this pseudocode. You need to get the hang of utilizing loops. Then if you need further help, reply to this thread.
Last edited on
why it is necessary to initialized 0 ????
why it is necessary to initialized 0 ????

What do you think it contains if you don;t initialize it?
Hint: garbage.

You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  //Start of Local Declarations
    int numberOfPages;
    int i;
    double cost=0;
   //End Of Local Declarations

    //prompt message for user
    cout << "Please enter the number of pages to be faxed :: ";
    cin  >> numberOfPages;

    if (numberOfPages > 0)
    {

        for (i = 1 ; i <= numberOfPages ; i++)
        {
            cost = 3.0;
            if (i <=10)
            {
             cost= cost + 0.20;
            }
            if (i>10)
            {
                cost = cost+ 0.10;
            }

        }
        cout << cost;
    }
i did this but it did not give true result it gave me still 3.1 whatever i input please  point out my mistakes and explain me
Last edited on
Each time through the loop, you reset cost to 3.0. You need to initialize the fixed value of cost BEFORE you enter the loop.

BTW, there is no need for a for loop here. You can calculate the cost without using a loop.

WHAT PART OF USING CODE TAGS DO YOU NOT UNDERSTAND?
I will not respond further until you apply code tags.
http://www.cplusplus.com/articles/jEywvCM9/
Just as AbstractionAnon stated, you have no idea what value a variable could be storing when you first declare it.
 
double test;

That test variable could be storing 0 (if you are lucky) or -242342423. You don't know.

But if you do this,
 
double test = 0;

You know for sure that test now has 0 and later in the program, you can utilize the following without worrying about what initial value the variable might have
 
test += 5;        // test = test + 5 

That's why you want to initialize some variables.

Like AbstractionAnon said, I'm sure there is a way to solve this problem without utilizing the loop (As a matter of fact, your original code might even work after adjustment now that I take a second look at it). You have * instead of + between (3.0 + 0.20 * 10 ) and (numberOfPages - 10 + 0.10). And it is supposed to be (numberOfPages - 10) * 0.10, not + 0.10. When I first came up with the algorithm, I just happened to think of using the loop. You can solve the problem however you want. After all, this is your assignment.

If you are using a loop and it is not producing the result you expected, then you have to try to debug it. Get a paper and pencil and follow each iteration of the loop to find out why the program is not producing the correct result. It sounds old school, but you'd be surprised at how effective this pencil / paper approach is. Also, you can include cerr/cout statements in the program for debugging purpose and then comment them out later.

The bottom line is, your job isn't just creating the code. You have to be able to debug your own code.
Last edited on
thanx for helping....

now help me to solve this problem.....

The acceleration of a sleigh sliding down a hill is a = g sin (a) , where a is the slope of the hill, and the gravity acceleration g = 9.8 ms-2. Write a C++ function that calculates the acceleration as a function of the slope a in degrees.
Write a C++ program that reads the slope and the length of the hill as inputs, and calculates how long time it takes to slide down the hill.
Last edited on
I suggest that you start by writing a program that reads the slope and length of the hill, and then prints out their values. Then work out the math for what the program should do. Compute a couple examples by hand (maybe for 45 and 60 degrees). Now add the code to do the math and print the results.

Remember that std::sin() takes its argument in radians, not degrees, so you will need to convert from degrees to radians.
@Azeem Sulehri

You've already asked several different questions on one thread. I think it's about time you let this die and create a new thread to ask a new question.

As for that problem, I suggest that you study <cmath> library and available functions.

Here is some information on <cmath>
http://www.cplusplus.com/reference/cmath/

Here is some information on sine function from <cmath>
http://www.cplusplus.com/reference/cmath/sin/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <cmath>

using namespace std;

double quadA (int a,int b,int c);
double quadB (int a,int b,int c);


int main ()
{
    double x1=0;
    double x2=0;
    int a;
    int b;
    int c;

    cout << "Please enter the value of a :: ";
    cin  >> a;
    cout << "Please enter the value of b :: ";
    cin  >> b;
    cout << "Please enter the value of c :: ";
    cin  >> c;

    x1 = quadA(a,b,c);
    x2 = quadB(a,b,c);

    cout << "The value of x = "<< x1<<endl;
    cout << "The value of x = "<< x2;
}
double quadA (int a,int b,int c)
{
    return ( - b + sqrt ( pow (b,2) - 4*a*c ) ) / (2*a);
}
double quadB (int a,int b,int c)
{
    return ( - b - sqrt ( pow (b,2) - 4*a*c )) / (2*a);
}


i did this but it give wrong answer help me to make it right
Did you work out the math by hand for an example or two? That's the place to start.
i did not work by hand i just make it but it give wrong result i don't know why
What is the right result? Why is that result right? You have to be clear about the math involved to solve the problem before you can express that math in code. Otherwise it's like saying you want to translate a sentence into French, but you don't have the original sentence.
Next time, please do us a favor and describe in words what the program is supposed to do instead of just throwing the code here. Your program is supposed to solve Quadratic equation after user provides the coefficients.

I've asked you to create a new thread since you've already asked multiple questions on this thread and all you did was just change the title. And the title, "Solve it..." provides NO INFORMATION whatsoever. Make the title more meaningful and you will get better help and you will help other people that might be struggling with a similar programming assignment.

Reference this article for posting on the forum.
http://www.cplusplus.com/forum/beginner/1/

Before we can help you, you need to help US first. "i did this but it give wrong answer help me to make it right" provides no information whatsoever. You could've told us what input you used and what the program output was. Basically, the only information I received from your question is just the code. Now, I have to figure out what the code is supposed to do and then I need to figure out what is wrong with the code from scratch and then magically solve the problem for you?

I will end my rant here.


I've used 3 tests on your program and it seems to be working just fine. I won't use the entire test case since it is your job to test your own code. Here is the input I used and the program output. All of these are correct.
1
2
3
4
5
6
7
Please enter the value of a :: 1
Please enter the value of b :: 4
Please enter the value of c :: 4
The value of x = -2
The value of x = -2
Process returned 0 (0x0)   execution time : 2.751 s
Press any key to continue.

1
2
3
4
5
6
7
Please enter the value of a :: 1
Please enter the value of b :: -4
Please enter the value of c :: -4
The value of x = 4.82843
The value of x = -0.828427
Process returned 0 (0x0)   execution time : 3.706 s
Press any key to continue.

1
2
3
4
5
6
7
Please enter the value of a :: 1
Please enter the value of b :: 16
Please enter the value of c :: 8
The value of x = -0.516685
The value of x = -15.4833
Process returned 0 (0x0)   execution time : 8.255 s
Press any key to continue.

Last edited on
Topic archived. No new replies allowed.
Pages: 12