Jump to content

Need help with C language

oc0110

I am trying to solve a few problems and got stuck with these three.

 

#1

Area of a triangle is given by the formula

A = sqrt[s(S-a)(S-b )(S-c)]

where a, b and c are sides of the triangle 2S = a + b + c. Write a program to compute the area of the triangle given the values of a, b and c.

 

#2

Write a program to determine and print the sum of the following harmonic series for a given value of n:

1 + 1/2 + 1/3 + ... + 1/n

The value of n should be given interactively through the terminal.

 

#3

Write a program that prints the even numbers from 1 to 100.

 

 

I'm really new to C language :(

 

Thanks to those who are helping :) I really appreciate it

Link to comment
Share on other sites

Link to post
Share on other sites

Coming soon

 

Coming soon

 

#include <stdio.h>#include <math.h>int main(void){   int n = 1;   while(n<=100){      if(n%2==0){         printf("%d\n", n);      }      n++;   }   return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Link to comment
Share on other sites

Link to post
Share on other sites

I can't get my compilers fixed in both Windows and Linux mint...

 

I'm sure #3 will run as expected...

Link to comment
Share on other sites

Link to post
Share on other sites

can you post your code so far?

a re-read at the basics chapter of your C manual/videotutorial/book should make the solutions pretty clear

Link to comment
Share on other sites

Link to post
Share on other sites

Thts the thing. I'm not sure about the question.. Sry I'm really new to programming.

Link to comment
Share on other sites

Link to post
Share on other sites

Thts the thing. I'm not sure about the question.. Sry I'm really new to programming.

 

I'll give you some clues to #1...

 

You need math.h to get the sqrt() function to work...

 

You also need the string.h and stdlib.h to turn the user inputs to the proper data type...

 

I'll give you the block of code for the sqrt() part of the thing...

A = sqrt(S*(S-a)*(S-b)*(S-c));//A's data type is float and not int

You need to figure some things out by yourself if you ever want to learn how to code...

Link to comment
Share on other sites

Link to post
Share on other sites

Ah yes thank you so much just the type of help i needed cause i was stuck right there. :)

Link to comment
Share on other sites

Link to post
Share on other sites

So far I have this written down, but I don't know where I'm wrong for number 1 

#include <stdio.h>#include <math.h>main(){	double area, a, b, c, S;	printf("A = %d", area);	int 2.0*S = a + b + c;	area = sqrt(S*(S-a)*(S-b)*(S-c));		getchar();}
Link to comment
Share on other sites

Link to post
Share on other sites

now i edited to: 

#include <stdio.h>#include <math.h>main(){	double area, a, b, c, S;	printf("A = %d", area);	int S = (a + b + c)/(2.0);	area = sqrt(S*(S-a)*(S-b)*(S-c));		getchar();}

but I keep getting an error message :(

Link to comment
Share on other sites

Link to post
Share on other sites

but I keep getting an error message :(

read the message, understand it (and/or google it), and fix the program

Link to comment
Share on other sites

Link to post
Share on other sites

now i edited to: 

#include <stdio.h>#include <math.h>main(){	double area, a, b, c, S;	printf("A = %d", area);	int S = (a + b + c)/(2.0);	area = sqrt(S*(S-a)*(S-b)*(S-c));		getchar();}

but I keep getting an error message :(

try moving print to the last line. Also try double in front of S as that's what you said it is in the first line :)

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to comment
Share on other sites

Link to post
Share on other sites

done with #1 yay

thanks guys

what was the issue?

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to comment
Share on other sites

Link to post
Share on other sites

since I already declared S was double on the first line it doesn't make sense to make it an integer later on so, I took out S from the first declaring line and replaced int with float.

Link to comment
Share on other sites

Link to post
Share on other sites

Look up the basics of C. Those are very simple questions.

 

I'd say look up the equation online and then it wouldn't be too hard to build it in C.

 

http://www.cprogramming.com/tutorial/ - This should teach you the C basics for the first, second and thirt question

 

http://www.cplusplus.com/reference/cmath/sqrt/ - This should help you with your 2nd question. I know that this is a C++ reference but it's the same in C. C++ is basically C with the ability to create Objects.

Link to comment
Share on other sites

Link to post
Share on other sites

now i edited to: 

#include <stdio.h>#include <math.h>main(){	double area, a, b, c, S;	printf("A = %d", area);	int S = (a + b + c)/(2.0);	area = sqrt(S*(S-a)*(S-b)*(S-c));		getchar();}

but I keep getting an error message :(

 

Syntax error with declaring the main function...

int main(void){    //your code here}

Use ^this^...

 

Try putting printf after calculating the area...

 

Also use %lf as placeholder for the 'double' data type...

 

Test your code by assigning plausible values to a, b, and c...

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×