I'm taking Computer Science and ive never coded before and this is my 2nd week in and i am fucking so confused.I can't wrap this shit around my head. I'm not going to give up though.
Could someone please give me a detailed explanation pm everything that is happening in these lines of code as you if you were explaining it to a child. Thank u so much annons
here is the code; int main(void) { int high = 0; int low = 0; float total_high = 0; float total_low = 0; float avg = 0; int i;
printf("---=== IPC Temperature Analyzer ===---\n");
for (i = 0; i < NUMS; i++) { printf("Enter the high value for day %d: ", i + 1); scanf("%d", &high);
printf("\n");
printf("Enter the low value for day %d: ", i + 1); scanf("%d", &low);
no, i excel in mathematics though. Majority of my classmates have been coding for a while. Idk i just feel like a dumbass maybe i am.
Wyatt Jones
Is he actually fucked?
Brody Russell
not really, but he should have prepared better Every programming language resembles C, you should have received an overview of syntax before being dumped into a programming assignment.
Jonathan Bennett
HAHAHAHAHAHAHAHA
Adam Phillips
Should really change majors before you waste 4 years
Jackson Richardson
I plan on majoring in computer science, how should I prepare?
Owen Martin
I'm aware im an idiot, but are any of you going to help me out or not?
jheez anons.
Carson Fisher
Learn to program
Dominic Adams
It calculates average temperature for 6 days. This code is incorrect by the way, NUMS is not defined, but I assume it's 6 due to the way the average value is calculated. All the syntax used here can be learned in, like, half an hour. I recommend
Bentley Sanders
>taking ComputerScience >never coded before
you are fucked. If you come to cs without any programming experience that implies that you are not really interested in it. While its certainly possible to learn from scratch (but forget any social life and any free time) it will be hell on earth for you
Ian Walker
isn't this what teachers are for? go ask them instead of wasting other peoples time, you paid for it
Jason Phillips
i appreciate the criticism, but i wont be switching majors. Im not giving up.
John Kelly
imagine going into a upper division math course and asking how to do long division you will be laughed out of their office
Adrian Collins
I already knew how to program trading bots in high school and I'm not even a CS grad you're boned mate
Next time post code with [spoiler] with code instead of spoiler [/spoiler]. Also, I don't see were you initialize NUMs. This program shouldn't compile or run.
Hunter Scott
Thank you
but i can promise u all i wont drop out. Majority of the students that drop out can't handle the math courses and i excel in them. I just have to study a lot and as user said above i will have no social life.
Ive actually have been thinking about dumping my girlfriend of 2 years just to focus solely on school.
Jace Diaz
program complies and runs fine.
I received help from a friend in class, but i thought if i could get a experienced programmer to explain in detail the syntax and recommendations to improve the code itself it would give me a better understanding
Brandon Fisher
>Ive actually have been thinking about dumping my girlfriend of 2 years just to focus solely on school. normie detected
Ian Howard
No, it doesn't. Lol, you said you didn't know anything. You DIDN'T initialize NUMS. You should listen me.
Joseph Sanders
here is the code; int main(void) { >main fonction int high = 0; >create high variable and assigne 0 (this will be later inputed by the user) int low = 0; >create low temp variable and assign 0 (this will be later inputed by the user) float total_high = 0; >create total_high temp variable and assign 0 (this will be calculated) float total_low = 0; >create total_low temp variable and assign 0 (this will be calculated) float avg = 0; >create average (avg) temp variable and assign 0 (this will be calculated) int i; > create the i variable used for iterations
printf("---=== IPC Temperature Analyzer ===---\n"); >just print that on screen
for (i = 0; i < NUMS; i++) { >for each iteration ( from 0 to NUMS) add +1 to i and: printf("Enter the high value for day %d: ", i + 1); >just print that scanf("%d", &high); >wait for user to input the high temperature value
printf("\n"); >skop a line
printf("Enter the low value for day %d: ", i + 1); >just print that on screen scanf("%d", &low); >wait for the user to input the low temperature value
printf("\n"); >skip a line
Ian Garcia
while (high = 41 || low when considering the high and low inputs, if the high input is superior to low, or if the high input is >41°, or if the low input is print that error message
printf("Enter the high value for day %d: ", i + 1); >print that for each day (%d) as it goes on and increment i for each day scanf("%d", &high); >wait for input for high temperature value for day number "%d"
printf("\n"); >skip a line
printf("Enter the low value for day %d: ", i + 1); >print that for each day (%d) and increment "i" scanf("%d", &low); >wait for input for low temperature value for day number "%d"
printf("\n"); calculate new total_high after user input (0+new high) after the first input total_low = total_low + low; >calculate total low after user input (0+ new low) after the first input
}
avg = (total_high + total_low) / 6.0; calculate the average temperature after 6 inputs (6days)
printf("The average (mean) temperature was: %.2f\n", avg); print the calculated average temp
return 0; >end of the program }
i have never taken a class in programming and don't even know what language is used there ( maybe C?) last time i programmed was in locomotive basic on amstrad CPC lol
Zachary Edwards
pastebin.com/PNuZJhHG Couldn't post it here! So I made a Pastebin it should exlainh everything!
Pls Upvote
Samuel Russell
I dual majored in physics and EE with a minor in mathematics
Logan Jenkins
Pls rate my explanation! Don't his,! I'll add that The scanf("%d," &a) >You see, when you pass a variable to a function it just evaluates that, but when you pass it as a reference (memory reference, by appending & you are giving the function something like 0xfa550t11, the address in memory, so he can modify it in printf()
Luke Mitchell
>avg = (total_high + total_low) / 6.0;
why they divide by 6 and not by "i" there? this would only work after 6 inputs whereas "i" would work for any number of inputs?
Christian Stewart
Void in main's arguments is fine, he isn't passing arguments from the command line to the program. Void is okay, and won't break anything; he could have left it blank with no difference to how it runs.
Jaxon Moore
He is majoring, it's considered bad programming, because, people actually pass arguments and some compilers may get it wrong, because it's standardized(or something like that)
Anyways you got a nice (You) wanna see mine?
Chase Williams
what is %d? a variable? it's not declared?
James Roberts
Floating point arithmetic.
Look up printf.
Jack Sanchez
Idk about all that, I'm self taught, but you said: > //main is the entry point, or starting point of the program, and void is what arguments it takes, void in main is not a good thing, it's usually bad. Not very academic, imho.
Juan Rivera
It's an integer conversion specification. Basically it tells printf to expect and format, what it is passed, as a four byte integer.
Gavin Diaz
i appreciate all the explanations guys
I don't understand what you're deferring too when you're requesting i upvote you? where?
NUMS indicates the amount of days i want the program to ask the user to input, defines constant macros
How would i go about adjusting the code if i wanted to display the highest & lowest days or the mean of an x amount of days not just the 3?
Joshua Rodriguez
%d flags are a nice and ancient convention used by the printf family functions as a consequence of how primitive C's type system is if you want to write generic code to tell a function what it's about to receive, you need to add a flag for every type you intend to support there's also some hardcoded hacks in the compiler that allow stdargs.h and by extension, variable argument lists like int func(const char *fmt, ...); to work
Kayden Collins
referring*
Kevin Bennett
Why are they teaching C to beginners? what happened to SICP and Smalltalk and shit
Connor Fisher
>if you want to write generic code to tell a function what it's about to receive, you need to add a flag for every type you intend to support
oh i see, thanks. this seems kinda redundant since you already told what kind of content (int, float..) should be assigned to the variable when you declared it
Mason Brooks
Highest and lowest days: Make two new variables (ie "highest"). Initialize them. During that loop where you collect the temps, check if the temp is greater than the highest temp you've seen so far (stored in that new variable, "highest"). If it is, replace the current value of highest with the current temp, ie if(high > highest) { highest = high; }
You're not going to learn C like this. You have to practice. youtu.be/ot9LoXNSqLU You're welcome.
Benjamin Clark
compiled c code (ASM) has no concept of types, only memory addresses and layouts of user-defined compound types (structs). you're telling the function how to interpret the arguments in printf because it doesn't know otherwise, remember this is cutting edge 1972 technology.
other than that, C's ancient quirks don't come up again
Noah Wilson
if you didn't specify the %d flag and the user inputs "potato" as a value for the high "high" variable, the program would assign the float value of the word "potato" ??
Brayden Kelly
>assign the float value *assign the integer value
Easton Morgan
it'll attempt to interpet the 64-bit wide const char * pointer you passed in as an 32-bit integer and lead to undefined behavior best case is printing out a really high number and trying to interpet it as bytes forming a signed integer
William White
>Majority of the students that drop out can't handle the math courses from my experience, it's because of the programming classes ie. change majors
Jonathan Foster
ook
for instance in python if you input a char for an int value i think it would just crash
Nicholas Rodriguez
Google what variables and loops are dude. This shit is way easier than math, I can program and I've failed multiple math classes.
James Long
The Program would assign "potato" to the variable. This might break your program, if your input length isn't checked, you could have a buffer overflow; which is a security no-no. Strings aren't a type in C. So "potato" actually has a numerical value.
Anthony Russell
This explains why math majors write such godawful code. They just get shoved into it without any foundational programming logic skills or even a tutorial on C.
Isaiah Morales
you're just a dumbass, go do something easy like gender studies
Logan Harris
Python is "higher" level, and is considered memory safe. C doesn't protect you from such errors. You have to make sure that fuckeries like that don't occur.
Jaxon Johnson
you will drop out, you have no interest
Caleb Garcia
thanks man
Caleb King
ook i understand why they often teach python and not C for programming 101 that and the shell, and the debugger...
Sebastian Torres
aren't algorithms and Boolean logic taught in high school though?
Angel Ward
I didn't even know what those words were till I picked up a programming book. I also skipped high school calc, but that's because I hated math
Kevin Ross
I would recommend python first. After some basics, come back to C. The way C handles memory is a bitch to understand, especially if you're learning on your own. I only learned it recently. How the stack and heap works might too much for you right now.
Alexander Walker
It's an elective, which I thankfully took.
Asher Allen
python teaches lots of bad habits learn C first, all other languages become childs play afterwards modern languages are built on convenient abstractions and you need to know what they're abstracting away before you can see the benefits of using them at all
Nathan Moore
I agree, python is rather ugly; and I used to think it looked nicer than anything else. However, he is struggling with basics. How can he learn if needs help with variable assignment, for loops, scanf, and printf? I bet he doesn't know his way around a terminal. Not to shit on you, man.
Ryder Williams
ook in my country they're taught from 9th grade for all science and technology sections
Isaiah Russell
That makes sense. You can't study any field of science without understanding basic logic (And, Or,If, Then)... these are basic tools needed for any rational reasoning.
Andrew Morris
Oh, you're not American? Where are you from?
Blake Nelson
belgium, kek
Aiden Garcia
These can be learned in an afternoon tho. Why do people think conditional logic is so hard?
Lincoln Torres
>Majority of the students that drop out can't handle the math courses
That is literally just a meme Sup Forums tells itself. I honestly can't believe someone fell for the meme.
If you actually excel at the math, then just become a fucking math major.
Leo Anderson
*conditional expressions
Ethan Clark
That makes sense why you're having trouble picking this stuff up. Guess I'm lucky I was born over here. Good luck, man. Hope the best.
Justin Watson
i'm Anonymous btw, not Anonymous
Ryder Taylor
>You can't study any field of science without understanding basic logic (And, Or,If, Then) You mean common sense?
Aiden Collins
C is a difficult language to start with, but rather rewarding and helpful in the long run when you succeed. I started with C and was able to teach myself a lot. The key is really understanding what you are doing. Do not start with code someone else wrote that you don't understand. The basics of C are actually relatively simple, you don't need to learn much to get started. Read a C book, look over the example code, and then experiment with writing simple programs. Change small things and observe the differences. You must learn by doing, no one can learn it for you. Good luck, you can succeed. Do not make it out to be more difficult than it is. God speed.
Liam Jenkins
Learn C++, Java, JavaShit, HTML5/CSS on your own before you take any of those classes. Like, get a book and learn the language with the book. Then when you're breezing through the classes, learn languages you actually care about, or even better, learn more math. Every good computer scientist knows math well. There are no exceptions.
Sebastian Miller
English is your native language. You're not appreciating how intuitive the syntax is for you.
Colton Campbell
Except every dick head Prof. has there coding style/preferences that you need to observe so that you have to worry about each fucking line you write. I have 3 C++ courses and each professor expects a different style.
Cameron Taylor
This. We need more people in gender studies.
Juan Allen
We also need less people of the people in gender studies.
Tyler Brooks
No. Logic (as the user you referenced is using that word)and common sense are not the same.
Alexander White
>Collge
Also, English Comp 1 & 2
Nolan Nelson
I wasn't aware! Thanks user. All the teachers I had in the CS department didn't make any mention about forced preference/style. All the programming I have cared about has been for MATLAB, and most of that in my spare time for pleasure. pls no bully.
Lincoln Jones
not neccessarily, you might still have a chance if you can think in a non-linear fashion. the quickest way to learn is fuck with the code and see how it produces different results.
if that is too difficult, bail now
Daniel King
Kekistan
Brody James
well de Mrogan's theorem or Karnaugh mapping go a bit beyond common sense...
James Murphy
When I went to university my school had a beginning programming class. Had never written code once before.
I guess I got lucky enough that my school was willing to wipe my ass for me to the point where I finally got a job.
It's gonna take up a lot of your free time, but you can do it, user.
Kayden Cox
It all depends on the Prof, I guess you might get lucky. I've lost points for the dumbest shit. No blank line between C library includes and C++ library includes, -5 points. But Bessie's shit like that, it's not so bad. Also MATLAB is a joke.
Colton Cruz
DeMorgans makes logical sense if you take the time to break it down.
Nathaniel Young
yeah but it's not innate, some logical people may use it without even knowing what they are doing but i mean it's not common if you don't learn it.
Hunter Rogers
how do non-english speakers learn to program? do they learn english or do they just associate if, else, or, etc. as abstract keywords that do specific things and not as actual words?
Wyatt Smith
most if not all people learn some english in middle school otherwise yes, they have to associate the syntax with abstract concepts (a bit like maths concepts)
Caleb Wilson
i.e.: you don't know what a "log" means until you learn what a log is.
Joshua Cox
If you're good at math. I recommend you pick up realm of racket to learn how to program. The language was made for math majors to pick up easily and learn key programming concepts. General advice I can give you is that you can't manipulate or use data in c without having some where to put it and you can't manipulate something that doesn't exists yet. Once you master that it becomes a lot easier. Don't listen to any of the dicks on here anyone can learn to write basic programs. If you're having real trouble bring a laptop to your TAs office hours and write code there. Than you can ask them questions when you get stuck. Also read K&R it's perfect of beginners in C. It's one of the most brilliant programming books ever written. Good Luck.