Posted September 9, 2017 So I've never had any coding experience before, and have always leaned towards the hardware side of things. My degree is Tech Management which requires a Basic C course. My prof seems like a cool guy, buy he goes over his power points for a bit and hops right into coding. Now it's only the end of week two, so we haven't learned much, and what he has done everyone just copies him off of the projector. Anyway he straps with a real bear of a first homework assignment. We're using CodeBlocks for all of this ... Any help on this would be much appreciated!!! Here's where I am at and lost /* comments go here */ #include <stdio.h> int main() { // declarations A directly connected telephone network is one in which all telephones in the network are directly connected and do not require a central switching station ti establish calls between two telephones. For example, financial institutions on Wall Street use such a network to maintain direct and continuously open phone lines between firms. The number of direct lines needed to maintain a directly connected network for n telephones, is given by the formula lines= n(n-1)/2 For example, directly connecting four telephones requires six individual lines. Adding a fifth telephone to this network would require an additional four lines for a total of ten lines. Using the given formula, write a C program that determines the number of direct lines requried for the 100 telephones, and the additional lines required if 10 new telephones were added to the network. Spoiler LTT's Fastest single core CineBench 11.5/15 score on air with i7-4790K on air Main Rig CPU: i7-4770K @ 4.3GHz 1.18v, Cooler: Noctua NH-U14S, Motherboard: Asus Sabertooth Mark 2, RAM: 16 GB G.Skill Sniper Series @ 1866MHz, GPU: EVGA 980Ti Classified @ 1507/1977MHz , Storage: 500GB 850 EVO, WD Cavier Black/Blue 1TB+1TB, Power Supply: Corsair HX 750W, Case: Fractal Design r4 Black Pearl w/ Window, OS: Windows 10 Home 64bit Plex Server WIP CPU: i5-3570K, Cooler: Stock, Motherboard: ASrock, Ram: 16GB, GPU: Intel igpu, Storage: 120GB Kingston SSD, 6TB WD Red, Powersupply: Corsair TX 750W, Case: Corsair Carbide Spec-01 OS: Windows 10 Lenovo Legion Laptop CPU: i7-7700HQ, RAM: 8GB, GPU: 1050Ti 4GB, Storage: 500GB Crucial MX500, OS: Windows 10 Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 9, 2017 this looks like the easiest homework ever. Do some maths 100 (100 - 1)/2 and print the result. Do the maths again 110 (110 - 1)/2 - previous result, and print the result ☼ ψ ︿_____︿_ψ_ Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 9, 2017 Author 12 minutes ago, SCHISCHKA said: this looks like the easiest homework ever. Do some maths 100 (100 - 1)/2 and print the result. Do the maths again 110 (110 - 1)/2 - previous result, and print the result I understand the easy Math. I just don't know how to type it into the code for it to printf? Spoiler LTT's Fastest single core CineBench 11.5/15 score on air with i7-4790K on air Main Rig CPU: i7-4770K @ 4.3GHz 1.18v, Cooler: Noctua NH-U14S, Motherboard: Asus Sabertooth Mark 2, RAM: 16 GB G.Skill Sniper Series @ 1866MHz, GPU: EVGA 980Ti Classified @ 1507/1977MHz , Storage: 500GB 850 EVO, WD Cavier Black/Blue 1TB+1TB, Power Supply: Corsair HX 750W, Case: Fractal Design r4 Black Pearl w/ Window, OS: Windows 10 Home 64bit Plex Server WIP CPU: i5-3570K, Cooler: Stock, Motherboard: ASrock, Ram: 16GB, GPU: Intel igpu, Storage: 120GB Kingston SSD, 6TB WD Red, Powersupply: Corsair TX 750W, Case: Corsair Carbide Spec-01 OS: Windows 10 Lenovo Legion Laptop CPU: i7-7700HQ, RAM: 8GB, GPU: 1050Ti 4GB, Storage: 500GB Crucial MX500, OS: Windows 10 Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 9, 2017 26 minutes ago, Br3tt96 said: I understand the easy Math. I just don't know how to type it into the code for it to printf? I really dont like the way C does printing to standard output. You put a %d in the string where you want your variable to go, and then you specify the variable as a second parameter to printf(). there is more than %d but well just use it for now. You can look up a reference like wikipedia for a full list int lines = 100(100-1)/2; printf("the number of line required = %d\n", lines); ☼ ψ ︿_____︿_ψ_ Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 10, 2017 1 hour ago, Br3tt96 said: I understand the easy Math. I just don't know how to type it into the code for it to printf? printf uses the arguments in the following format: printf(" ", arg1, arg2, ...); The quotes is where you put what you want to show on the console. arg1, arg2, ... are the arguments you pass into it, or the data you want to show. The ... means that printf can take an arbitrary amount of arguments. printf uses what's called a format specifier. Meaning it's looking for a combination of characters to replace them with what you want. All format specifiers start with a percent sign (%) and use a single letter to specify what kind of data it's supposed to print out. For example: %d - prints a signed integer. %u - prints an unsigned integer %f - prints a floating point value %c - prints a single character %s - prints a character string (make sure the string you're passing is null terminated) So putting this altogether: int main(){ int d = 100; float f = 123.456f; char *s = "Hello world!\0"; /* Note the \0, or null terminator. This is important. */ printf("int - %d, float - %f, string - %s\n", d, f, s); /* This will print "int - 100, float - 123.456, string - Hello world!" */ } I have a blog! And a list of guides I've posted Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 10, 2017 3 hours ago, M.Yurizaki said: <snip> Defining a string literal that way is considered bad practice, it should be: const char *s = "Hello world!"; //(1) char s[] = "Hello world!"; //(2) Note that the explicit 0 terminator is not necessary. It is implicitly included by the compiler. "Hello World!" is a string literal, part of read only storage. By assigning it to a non-const pointer, as in your example, you can (accidentally) modify the string which leads to undefined behavior due to writing to RO memory. You'll probably get a segfault. Some compilers also warn about deprecated assigning of string literals to non-const pointers because of this. Either make it a const pointer (1) which will catch any (accidental) attempts to modify the string at compile time or make it a array (2) which allows for modifying the string because arrays are normal variables in RW memory. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 > I've never had any coding experience before, and ... requires a Basic C course. Three problems: > never had any coding experience > Basic > C course ... impossible! Well, not impossible, but VERY impractical. You should learn a more beginner-friendly language, like Python or Javascript. > My prof seems like a cool guy ... but is he a good teacher? > ... he goes over his power points for a bit and hops right into coding. Now it's only the end of week two, so we haven't learned much, and what he has done everyone just copies him off of the projector. No signs yet... > Anyway he straps with a real bear of a first homework assignment. We're using CodeBlocks for all of this ... Any help on this would be much appreciated!!! Ahh! There we go! He hasn't spotted that you're having difficulties, yet. If you put yourself in his shoes for a moment, when (as him) you're walking around the class-room three or four times a lesson to gauge what each student is up to, it shouldn't be too hard to spot the struggling students, right? Having said that, it also shouldn't be too hard to approach him with a question. Interactivity is key, and it seems like you've not had much in your lessons. Perhaps you could pick up a copy of K&R2E, before it's too late. Even if you don't get to pass this assignment, it's probably the only way you'll pass the others. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 On 9/9/2017 at 7:54 PM, SCHISCHKA said: I really dont like the way C does printing to standard output. It's understandable if you know how varargs work in c/c++. You have to know the data types (which are lost at compile time) in order to do proper formatting and retrieval from the stack. <stdarg.h> More modern langues are interpreted or have a runtime environment where that information isn't lost.@Br3tt96 Don't be afraid to ask questions, and a lot of them; as many as are necessary...and then maybe a few more for good measure. It's always better to err on the side of too many questions now so you don't end up five topics later having missed them all because you didn't understand the foundation. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 55 minutes ago, Sebivor said: You should learn a more beginner-friendly language, like Python or Javascript. Please don't. Write in C. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 21 minutes ago, Dat Guy said: Please don't. Don't what? Don't discourage someone from taking the same path I took, and realising how much of a mistake it was five years later? Python and Javascript have no undefined behaviour. That is the significant issue here. Anyone who argues against that point doesn't know C or is trying to engage in psychological warfare. Have you ever had a problem which seemed to go away for some strange reason, like, you comment out an unused variable and the code starts crashing? Undefined behaviour... Tell me, how long did it take you to learn about the cause? Two hours, if you're lucky? Two weeks, if not? Perhaps you never even see your undefined behaviours! It could go unseen for years, like the Heartbleed bug, which constitutes a security vulnerability (hello, undefined behaviour!)... Or it could instill misinformation into the student; for example, students commonly analyse `printf("%d", x++, x, ++x);` to try to determine the order of operation, and expect that analysis to be true well into the future... Spoiler printf("%d", x++, x, ++x); // is undefined behaviour; it might appear to reproduce the same outcome on your computer now, but that's not a requirement for the future It could be justified, "well, C isn't a portable language, after all". No. C is portable. Undefined behaviour is not. C does not define the undefined behaviour. You get no guarantees from the C standard, when you invoke undefined behaviour. It might "work", whatever that means, as expected for mny years, and then break for any reason or no reason at all. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 3 minutes ago, Sebivor said: Don't what? Don't use languages with a horrible syntax and awful performance. 3 minutes ago, Sebivor said: Python and Javascript have no undefined behaviour. Except that Javascript, indeed, does: https://stackoverflow.com/a/14863616/1835564 So why do you still recommend that crap? 3 minutes ago, Sebivor said: Anyone who argues against that point doesn't know C or is trying to engage in psychological warfare. Or knows your recommended languages better than you do. Write in C. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 Just now, Dat Guy said: Don't use languages with a horrible syntax and awful performance. Performance is not so much bound to languages as it is to implementations of languages. For example, there are fast implementations of C (such as gcc, clang, even Microsoft C to an extent) and slow implementations of C (such as Ch, C-INT and cling, which are all interpreters rather than compilers). Similarly, there are fast implementations of JS, such as V8 which compiles to bytecode, where it's fast enough to be considered a backend to llvm/clang via emscripten. That is to say, you can compile your C code to Javascript! Now which is faster, C implemented on top of the Javascript VM or Javascript implemented in C? Technically, most of the C code you execute is no longer C code, as C defines the syntax & semantics of the source code, not what your compiler produces. Quote Except that Javascript, indeed, does: https://stackoverflow.com/a/14863616/1835564 Oh, puh-lease! I expect that you can quote from the authoritative specification. For example, here is the relevant section definining "undefined behavior" in the C11 standard, the very document compiler devs are required to adhere to Quote 1 undefined behaviorbehavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements KEYWORDS: "imposes no requirements". The significance of this is likely yet to hit you, but it will when you read about and consider the distinction between undefined behavior and implementation-defined behavior, a distinction your linked-to answer seems to have glossed over. Here's what C says about implementation-defined behaviour: Quote 1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made KEYWORDS: "documents how the choice is made" In summary, yes, C has implementation-defined behaviours IN ADDITION TO undefined behaviours! Now, please find me an ACTUAL undefined behaviour in Javascript. Remember the keywords: the Javascript specification should "impose no requirements". Quote Or knows your recommended languages better than you do. It's got nothing to do with what I say, nothing to do with what StackOverflow thinks and everything to do with what the C standards and the JS standards say. JS has nothing quite as troublesome as Cs undefined behaviour. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 ... and you can see more support for this in the comment below the answer you cited: Quote Of course, "Implementation-dependent" in JavaScript is nothing like "Undefined Behavior" in C. – supercat Feb 16 '16 at 3:02 > supercat, 47,745 REPUTATION > Sebivor, 11,185 REPUTATION, 88% of which came from answering C-related questions If you don't trust us, trust the standard specs... Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 There is a technical difference between "undefined" and "implementation-defined", but in practice it does not matter; the identical result is: You won't know what your application does for other people. I can live with that (since my C applications are usually available as a binary, so I know what they do on other machines); but you obviously can't...? Write in C. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 34 minutes ago, Sebivor said: printf("%d", x++, x, ++x); // is undefined behaviour; it might appear to reproduce the same outcome on your computer now, but that's not a requirement for the future All my compilers warn about sequence points tough, with the warning levels turned up (-Wall which includes -Wsequence-point). Always compile with highest warning level. Just a FYI, carry on. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 11 minutes ago, Sebivor said: Performance is not so much bound to languages as it is to implementations of languages. No it is not. You said it yourself: C has undefined behavior and languages like Python do not - That's at the root of performance - defining everything ties the implementor's hands. I wrote this about it in a earlier thread: Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 > it does not matter Oh, no, the very core definition of C doesn't matter! It's all just undefined behaviour! There are no guarantees, no legal contracts, anyone can sell you a box with a picture of a phone on it and call it a phone, because "that [thing in the picture]" is a phone! You do understand that your logic undermines every piece of technical documentation ever written, right? What does it matter, if it's all undefined anyway? The documentation gives definition, which gives you the ability to stand in court and say... > Yes, your honour, I followed the C standard recommendations to a TEE! I did not violate the rules of the C standard in any way! It's the compiler's fault for the autonomous car's accident! The compiler had a bug; that bug wasn't mine! You get the same guarantees in Javascript, of course... plus more, because there's no "undefined behaviour" for you to fall trap of! Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 1 minute ago, Unimportant said: No it is not. You said it yourself: C has undefined behavior and languages like Python do not - That's at the root of performance - defining everything ties the implementor's hands. I wrote this about it in a earlier thread: According to the C standard, C11/5.1.2.3p11 The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant. You can see p5 to prove that the relationship between optimisation and undefined behaviour is not necessarily true:4 In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object). Nonetheless, point 1 proves that the performance of the common implementations is not bound to the language, and point 4 proves that even the most complex and effective optimisations (such as dead code elimination) needn't rely upon undefined behaviour. I've also given examples of real world, REALLY SLOW C implementations. You seem to be arguing in the face of both citations and actual examples! According to the C99 rationale, Quote Undefined behavior gives the implementor license not to catch certain program errors that are difficult to diagnose. It also identifies areas of possible conforming language extension: the 35 implementor may augment the language by providing a definition of the officially undefined behavior. What part of the rationale states definitively that the only reason or rationale for "undefined behaviour" is performance? Where did you get this from? Because literally none of my documents have it. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 That's the thing with C haters: They waste too much time trying to denounce C instead of being productive (e.g. in C). Write in C. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 27 minutes ago, Sebivor said: Performance is not so much bound to languages as it is to implementations of languages. For example, there are fast implementations of C (such as gcc, clang, even Microsoft C to an extent) and slow implementations of C (such as Ch, C-INT and cling, which are all interpreters rather than compilers). Similarly, there are fast implementations of JS, such as V8 which compiles to bytecode, where it's fast enough to be considered a backend to llvm/clang via emscripten. That is to say, you can compile your C code to Javascript! Now which is faster, C implemented on top of the Javascript VM or Javascript implemented in C? This. The fundamental concept of Turing completeness. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 I never claimed to hate C. In fact, if you'll see my StackOverflow profile (linked to above), you'll note that it's my most-used language. Where are you getting your information (all of it; about C, about me, etc) from? I'd start questioning your sources... There should be no doubt in your mind that I know C! I quote the same standards the compilers and standard libraries use, and my profile indicates that I'm no rockstar, but I'm also usually on the mark more often than not. I read and cite actual documentation... where-as I notice, you haven't yet done that! What I said initially, which flamed the entire discussion, was C is not a beginner friendly language. It's less beginner-friendly than Javascript; you can overflow buffers, for example, and not know that you've done anything wrong until someone hacks your computer... Or, you can access out of bounds of the array or read an uninitialised variable and get anything from junk to part of the password entered by the previous user! This is kinda why Heartbleed existed... Is there anything like that in Javascript? Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 1 minute ago, Sebivor said: C is not a beginner friendly language. It's less beginner-friendly than Javascript; you can overflow buffers, for example, and not know that you've done anything wrong until someone hacks your computer... Mistakes teach you how to avoid mistakes. C can and will inevitably teach you how resource allocation works. Javascript won't teach you anything about your computer. Write in C. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 5 minutes ago, Dat Guy said: C can and will inevitably teach you how resource allocation works. Javascript won't teach you anything about your computer. What is 143726 * 9304723 / 5? No calculator, pencil or paper. Just because you know something exists or how it works in absolutely no way means you will get it right. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 Ahh, yes... practice makes perfect... By that logic, you can practice acting mentally brain damaged, and you might become perfect at it! You know, that's exactly what I thought over fifteen years ago, when I started learning C... It was C99 back then, but I didn't know about the standards. I thought "I'll just guess, and learn from my mistakes!" Unfortunately, mistakes aren't always fixed immediately. Heatbleed existed for over two years, as a bug, on the internet, before it got patched! Even before the patch, the code still appeared to function properly... Mistakes aren't obvious in C, yet they can have subtly devastating consequences! I spent years learning C, thanks to all of these mistakes, all of the times I got caught with a portability issue, until I found the standard... and an explanation... Then I wished I had made the choice to STOP GUESSING AND START READING earlier!Perfect practice makes perfect perfect! Consider all of the teaching experience some of our reputable professors have, They've seen people who try to wing it, as you suggested; they've seen what makes people confused and their lesson plans reflect it: rather than being stuck trying to diagnose your undefined behaviour for two weeks, the book only teaches that which is defined! Quote C can and will inevitably teach you how resource allocation works. Javascript won't teach you anything about your computer. Oh, lord... Not another "heap" and "stack" hippy! Have you ever tried any other language than C? Probably not, right? That's probably something you'll dabble in when you have ten years of C experience... The only way you can distinguish between the "heap" and the "stack", on your computer, is to invoke undefined behaviour. At this point, your code is no longer C but a subtly different programming language which is specific to your computer (kinda like machine code is specific to your computer). Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted September 13, 2017 1 minute ago, Sebivor said: Have you ever tried any other language than C? Yes, and none of them are similarly "low-level" - except Assembly, but that's nothing in which I would want to write actual software. Write in C. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
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 accountSign in
Already have an account? Sign in here.
Sign In Now