By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,798 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,798 IT Pros & Developers. It's quick & easy.

New c++ learner

P: 1
My tutor has given me an assignment but i'm not good with the syntax can someboby kindly solve this problems for me.

1. Write a program to to test whether given three integer values, the vaules represents the sides of a valid triangle.

2. Write a program to accept money in shillings and determine the change to be given in denominations of 100, 50,20,10, and 5 shilling denominations

This is my attempt to 1

// Program to test whether three integer values represent the sides of a valid triangle

#include <iostream>
using namespace std;

int main ()

{
int a, b, c;

cout<<"Enter the three vaules";


c>>a;
c>>b;
c>>c;

ifa=0||b=0||c0;

cout<<"It is not a valid triangle, enter the three values again";
c>>a;
c>>b:
c>>c;

if(a+b)>(c)&&(a+c)>(b)$$(b+c)>(a)


cout<<"This is a valid triangle";

else
cout<<"Not a triangle";

return 0
}
Oct 4 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
My tutor has given me an assignment but i'm not good with the syntax can someboby kindly solve this problems for me.

1. Write a program to to test whether given three integer values, the vaules represents the sides of a valid triangle.

2. Write a program to accept money in shillings and determine the change to be given in denominations of 100, 50,20,10, and 5 shilling denominations

This is my attempt to 1

// Program to test whether three integer values represent the sides of a valid triangle

#include <iostream>
using namespace std;

int main ()

{
int a, b, c;

cout<<"Enter the three vaules";


c>>a;
c>>b;
c>>c;

ifa=0||b=0||c0;

cout<<"It is not a valid triangle, enter the three values again";
c>>a;
c>>b:
c>>c;

if(a+b)>(c)&&(a+c)>(b)$$(b+c)>(a)


cout<<"This is a valid triangle";

else
cout<<"Not a triangle";

return 0
}
Reading a book would be better because knowing syntax rules can help you express any other statements by merely applying the rules. When you do get the syntax right, you could always post (in code tags and in this same thread) if you need additional help.
Oct 4 '06 #2

P: 27
I agree with the previous post, reading a book to learn the syntax would probably help you more in the long run. In the short, however, here's an example of how you could write that program to do what you are looking for:

Expand|Select|Wrap|Line Numbers
  1. // Program to test whether three integer values represent the sides of a valid triangle
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. int a, b, c;
  9.  
  10. do
  11. {
  12.     cout << "Enter the three vaules" << endl;
  13.  
  14.     cin >> a;
  15.     cin >> b;
  16.     cin >> c;
  17.  
  18.     if (a <= 0 || b <= 0 || c <= 0)
  19.     {
  20.         cout << "This is not a valid triangle, please enter"
  21.             << " positive numbers." << endl << endl;
  22.     }
  23. }
  24. while (a <= 0 || b <= 0 || c <= 0);
  25.  
  26. if (
  27.     a + b > c &&
  28.     a + c > b &&
  29.     b + c > a)
  30. {
  31.     cout << "This is a valid triangle." << endl;
  32. }
  33. else
  34. {
  35.     cout << "This is not a valid triangle." << endl;
  36. }
  37.  
  38. return 0;
  39. }
  40.  
Oct 4 '06 #3

100+
P: 293
D_C
Not only should A, B, and C be positive, they should also obey the triangle inequality. For any given side, without loss of generality, A, |B-C| < A < B+C. If A is equal to B+C, then angle BC is 180, so angle AB and AC are 0. Similarly for when |B-C| is equivalent to A.
Oct 4 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.