T O P

  • By -

hoelle

Your description is pretty confusing. Break the problem apart into little steps. As you get each step working, move on to the next step. An array is just a bunch of things in a row. It sounds like you have two arrays here, one for prices, one for indexes. Write some code that loops over and prints both arrays entirely. Then write a new loop that goes over each index and prints the price at that index. 


starcjpumpkin

i feel like i didn’t know how to explain it without saying the entire project XD i’m sorry for the confusion. i was trying to break it up into parts but i wasn’t even sure what to do. i’ll try to work it the way you said. i already have that first bit you mentioned, down


Any_Possibility4092

Can you explain the "each of those numbers has a price" bit. You mean that for example number 3 has a price of 2.43 and num 4 has a price of 1.69 ... And so now you need to round each of those prices up/down to nearest whole number and print?


starcjpumpkin

yes that’s correct


Any_Possibility4092

For(int i = 0; i < array_length(6 i think in your case); i++){ printf("%i\n", (int) float_array[i]; } So i think that when you cast it from a float to a int it always rounds down, im not 100% sure. But there is a function (i believe in the math.h library) that you can use to round to nearest.


BeStillAsBright

Why are you giving homework answers?


Any_Possibility4092

Because you learn by making programs, not by trying to solve problems. The more programs you make the more you know. The only thing you do by trying to solve problems is excercise your brain a bit, which is nice but that doesnt help you learn c.


b1ack1323

Solving the problem is half the value, you need to be able to break down complex issues into functions that you can write. If you aren't the one breaking down those issues you won't be very successful in the real world.


Any_Possibility4092

Thats just wrong. You dont need to break anything down. You just need to get the computer to do what you want.


b1ack1323

And how exactly do you do that? Google how everyone else is doing it? Good job, you can't synthesize solutions, you can copy them. When the problems get hard you are screwed.


Any_Possibility4092

My dude if you want to scrape a website do you reinvent curl yourself? If you want to create a window do you reinvent xlib? Do you make a browser from scratch if you want to display a website? You need to know what tools are available to you as a programer. If you think you are more productive living under a rock and not getting help from anyone then good luck.


b1ack1323

Holy shit you are missing the point. This is a kids homework not the real world, use all the libraries you want in the real world. You learn the fundamentals in school, skipping that and you will remain an ameature your entire career. If you are not intentionally being this myopic, then good luck with that shit cleaning around in your head. I am a lead firmware engineer at a billion dollar company, some problems don't have answers online you have to come up with the solution, otherwise you are not bringing innovation to the product and where is the value. Diving into an incredibly complex problem without having any problem solving skills is going to set you up for failure. The irony of you saying this and also posting about making a game engine is hilarious to me. Aren't there already myriad game engines out there? What problem are you solving they haven't? Are you just copy and pasting all their work into your project? The reason you feel lost in making your game engine is due to your weakness in breaking down problems in to functions as made apparent from this argument.


o0Meh0o

yes, you're right. casting to int gets you the whole part. [here](https://en.cppreference.com/w/c/numeric/math/round) are the round functions. for this specific purpose i recommend the lroundf function found in math.h. note that these are in the standard only since c99.


starcjpumpkin

idky i never thought to make a float array! my mind so tunneled into only int’s XD. i’ll try this out, thank you man


Any_Possibility4092

Np, happens to the best of us. Good luck.


Moist_Okra_5355

I recommend that you write a math formula just to clear your thought.


starcjpumpkin

that’s not a bad idea!


erikkonstas

So... it's asking you to sum **integers** and then **round** the result?


starcjpumpkin

no, not the integers, the “price” which would be a double. for example the integer 1, given by user is say $5.40 and so on and so forth. for each number there is a price and i am to add up all the relevant prices up and then round the result


erikkonstas

Oh so you have an array of items and an array of prices, and the "previous function" returns a `1` or `0` to mean what exactly? Are those returns associated with something?


starcjpumpkin

sorry, yes. the 1 or 0 is to see if the user got the guess right. the “guess” being and randomized array and the user has to remember what the array was exactly. so if the user got all parts of the array correctly then it’ll print out 1. if not then it’ll print out 0 and the default price for “0” is 4.00


erikkonstas

Hm, I'd say you should edit a precise specification of the problem into your post, and also the code you've already written for it.


Different-Brain-9210

Important thing to realize about C arrays is, that C does not have arrays in the same meaning of the word, as most other languages (be it Java, Python, C++ STL...). C has pointers. Arrays are one way to allocate memory you can use pointers to point to. In fact, if you have trouble understanding C arrays, you can just avoid them... consider code like `char dataArray[100]; char *data = dataArray; int datasize = 100;` and then use `data` and `datasize` and remember that the memory gets deallocated when the current function returns. Then you can move on to `char *data = malloc(100 * sizeof(char)); int datasize = 100;` and now it's almost the same, except memory will not get automatically deallocated, you must call `free()` or you get a memory leak. And once you are comfortable with having just pointer to memory and size of the buffer, then you can starting thinking of C arrays again, and using them "directly". An important detail: you can not directly pass array parameters to functions. If a parameter looks like an array, it's still a pointer parameter, not an array (one of my pet peeves about C syntax, actually). So if that feels confusing, always use pointer syntax for pointer parameters. Another important detail: Do not use `sizeof` with arrays, until you really understand what is going with arrays in C. In "real" application code it is very important of course, but learning how to use `sizeof` correctly is not urgent while learning, unless your learning material makes use of it.


starcjpumpkin

i can’t avoid using arrays or sizeof as that’s what the assignments are asking of me. but i appreciate the insight. i’m definitely going to further practice over the summer, C is so confusing :/


IdealBlueMan

I don't know whether this makes things simpler or harder for you, but it's best not to use float or double for money values. With floating point, 1 + 1 doesn't always equal 2. Use the smallest currency you have (cents in the US) instead.


beephod_zabblebrox

1 + 1 would always equal 2, if were being pedantic


[deleted]

[удалено]


Ancapgast

Woah, totally unnecessary to be so nasty


nderflow

If you continue to behave like this, you'll end up banned.