Help with C Programming, please

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);

printf("\n");


while (high = 41 || low

Other urls found in this thread:

my.mixtape.moe/xvommf.pdf
pastebin.com/PNuZJhHG
youtu.be/ot9LoXNSqLU
twitter.com/SFWRedditGifs

CHANGE MAJORS NOW

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.

Is he actually fucked?

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.

HAHAHAHAHAHAHAHA

Should really change majors before you waste 4 years

I plan on majoring in computer science, how should I prepare?

I'm aware im an idiot, but are any of you going to help me out or not?

jheez anons.

Learn to program

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

>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

isn't this what teachers are for? go ask them instead of wasting other peoples time, you paid for it

i appreciate the criticism, but i wont be switching majors. Im not giving up.

imagine going into a upper division math course and asking how to do long division
you will be laughed out of their office

I already knew how to program trading bots in high school and I'm not even a CS grad you're boned mate

Learn C then.
my.mixtape.moe/xvommf.pdf

then drop out once you hit the wall

Then what are you majoring in?

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.

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.

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

>Ive actually have been thinking about dumping my girlfriend of 2 years just to focus solely on school.
normie detected

No, it doesn't. Lol, you said you didn't know anything. You DIDN'T initialize NUMS. You should listen me.

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

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

pastebin.com/PNuZJhHG
Couldn't post it here!
So I made a Pastebin it should exlainh everything!

Pls Upvote

I dual majored in physics and EE with a minor in mathematics

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()

>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?

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.

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?

what is %d? a variable? it's not declared?

Floating point arithmetic.

Look up printf.

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.

It's an integer conversion specification. Basically it tells printf to expect and format, what it is passed, as a four byte integer.

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?

%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

referring*

Why are they teaching C to beginners? what happened to SICP and Smalltalk and shit

>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

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;
}

Collge Algebra
Trigonometry
Calculus 1 + 2
University Physics 1 + 2

You're not going to learn C like this. You have to practice.
youtu.be/ot9LoXNSqLU
You're welcome.

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

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" ??

>assign the float value
*assign the integer value

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

>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

ook

for instance in python if you input a char for an int value i think it would just crash

Google what variables and loops are dude. This shit is way easier than math, I can program and I've failed multiple math classes.

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.

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.

you're just a dumbass, go do something easy like gender studies

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.

you will drop out, you have no interest

thanks man

ook i understand why they often teach python and not C for programming 101
that and the shell, and the debugger...

aren't algorithms and Boolean logic taught in high school though?

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

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.

It's an elective, which I thankfully took.

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

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.

ook
in my country they're taught from 9th grade for all science and technology sections

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.

Oh, you're not American? Where are you from?

belgium, kek

These can be learned in an afternoon tho.
Why do people think conditional logic is so hard?

>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.

*conditional expressions

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.

i'm Anonymous btw, not Anonymous

>You can't study any field of science without understanding basic logic (And, Or,If, Then)
You mean common sense?

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.

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.

English is your native language. You're not appreciating how intuitive the syntax is for you.

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.

This. We need more people in gender studies.

We also need less people of the people in gender studies.

No. Logic (as the user you referenced is using that word)and common sense are not the same.

>Collge

Also, English Comp 1 & 2

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.

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

Kekistan

well de Mrogan's theorem or Karnaugh mapping go a bit beyond common sense...

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.

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.

DeMorgans makes logical sense if you take the time to break it down.

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.

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?

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)

i.e.: you don't know what a "log" means until you learn what a log is.

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.

/thread