• What do you need help with? Version 1
    5,001 replies, posted
[QUOTE=Zally13;24953047]Alright, I need some help here. I'm new to C++, only on the 47th page of my first book, and I don't exactly get what this does. I somewhat get vectors, I know what most of it means, but I need someone to thoroughly explain it to me, a newbie. [code]#include <algorithm> -snip- [/code][/QUOTE] It would help if you specified what you don't understand. It won't be really helpful if I just reread the appropriate Accelerated C++ chapter to you.
[QUOTE=ROBO_DONUT;24952389][code]status: :3[/code][/QUOTE] LOL? [QUOTE=shill le 2nd;24952826]C lets you shoot yourself in the foot. The most it would ever do is give you a warning, not an error. It would still let the program compile since it's still a valid statement; assignment returns a value, so the if statement evaluates that value. But now I'm curious about whether "if (func())" would be an error if func were void.[/QUOTE] -Werror [QUOTE=Zally13;24953047]Alright, I need some help here. I'm new to C++, only on the 47th page of my first book, and I don't exactly get what this does. I somewhat get vectors, I know what most of it means, but I need someone to thoroughly explain it to me, a newbie. [code]...[/code][/QUOTE] [cpp]#include <algorithm> #include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> using std::cin; using std::sort; using std::cout; using std::streamsize; using std::endl; using std::string; using std::setprecision; using std::vector int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; //ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for and read the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; //::I will assume that you understand everything prior to this line vector<double> homework; //::This will create a vector with doubles double x; // invariant: homework contains all the homework grades read so far while (cin >> x); homework.push_back(x) ; //::push_back adds something to the vector // check that the student entered some homework grades typedef vector<double>::size_type vec_sz; //::vec_sz is now a numerical type, that can represent indices of our double-vector vec_sz size = homework.size(); if (size == 0) { cout << endl << "You must enter your grades. " "Please try again." << endl; return 1; } // sort out the grades sort (homework.begin(), homework.end()); //::calls sort-function and passes in the range to sort (from begin to end) //::vector-related code ends here. I will assume that you also understand the code following that line // compute the median homework grade vec_sz mid = size/2; double emdian; media = size % 2 == 0 ? (homework[mid] + homework[mid=1]) / 2 : homework[mid]; // compute and write the final grade streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl; return 0 } [/cpp] I marked my comments by beginning with ::. And you probably know this, but since you didn't state exactly what you had problems understanding: vectors are dynamically sizable arrays.
How would I go about generating a convex polygon from an image in order to draw dynamic shadows in 2D? I'd assume scanning the image from left to right of each pixel and add a new vertex if every pixel to the left is transparent and the last vertex had a different x value, and then scan from right to left and do the same thing.
Not sure if this is what you mean but you could flood fill the non transparent parts of the image and put a vertex every time the fill reaches a transparent spot. It would outline every polygon tho they wouldn't be convex unless they already were in the original image.
[QUOTE=NorthernGate;24962756]How would I go about generating a convex polygon from an image in order to draw dynamic shadows in 2D? I'd assume scanning the image from left to right of each pixel and add a new vertex if every pixel to the left is transparent and the last vertex had a different x value, and then scan from right to left and do the same thing.[/QUOTE] I've actually made a program that did that before. What I did was I used [url=http://en.wikipedia.org/wiki/Graham_scan]Graham scan[/url], but instead of sorting it all by angle to one point I scanned from right to left upwards and then left to right downwards.
[QUOTE=Zally13;24953047][code] // sort out the grades sort (homework.begin(), homework.end()); // compute the median homework grade vec_sz mid = size/2; double emdian; media = size % 2 == 0 ? (homework[mid] + homework[mid=1]) / 2 : homework[mid]; // compute and write the final grade streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl; return 0 } [/code][/QUOTE] I don't get this part except for the return 0.
[QUOTE=Zally13;24967488]I don't get this part except for the return 0.[/QUOTE] [code] // sort out the grades sort (homework.begin(), homework.end()); [/code] Sorts homework, like it says. [code] // compute the median homework grade vec_sz mid = size/2; double emdian; media = size % 2 == 0 ? (homework[mid] + homework[mid=1]) / 2 : homework[mid]; [/code] The ?: is a conditional operator. It checks the left expression and chooses one of the values to the right. "a=b?c:d" is the same as "if (b) { a = c } else { a = d }" So "mid" is the index of the middle element in the array. "size % 2 == 0" tests whether "size" is even or odd. If it's even, media is set to "(homework[mid] + homework[mid=1]) / 2", or the average of the two middle elements. If it's odd, it's just the middle element. [code] // compute and write the final grade streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl; return 0 } [/code] I'm not much of a C++ programmer, but all the precision stuff looks like it might be for formatting floating point numbers. It sets "precision" to 3, calculates and prints the weighted average of the grades, then restores the original precision value. [editline]10:32PM[/editline] I think there's a typo in there. "(homework[mid] + homework[mid=1]) / 2" should probably be "(homework[mid] + homework[mid[highlight]+[/highlight]1]) / 2"
Yeah I noticed quite a few typos... Probably why it didn't compile properly.
Is it possible to make a program that just outputs text run as a standalone rather than having to compile it? It's Java, and this is the .java: [url]http://filesmelt.com/dl/cdc.java[/url]
[QUOTE=ROBO_DONUT;24967862][code] // sort out the grades sort (homework.begin(), homework.end()); [/code] Sorts homework, like it says.[/QUOTE] Expanding on this a little: all the standard containers, like std::vector, have an associated type called an "iterator" that's used for going through the items in the container. Think of it as a cursor that sits on one item at a time, and can be used to access that item and can also be moved to the previous or next item. The begin() function returns an iterator that refers to the first item in the container, and the end() functiion returns one that's "just past the end" of the container. If you wanted to loop over all the grades in the homework vector, you'd do something like this: [cpp] for (std::vector<double>::iterator iter = homework.begin(); iter != homework.end(); ++iter) { double &x = *iter; // Take the number that iter currently refers to and call it "x" // Do stuff with x... } [/cpp] which means to make an iterator that starts on the first grade, and as long as it hasn't gone through all the grades, do stuff with the current grade and then move on to the next one. Passing a pair of iterators to the sort() function is how you tell it what range of numbers it should sort. Internally, it can use them for something like the above loop. (If you just wanted to sort a particular region of the homework vector, rather than the whole thing, you could pass a pair of iterators that surround just that region, and it'd work just the same.)
[code]double emdian; media = size % 2 == 0 ? (homework[mid] + homework[mid=1]) / 2 : homework[mid];[/code] everything about that line is WRONG here, I fixed it [code]double median; median = size % 2 == 0 ? (homework[mid] + homework[mid+1]) / 2 : homework[mid];[/code]
OK so i just looked up pointers in C++, and i understand how there created and what they do I just dont see why to use them? Ok so in a big project where you dont know what your going to have in it, fine i can see the use But why use a pointer to point to a variable? why not just use the variable its self?
[QUOTE=Richy19;24976308]OK so i just looked up pointers in C++, and i understand how there created and what they do I just dont see why to use them? Ok so in a big project where you dont know what your going to have in it, fine i can see the use But why use a pointer to point to a variable? why not just use the variable its self?[/QUOTE] Because you might not have access to the variable itself. This is when you need to dynamically allocate stuff, or otherwise not have variables stay 'local' to your function. [cpp] char* myBuffer = (char*)malloc(1000); [/cpp]
[QUOTE=ZeekyHBomb;24959233]I marked my comments by beginning with ::.[/QUOTE] Why didn't you put them in your own namespace?
Didn't want to make a whole new thread for a single simple question: I've been programming in C# for 4 (maybe 5?) years now and I feel like I have plateaued and want to expand my knowledge of the language even further. (And get back into the swing of it, not programming for 6 months isn't very productive) Anyone know of any sites (or books) that cover more obscure, intermediate/advanced subjects?
[QUOTE=turb_;24976339]Because you might not have access to the variable itself. This is when you need to dynamically allocate stuff, or otherwise not have variables stay 'local' to your function. char* myBuffer = (char*)malloc(1000); [/QUOTE] He's doing C++, not C. char* myBuffer = new char[1000];
[QUOTE=Richy19;24976308]OK so i just looked up pointers in C++, and i understand how there created and what they do I just dont see why to use them? Ok so in a big project where you dont know what your going to have in it, fine i can see the use But why use a pointer to point to a variable? why not just use the variable its self?[/QUOTE] Because they're required to make complex data structures like trees, heaps, linked lists, etc. Also, you can cast pointers to interpret the same set of data in different ways. This doesn't really apply as much to C++, but it's very useful and often necessary in C.
[QUOTE=Richy19;24976308]But why use a pointer to point to a variable? why not just use the variable its self?[/QUOTE] Another usage is a reassignable reference and compatibility to C-libraries.
Is there a way to stop visual c# from "fixing" my code for me? I hate how it automatically turns [code] public int Foo() { } [/code] into [code] public int Foo() { } [/code] Or how it puts spaces on either side of operators. [code] i= 5; i = 5; [/code]
You could probably do it from the settings menu, but why the heck don't you want spaces on both sides of operators?
I'm looking at the options and it doesn't seem to be here.
[QUOTE=Richy19;24976308]OK so i just looked up pointers in C++, and i understand how there created and what they do I just dont see why to use them? Ok so in a big project where you dont know what your going to have in it, fine i can see the use But why use a pointer to point to a variable? why not just use the variable its self?[/QUOTE] Lets say you have a large data structure in memory. 200mb of data in the ram. You want to pass that structure to a function so it can access the data. If you pass the object itself you're going to find yourself using 400mb of ram. It gets the job done but it's not very efficient. What happens if you decide you want to edit the data structure from the function? Now you have to return the modified data structure too, yet more memory and processing power wasted. Now lets try passing a pointer to the data structure instead. Now that's a lot more efficient. How about a parent-child hierachy. You want a child to keep track of its parent but you cant put a copy of the parent in the child. It would be a bit of a paradox. Now try storing a pointer to the parent. Problem solved.
[QUOTE=RyanDv3;24979636]I'm looking at the options and it doesn't seem to be here.[/QUOTE] [img]http://anyhub.net/file/17capture.png[/img]
[QUOTE=eXeC64;24979735]Lets say you have a large data structure in memory. 200mb of data in the ram. You want to pass that structure to a function so it can access the data. If you pass the object itself you're going to find yourself using 400mb of ram. It gets the job done but it's not very efficient. What happens if you decide you want to edit the data structure from the function? Now you have to return the modified data structure too, yet more memory and processing power wasted. Now lets try passing a pointer to the data structure instead. Now that's a lot more efficient. How about a parent-child hierachy. You want a child to keep track of its parent but you cant put a copy of the parent in the child. It would be a bit of a paradox. Now try storing a pointer to the parent. Problem solved.[/QUOTE] In C++ you would normally use references for this, which are alike pointers except that they must be initialized to reference something (e.g. no NULL, no late initialization) and cannot be reassigned.
[QUOTE=pikzen;24979754][img]http://anyhub.net/file/17capture.png[/img][/QUOTE] The network I'm on is blocking this for some reason.
[QUOTE=ROBO_DONUT;24977673]Also, you can cast pointers to interpret the same set of data in different ways. This doesn't really apply as much to C++, but it's very useful and often necessary in C.[/QUOTE] And in C++ you use pointers for polymorphism, which means you can treat an inherited object as its base class (i.e., interpret only the data you need to).
[QUOTE=shill le 2nd;24980092]And in C++ you use pointers for polymorphism, which means you can treat an inherited object as its base class (i.e., interpret only the data you need to).[/QUOTE] You can also do a kludgy form of OOP and polyporphism in C using the right combination of structs, macros, pointer casting and callback functions. It's sort of what GObject does. [QUOTE=eXeC64;24979735]Lets say you have a large data structure in memory. 200mb of data in the ram. You want to pass that structure to a function so it can access the data. If you pass the object itself you're going to find yourself using 400mb of ram. It gets the job done but it's not very efficient.[/QUOTE] I don't think you could even do that. It would be like an instant stack overflow.
[QUOTE=shill le 2nd;24980092]And in C++ you use pointers for polymorphism, which means you can treat an inherited object as its base class (i.e., interpret only the data you need to).[/QUOTE] Also works with reference types?
[QUOTE=eXeC64;24979735]Lets say you have a large data structure in memory. 200mb of data in the ram. You want to pass that structure to a function so it can access the data. If you pass the object itself you're going to find yourself using 400mb of ram. It gets the job done but it's not very efficient.[/QUOTE] Probably a bit over exaggerated but i get the point
I'd say the most real world example of using references are vectors full of actual objects (not pointers) and strings.
Sorry, you need to Log In to post a reply to this thread.