C Programming Tutorial 44 - Coding Challenge

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

I just wanted to comment as I have followed your videos from the very beginning and I just completed this challenge. I did it completely solo and my code is a little different to yours but it still works the same way! I just wanted to share with you because I'm proud of writing my first ever program by figuring it out myself!


#include <stdio.h>
#include <math.h>

/*Create a program that takes two doubles as inputs, calculates
the hypotenuse of a right angle triangle and outputs it */

int main(void)
{
printf("This program will calculate the hypotenuse of a right-angled triangle\n");

double a;
printf("Enter a number for the a side of the triangle: ");
scanf("%lf", &a);
a = pow(a, 2);

double b;
printf("Enter a number for the b side of the triangle: ");
scanf("%lf", &b);
b = pow(b, 2);

double c = a + b;

double d = sqrt(c);
printf("The hypotenuse of the right angle triangle is %f", d);

return 0;
}


Thanks Caleb :) looking forward to learning in the future videos of the series!

LimeWitness
Автор

#include <stdio.h>
#include <math.h>
#include <cs50.h>

// Purpose: To take two doubles as inputs and calculate the hypotenuse of a right triangle.

int main ()
{
double a;
double b;
double c;

printf("This app will calculate the hypotenuse of a right triangle.\n\n");

//Prompts user for input of value a and b.
a = get_double("What is the value of side a?\n");
b = get_double("What is the value of side b?\n");

//Brings both variables a and b to the power of 2.
double a2 = pow(a, 2);
double b2 = pow(b, 2);

//Calculates the sum and square root of both variables a and b, which is equal to the size of a hypotenuse of a right triangle.
c = sqrt(a2 + b2);

printf("The value of the hypotenuse is ~ %.2lf\n", c);

return 0;
}

chrisradzanowski
Автор

I ran into a problem when compiling with gcc
" Undefined reference to 'sqrt' "
To solve this just add -lm at the end like this
gcc filename.c -lm

FireJosiah
Автор

for compiling in vim and gcc, use - gcc <nameoffile.c> -o nameoffile -lm then use ./nameoffile to run the program

Aditya-nelk
Автор

Nice work. Clear and concise. Now for an additional challenge. Since you know it's a right triangle and you know the lengths off all 4 sides, calculate the 2 remaining angles - hint use the trigonometry functions Sin and Cos.

stevenwillis
Автор

Posting this here before I watch the video to see if his code is similar lmao:


#include <stdio.h>
#include <math.h>

//Program finds the hypotenuse of a triangle
int main()
{
double leg1;
double leg2;

printf("What is the value of leg 1? ");
scanf("%lf", &leg1);
printf("What is the value of leg 2? ");
scanf("%lf", &leg2);

double hypotenuse = sqrt((double)leg1 * leg1 + leg2 * leg2);

printf("The hypotenuse of the given triangle is %f", hypotenuse);

return 0;
}

rianlewis
Автор

I made it thaaank yooouu!!!!, I want to update people who are trying it these days, you should put %lf instead of %f in the last function. Otherwise you will encounter with error as just happened to me minutes ago.

alperenceliktas
Автор

Pythagoras? More like Pythagor-outstanding! Think you for making all of these wonderful videos.

PunmasterSTP
Автор

The sqrt() function didn't work until I added "-lm" to the command. I use Elementary OS on Linux.

oglothenerd
Автор

I did it this way
#include <stdio.h>
#include <math.h>

int main()

{
double a, b, c;
printf("Please enter the value of a: ");
scanf("%lf", &a);

printf("Please enter the value of b: ");
scanf("%lf", &b);

c = (a * a) + (b * b);

double hypotenuse = sqrt(c);
printf("The length of hypotenuse is: %lf\n", hypotenuse);

return 0;
}
But the only way to compile it is gcc pythagoras.c -lm Something about linking something.Not sure yet.My PC runs on debian 9.Anyway the code works and it does what it has to!

andyoclover
Автор

Feeling so good after being able to make a program all by myself

yllibeadini
Автор

i still confuse about double vs float and why scanf using %lf but printf using %f?

ibrahimcious
Автор

you sound a bit sad compared to your other videos, is everything ok??

ritamuniz
Автор

I knew exactly what needed done and I did it and ran into a major road block. Apparently the math library isn't automatically linked and you need to add the -lm option to gcc to link to it.

markjones
Автор

Curious.... how did he get the program to compile without the -lm flags at the end? If I try that in either vim or VSCode the program doesn't compile. Is it a setting in his tasks.json?

ronpipes
Автор

yes i did it without watching the video thanks to you!

samarthvs
Автор

When did u teach this functions to us?

ridhofachrurrozy
Автор

Uggg had to figure out how to use M Visio Studio, set up gcc again, and vim. Still having trouble. Do you have other videos on how to set up gcc on Microsoft Visual Studio? Typed exactly what you typed and this is the first one that does not compile.

DjehutimasAsarRa
Автор

When he figures out there might be a run button.

_score
Автор

Pizza cake!! Moving on to the next video. Lol

paulcrandall