C++ Code

compiler
Last edited on
You can start by creating the main function. And cout statement with the prompt.

Edit : http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
i dont know programming, i am new. Can you write a programme and give it to me it will be helpfull
Have you even tried...? Use google, Use Youtube, research, there are endless amount of contents showing you how to get.

https://www.youtube.com/watch?v=SWZfFNyUsxc&index=2&list=PLAE85DE8440AA6B83&ab_channel=thenewboston
Break it down into simple, testable steps.

* As Tarik said, start with main. Compile your program and run it.
* Make a cout statement that asks for the sides of a triangle. Compile your program and run it - make sure you see your cout statements.
* Write code to get input from the user. Compile your program and run it - make sure you can input values. As an additional test, you may want to cout those values afterwards just to make sure you got them correctly.
* Determine the formula that verifies a valid triangle. Implement that formula. Compile your program and run it twice - try with different outputs that make it a valid and invalid triangle.
* Find out the formula to compute the area of a triangle. Implement that formula. Compile and run your program with several different inputs to verify your area function works.

If this approach helps, you can read more about it on my blog: http://www.mycpptutor.com/blog/2015/03/31/how-to-get-unstuck-on-your-programming-assignment/
Please advice if below code is correct? How to compute area of triangle?

#include <iostream.h>

main()
{
//variables
float aside, bside, cside;
//enter side a
cout<<"enter the length of side a "<<endl;
cin>>aside;
//enter side b
cout<<"enter the length of side b "<<endl;
cin>>bside;
//enter side c
cout<<"enter the length of side c "<<endl;
cin>>cside;


if(a==b && b==c) // all sides equal
cout << "Equilateral triangle\n";
else if(a==b || a==c || b==c) // at least 2 sides equal
cout << "Isosceles triangle\n";
else // no sides equal
cout << "Scalene triangle\n";
}
Last edited on
Well not quite. Firstly, please always put all of your code between code tags <>. Its under the format section.

First half is good, second half makes no sense.

You input the info about the sides in aside, bside and cside.
And then in your ifstatements, you ask about a,b,c. What is a,b,c? There is no a,b,c. They are called aside,bside,cside remember?

1
2
3
4
5
6
if (aside == bside && bside == cside) // all sides equal
		cout << "Equilateral triangle\n";
	else if (aside == bside || aside == cside || bside == cside) // at least 2 sides equal
		cout << "Isosceles triangle\n";
	else // no sides equal
		cout << "Scalene triangle\n";
Now if i want to calculate the area of triangle (How do i integrate both the programs?)



#include<iostream>
#include<math.h>
using namespace std;
int main()

{

float first,second,third;
float s,area;
cout<<"Enter size of each sides of triangle"<<endl;
cout<<"Enter size for First Side =";
cin>>first;
cout<<"Enter size for Second Side =";
cin>>second;
cout<<"Enter size for Third Side =";
cin>>third;
s = (first+second+third)/2;
area = sqrt(s*(s-first)*(s-second)*(s-third));
cout<<"Area of Triangle= "<<area<<endl;
return 0;

}
By putting this one right under the other one? Also, I told you to put your code between code tags, next time if you post and dont do that, I will simply ignore 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
30
31
32
33
34
35
36
37
int main(){

	//variables
	float aside, bside, cside;
	//enter side a
	cout << "enter the length of side a " << endl;
	cin >> aside;
	//enter side b
	cout << "enter the length of side b " << endl;
	cin >> bside;
	//enter side c
	cout << "enter the length of side c " << endl;
	cin >> cside;

	if (aside == bside && bside == cside) // all sides equal
		cout << "Equilateral triangle\n";
	else if (aside == bside || aside == cside || bside == cside) // at least 2 sides equal
		cout << "Isosceles triangle\n";
	else // no sides equal
		cout << "Scalene triangle\n";

	float first, second, third;
	float s, area;
	cout << endl << "Enter size of each sides of triangle" << endl;
	cout << "Enter size for First Side =";
	cin >> first;
	cout << "Enter size for Second Side =";
	cin >> second;
	cout << "Enter size for Third Side =";
	cin >> third;
	s = (first + second + third) / 2;
	area = sqrt(s*(s - first)*(s - second)*(s - third));
	cout << "Area of Triangle= " << area << endl;

	system("pause");
	return 0;
}
Sorry
Did you remove using namespace std; ?...
Yes. After using namespace std it was resovled.

Now i want to write a pseudo code for above code. How to do it ? Can you help me with it ?
Last edited on
Sorry, never written pusedo code so cant help you with that. Im sure if you google some tutorials or some examples you'll find good stuff.
Hi,

mabele wrote:
Now i want to write a pseudo code for above code. How to do it ?


Pseudo code, is normally written before one writes their program code. It is a most basic method of designing your project.

It's purpose is to help one write code, by putting down general ideas of what you want to do, in order. Then go back and flesh out those ideas with more detail until one is happy to convert that to code. It can help identify what needs to be in functions, and what can be put into loops.

It's a good idea to do this with comments in your file, leave the comments in after you write the code - they could serve as a form of documentation. Although one should aim to use meaningful variable & function names to result in self documenting code.

tscott8706's post could be seen as a description of what to do. Pseudo code might look like this:

1
2
3
input variable a
validate variable a
print variable a


You can write whatever you want, we know that we could use std::cin and std:: cout, but in our pseudo code we use input and print because it is simple and we know what it means.

Be careful when equating floats or doubles - they are not represented exactly and cannot represent every real number, so using the equality operator will almost never work. Instead check to see if the difference between the 2 variables is less than some arbitrary precision value like 0.001 say. However it will work if the user enters the same numbers. I am just pointing this out because it won't work in a majority of other situations.

The default type for floating point in C++ is double, prefer that over floats. It is very easy to go past the precision available to a float, they can only do 7 or 8 significant figures, whereas a double can do 15 or 16.

Another Golden Rule is to always initialise your variables to something, preferably on the same line as the declaration of the variable. Do 1 variable per line, with a comment if necessary. Comments can specify expected ranges of values.

Always validate the values that are input, what if the user put in a negative value? Did you check to see if the sum of 2 of the sides is larger than the other one - if not, it's not a triangle. Try to always be mindful of what can go wrong.

Hope all is well

I tried to run the above c++ program in Netbeans. Its shows Build Successful and Run Successful.

I also used cygwin.

But I am unable to run the program. Like it doesnt ask me for inputs of sides ..

How to do that. When i run the .exe file a command prompt opens and closes directly.....
In the code written above ? How are we validating if the triangle is valid ??
Topic archived. No new replies allowed.