Home › Forums › Computers, Games and Technology › A little Help with C
This topic contains 12 replies, has 7 voices, and was last updated by
sidecar 4 years ago.
- AuthorPosts
Gentleman,
I”m taking the time off before next semester to brush up on my C. Instead of rolling right into a 200 series class, I’d like to get down the fundamentals. Can anyone look at this and tell me if it could be coded more efficiently? Any and all feedback is appreciated//calculate miles per gallon
#include <stdio.h>Int main (void)
{
//initialize variables and values
float gas; //gas value
float miles; //miles valuefloat mpg; //number with decimal for mpg
printf( “%s”, “Enter gallons used (-1 to end): “); //prompt for gas used
scanf( “%f”, &gas); //user entry for gas used (floating-point)while (gas != -1){ //check for sentinal value
printf( “%s”, “Enter miles driven: “) ; //prompt for miles driven
scanf( “%f”, &miles); //user entry for miles driven (floating-point)
mpg = (float) miles / gallons;
printf( “The miles/ gallon for this tank was %f\n”, mpg);
printf( “%s”, “Enter gallons used (-1 to end): “); //prompt for gas used
scanf( “%f”, &gas); //user entry for gas used (floating-point)
} //endwhileif (gas ==-1)
printf( “%s”, “End of Program\n”);} //end function main
Now, syntax highlighting irregardless, it took me far too long to create this program (even using pseudo-code. The purpose was to get the miles and gallons used to calculate mpg, and have it loop until the sentinal value of -1 was entered.
Can someone tell me if there is a more efficient way to right this? Was I correcting in using the %f to call floating point integers? Any insight would be greatly appreciated.Funny, isn't it? How women thrive on a mans time, attention and resources, while simultaneously telling him he isn't enough...
I should also add that I am limited to the selection statements “If” and “if-then” and the repetition statement “while”. I have not covered “switch”, “do-while” or the “for” statements…
Funny, isn't it? How women thrive on a mans time, attention and resources, while simultaneously telling him he isn't enough...
Yeah. Problem is I travel too much though brother. Thought I could circumnavigate the problem by going the “E-read and download” route. .. but way too many discrepancies. Still.. pretty sound advice.. thanks!
Funny, isn't it? How women thrive on a mans time, attention and resources, while simultaneously telling him he isn't enough...
Not a professional C programmer by any means, but I have written some off and on both personally and professionally. I might do it like this:
printf( “%s”, “Enter gallons used (-1 to end): “); //prompt for gas used
scanf( “%f”, &gas); //user entry for gas used (floating-point)//not sure why you would need this initial set of inputs – why not just go right into the while loop?
while (gas != -1){ //check for sentinal value
printf( “%s”, “Enter gallons used (-1 to end): “); //prompt for gas used
scanf( “%f”, &gas); //user entry for gas used (floating-point)
printf( “%s”, “Enter miles driven: “) ; //prompt for miles driven
scanf( “%f”, &miles); //user entry for miles driven (floating-point)
mpg = (float) miles / gallons;
printf( “The miles/ gallon for this tank was %f\n”, mpg);} //endwhile
Wouldn’t that block of code right there accomplish the same thing and be somewhat smaller? I may very well be missing something so take it with a grain of salt, but C is definitely compact if nothing else, and I think the basic input and calculation statements are about as concise as they could be.
You might also ask for a single line of input with both values seperated by a delimiter like whitespace or a comma and then parse that. That would probably get you fewer lines of code if that was the object, but IMHO coding it that way is purely a matter of taste/preference/useability and not one of efficiency.
My $.02,
Biggs
"Data, I would be delighted to offer any advice I can on understanding women. When I have some, I'll let you know." --Captain Picard,
Computer engineering graduate here (will be a graduate in a few months).
The best way to optimize C code is to use gcc -o3. Problem solved. 😛
". . . elle, suivant l’usage des femmes et des chats qui ne viennent pas quand on les appelle et qui viennent quand on ne les appelle pas, s’arrêta devant moi et m’adressa la parole"—Prosper Mérimée
The best way to optimize C code is to use gcc -o3. Problem solved.>
That is just mean, also do you spend times trolling Gentoo Linux forums? 😉
Yeah. Problem is I travel too much though brother. Thought I could circumnavigate the problem by going the “E-read and download” route. .. but way too many discrepancies. Still.. pretty sound advice.. thanks!
Become familiar with Stackoverflow they have millions of members and answers for just about every problem you can imagine in the programming world.
In the last 6 months I’ve had my hands on applications in the C, C++, C#, and Python languages. You will find yourself learning and re-learning stuff all the time. That is why it is my suggestion to get good at the tools that help you find answers.
As far as your code itself it looks fine with Biggus’ suggestions. Though my suggestion is look into ways of getting your input data from sources other than your users. User’s can’t be trusted. Work on learning how to get data from text files, binary files, and databases. Also if you have you are going into 200 level C classes, you will want to focus more on address syntax and logic. That is one thing I always have to double check myself on even having done it for years now.
I find that C++ is a tad bit better than C honestly.that and it is more common.
A MGTOW is a man who is not a woman's bitch!
//not sure why you would need this initial set of inputs – why not just go right into the while loop?
I guess the idea “at least in my head” was that the While loop test for a condition to be true or false ( the sentinel value -1). If it had no condition to check… wouldn’t it just go straight into the loop with no information to check for that value? Guess I can comment it out and see what happens. Thanks for the heads up though. Much appreciated
Become familiar with Stackoverflow they have millions of members and answers for just about every problem you can imagine in the programming world.
Yes bro.. I’ve looked at stack overflow. . and it’s a little annoying because I depend heavily on comments in code to work out “at least in my head” wtf is going on. And most of the problems I’ve seen there are simply void of it. It both annoying and frustrating. Guess I’ll have to check again.
Also if you have you are going into 200 level C classes, you will want to focus more on address syntax and logic.
Sigh…just… not very reassuring. Lmao. But I’ll keep it in mind. Thanks for the heads up. FML.
Funny, isn't it? How women thrive on a mans time, attention and resources, while simultaneously telling him he isn't enough...
I should also add that I am limited to the selection statements “If” and “if-then” and the repetition statement “while”. I have not covered “switch”, “do-while” or the “for” statements…
What about break or exit?
#include <stdio.h>
int main(void) {
float miles, gas = -1;
while (1) {
printf( “Enter gallons used (-1 to end): “);
scanf( “%f”, &gas);
if (gas == -1) {
printf( “End of Program\n”);
exit(EXIT_SUCCESS); //a break is fine too
} //if
printf( “Enter miles driven: “) ;
scanf( “%f”, &miles);
printf( “The miles/ gallon for this tank was %f\n”, (miles / gas));
} //while
exit(EXIT_FAILURE); // shouldn’t reach this point with exit above.
// change to EXIT_SUCCESS if break was used instead.
} //mainYou probably also want to check the return values of each scanf() to make sure the required input was received. Right now someone could enter an invalid response and scanf would return with 0 matches, but your code would continue on obliviously and use whatever random values your variables had at declaration. There’s also a divide by zero problem to check for. I was too lazy to deal with these issues in my example above, but they’re pretty simple to handle.
I sincerely appreciate all the feedback and insight gentleman.. as an aside, can someone explain how in the world just about EVERYOBODY on this site knows how to program? Was this some requirement during the sites inception? A Right to Entry maybe, to prove you could utilize logic? Or maybe KM used programming in place of a captcha….
I mean.. dont get me wrong I appreciate the help.. I do.. its just… its weird man… Matrix weiRd… like being surrounded by a bunch of agents..
Funny, isn't it? How women thrive on a mans time, attention and resources, while simultaneously telling him he isn't enough...
I sincerely appreciate all the feedback and insight gentleman.. as an aside, can someone explain how in the world just about EVERYOBODY on this site knows how to program? Was this some requirement during the sites inception? A Right to Entry maybe, to prove you could utilize logic? Or maybe KM used programming in place of a captcha….
I mean.. dont get me wrong I appreciate the help.. I do.. its just… its weird man… Matrix weiRd… like being surrounded by a bunch of agents..
I’m far from an expert. but I took courses on Visual Basic and C++ in college, as well as scripting in Linux/UNIX.
//not sure why you would need this initial set of inputs – why not just go right into the while loop?
I think I understand now. I was given the impression that there are only really 3 types of statements needed to write a program in C. That is a sequential statement, a decision statements (if, if-else, switch) and repetition (while, do-while, and for).
I also understand that when writing pseudocode, the process includes:1) Declaring Variables
2) initialize variables that do not utilize non-destructive write-in values (such as counters)
3) processing phase
4) termination phaseI guess I misunderstood and assumed that repetition statements could only be used to loop statements that had already been created. I would not have considered using a repetition statement (while statement) to actually start an algorithm. Thanks fellas, that is a TREMENDOUS help. I know other sources (like stackoverflow) may help, but the question seemed so rudimentary I opted to ask here. Thanks for helping me along!
What about break or exit?
Not quite there yet sir. I’m only on Ch. 3 of 24.. I took and passed the course last semester, but don’t feel too comfortable with what I learned. I’m going to try to get through the whole book this semester however. I’m just a little nervous. I want to make sure I have the basics down before venturing further. Thanks for the input though SideCar.
I find that C++ is a tad bit better than C honestly.that and it is more common.
That’s what I hear. However, we know C++ is in addition to regular C. I ultimately want to get into Java and dev android apps, but that seems like a long way out… oh well, best way to eat an elephant…. one fork-full at a time.
Funny, isn't it? How women thrive on a mans time, attention and resources, while simultaneously telling him he isn't enough...
can someone explain how in the world just about EVERYOBODY on this site knows how to program?
I can think of two reasons:
1. The site is men only.
2. Only men who can code would comment in this thread.Not quite there yet sir. I’m only on Ch. 3 of 24.
Surprising. The break and continue statements are fundamental to loop control, and loop control is the heart of all programming beyond “Hello World.”
Put succinctly, break means: “Get the f~~~ outta the loop right now.” And continue means: “Get your ass back to the beginning of the loop right now.”
And of course exit() means GTFO period.
Thanks for the input though SideCar.
It was nothing.
But I’m serious about getting into the habit of always checking function return values for errors and always checking for pesky things like divide by zero errors. Also you are potentially opening yourself up to stack overflows if you rely on the scanf() family of functions too much without knowing how they operate under the hood. Use that family of functions with caution – not all scanf()s are equal.
Atton wrote:
I find that C++ is a tad bit better than C honestly.that and it is more common.That’s what I hear.
It depends on the field of programming. Some fields use more C++, but some are almost exclusively C. Generally the closer you are to the hardware the more C is used.
- AuthorPosts
You must be logged in to reply to this topic.

921526
921524
919244
916783
915526
915524
915354
915129
914037
909862
908811
908810
908500
908465
908464
908300
907963
907895
907477
902002
901301
901106
901105
901104
901024
901017
900393
900392
900391
900390
899038
898980
896844
896798
896797
895983
895850
895848
893740
893036
891671
891670
891336
891017
890865
889894
889741
889058
888157
887960
887768
886321
886306
885519
884948
883951
881340
881339
880491
878671
878351
877678
