C coding, adding two numbers and getting the average.
47 replies, posted
Yes i'm kinda new to this and we got a task where you have to code so you can add two numbers and get the average out of it.
I'm really new to this so any help would be apriciated.
[QUOTE=zotic;16957590]Yes i'm kinda new to this and we got a task where you have to code so you can add two numbers and get the average out of it.
I'm really new to this so any help would be apriciated.[/QUOTE]
(n1 + n2) / 2
Something like this?
[cpp]#include <stdio.h>
int main()
{
float a, b;
printf("i can has a number plox : ");
scanf("%f",&a);
printf("i can has another number plox : ");
scanf("%f",&b);
printf("average lololol : %f\n",(a+b)/2);
printf("teh end\n");
system("pause");
return 0;
}[/cpp]
-snip-
I thought you meant C# haha
it must be normal C not C++ or sharp.
[editline]11:12AM[/editline]
2 sec.
[editline]11:18AM[/editline]
still can't get it to work..
just need input a and b as number then they should be added and average number should comeout
Forgot to add system("pause"); when the program ends. If it's not there, the program ends immediately, so you don't really have time to see the result.
Also, read books or at least pay attention, you're new, but you're not an idiot (well at least I hope so). That's, like one of the simplest things you do when learning C. And if you didn't know how to calculate the average of two numbers then I feel sorry for you.
EDIT: Unless you mean it doesn't stop, he just told you how ^
What _killburn gave you works just fine.
[QUOTE=_Kilburn;16957801]Forgot to add system("pause"); when the program ends. If it's not there, the program ends immediately, so you don't really have time to see the result.
Also, read books or at least pay attention, you're new, but you're not an idiot (well at least I hope so). That's, like one of the simplest things you do when learning C. And if you didn't know how to calculate the average of two numbers then I feel sorry for you.[/QUOTE]
By new i mean i just started 10minutes ago.. Also i do know how to calculate the average..
[QUOTE=_Kilburn;16957801]system("pause");[/QUOTE]
:argh:
Anyway, in C:
[cpp]
/*The header for input and output functions*/
#include <stdio.h>
int main()
{
float a, b;
int succ;
/*ask for our first number until a valid number is input*/
do
{
printf("Input number: ");
succ = scanf("%f", &a);
}
while(succ != EOF);
/*ask for our second number until a valid number is input*/
do
{
printf("Input another number: ");
succ = scanf("%f", &b);
}
while(succ != EOF);
/*output result*/
printf("Average of %f and %f is %f\nPress a key to continue\n", a, b, (a+b)/2);
/*wait for a keypress*/
getchar();
return 0;
}[/cpp]
[QUOTE=jA_cOp;16957883]:argh:[/QUOTE]
Who gives a shit, it works on Windows, it works on Linux, and you're never going to use that in a real program. That's what all teachers and books teach us, fucking use system("pause") and deal with it, it works, and that's enough.
Also, nice code you've got there, the only problem with it is that I doubt a newbie programmer can understand it.
Oi got it working now. Thanks for the help
[QUOTE=_Kilburn;16957898]it works on Linux[/QUOTE]
Oh really now? That depends on your distribution. And what about BSD users? Why rely on all these things when the better alternative is even shorter to write?
[QUOTE=_Kilburn;16957898]fucking use system("pause")[/QUOTE]
So, now it's the BEST option?
[QUOTE=_Kilburn;16957898]and you're never going to use that in a real program.[/QUOTE]
Oh really? What if I grew up with books, teachers and peers that all advocated system("pause"), never telling you the downsides of it. Would I still never use it in a "real program"?
[QUOTE=_Kilburn;16957898]Also, nice code you've got there, the only problem with it is that I doubt a newbie programmer can understand it.[/QUOTE]
With the comments, he can still read what it does, and perhaps pick up some concepts like do-while. Your code is basically a round-up of several bad C habits.
[QUOTE=_Kilburn;16957801]Forgot to add system("pause");[/QUOTE]
Don't do that, it's silly. Apart from being bad in every sense, it's not portable.
Debian:
You have searched for packages that names contain pause in suite(s) stable, all sections, and all architectures.
Sorry, your search gave no results
Ubuntu:
You have searched for packages that names contain pause in all suites, all sections, and all architectures.
Sorry, your search gave no results
I was going to check gentoo as well but oh my god who the hell designed their web page.
[url]http://packages.gentoo.org/[/url]
[QUOTE=_Kilburn;16957898]Who gives a shit, it works on Windows, it works on Linux, and you're never going to use that in a real program.
[/QUOTE]
Umm...What? Yes it works in Windows, it does [b]NOT[/b] work in Linux, Linux uses another command, can't remember what. Never going to use the averaging stuff in a real program? You're an idiot..What about a program requiring maths, or an accounting program..
[quote=_Kilburn]That's what all teachers and books teach us, fucking use system("pause") and deal with it, it works, and that's enough.[/quote]
Ever stop to think the books are outdated? I won't use system commands because of the fact it's not always cross-OS. I won't deal with it. If I see a newbie use that in here i'll correct them. I'm pretty sure the majority of us would. It might work but it's not a good way to deal with it, so no, it isn't enough.
[quote=_Kilburn]Also, nice code you've got there, the only problem with it is that I doubt a newbie programmer can understand it.[/quote]
If a newbie can't understand that then they shouldn't learn to program.
If they don't know a function, maybe they should use [highlight][b]GOOGLE[/b][/highlight] and search for the function.
Now i'll finish here because if I continue I'll be banned for flaming.
No, maybe you should continue :colbert:
So you don't know what an average is exactly yet you are going to a school that does programming or want to learn programming yourself?
Wow.
[QUOTE=Sporbie;16959601]So you don't know what an average is exactly yet you are going to a school that does programming or want to learn programming yourself?
Wow.[/QUOTE]
This is the programming forum, I doubt he came here for help with the mathematics side of the program.
But we are talking about an arithmetic average, that's 5-6th grade maths.
[QUOTE=Sporbie;16959946]But we are talking about an arithmetic average, that's 5-6th grade maths.[/QUOTE]
And where did he say that he didn't understand it?
[QUOTE=zotic;16957852]By new i mean i just started 10minutes ago.. Also i do know how to calculate the average..[/QUOTE]
I misread this post, looked to me like it said "Also i dont know how to calculate the average". Rate me dumb.
Average:
Sum of observations divided by number of observations.
Eg: a+b/2 or a+b+c+d+e+f/6
Alternatives to system("pause");
std::cin.get(); iostream
std::cin.ignore(); iostream
getchar(); dun know the header for this one, stdio.h i guess?
getch(); conio.h
[QUOTE=Corewarp3;16960030]Average:
Sum of observations divided by number of observations.
Eg: a+b/2 or a+b+c+d+e+f/6
Alternatives to system("pause");
std::cin.get(); iostream
std::cin.ignore(); iostream
getchar(); dun know the header for this one, stdio.h i guess?
getch(); conio.h[/QUOTE]
We're talking about C, though. There's no iostream header. And yep, getchar is declared in stdio.h.
Oh fuck cakes i totally forgot xD Danke for the correctionez
[QUOTE=Corewarp3;16960030]
Sum of observations divided by number of observations.
Eg: a+b/2 or a+b+c+d+e+f/6
[/QUOTE]
:bang:
[QUOTE=_Kilburn;16957898]Who gives a shit, it works on Windows, it works on Linux[/QUOTE]
It doesn't work on Linux. It silently fails. Just because you don't get a big error doesn't mean it works.
[QUOTE=Corewarp3;16960734]Excuse you?[/QUOTE]
Order of operations.
[QUOTE=DeltaOps101;16960746]Order of operations.[/QUOTE]
This.
Guys i got it to work, now our whole class is going to C# >_>'
[QUOTE=PvtCupcakes;16960708]It doesn't work on Linux. It silently fails. Just because you don't get a big error doesn't mean it works.[/QUOTE]
It can work on any system that has the system function implemented. The big problem is indeed that it silently fails on systems where the system function can't run a program or internal command named "pause". It's also relatively easy to exploit in a buffer overflow exploit. The system function usually also uses a lot more resources than alternatives, but that's not really a problem with the case of "pause".
[QUOTE=jA_cOp;16960971]It can work on any system that has the system function implemented. The big problem is indeed that it silently fails on systems where the system function can't run a program or internal command named "pause". It's also relatively easy to exploit in a buffer overflow exploit. The system function usually also uses a lot more resources than alternatives, but that's not really a problem with the case of "pause".[/QUOTE]
Yeah I know system() works on Linux, but pause isn't a valid Linux/Unix/bash command.
The compiler can't tell you that because all it sees is a function (system()) that takes a string argument, and "pause" is a perfectly valid string so it compiles.
Sorry, you need to Log In to post a reply to this thread.