Aiwendil

Members
  • Content count

    118
  • Joined

  • Last visited

Community Reputation

158 Neutral

About Aiwendil

  • Rank
    Member
  1. Another C question (sorting arrays)

    I thought & was the address and * was the value. Either way, I tried it and it didn't work. Thanks for trying to help though.
  2. Hi again, Sorry to be asking so many questions, but I have another problem. I'm trying to write a program that takes command line parameters and sorts them, then does various operations on them. Everything works fine except for the sorting. #include <stdio.h> #include <stdlib.h> float variance(char* data[], float average, int num); void swap(char *a, char *b); int main(int argc, char *argv[]) { int i; float total = 0; float mean = 0.0; printf("Data: "); for (i = 1; i < argc - 1; i++) { if (argv[i] > argv[i + 1]) { swap(&argv[i], &argv[i + 1]); } } for (i = 1; i < argc; i++) { printf("%s ", argv[i]); } printf("\nMaximum: %s\n", argv[argc - 1]); printf("Minimum: %s\n", argv[1]); for (i = 1; i < argc; i++) total += atoi(argv[i]); mean = total / (argc - 1); printf("Mean: %.2lf\n", mean); printf("Variance: %.2lf\n", variance(argv, mean, argc)); } float variance(char* data[], float average, int num) { float sum = 0.0; int i; for (i = 1; i < num; i++) { sum += (atoi(data[i]) - average) * (atoi(data[i]) - average); } return (sum / (num - 1)); } void swap(char *a, char *b) { char tmp = *a; *a = *b; *b = tmp; } I've also tried sorting inside main without using swap and a few other ways, but none of them really do anything. What am I doing wrong? Thanks
  3. Why is my program skipping fgets()?

    Thanks, that fixed it.
  4. Why is my program skipping fgets()?

    Thanks for the replies. So is that scanf("\n...", ...); or what?
  5. Hi again, I'm trying to get the user to enter a string in C by using fgets(), but my program keeps skipping over the input secion that uses fgets(). Can someone help me figure out why it's skipping? #include <stdio.h> int main(void) { char semester; char input = ' '; int year; char inputArray[50]; char* className; int lowGrades[4]; int numStudents = 0; int i, j; int student[50][3]; char *studentInitials[50]; printf("\t\tSEMESTER GRADE REPORT\n"); printf("\t\t-------- ----- ------\n\n"); printf("You will need the class identification, the initials of\n"); printf("and the three test scores for each student, and the low\n"); printf("cutoff score fo a grade of 'A', 'B', 'C', and 'D'.\n\n"); printf("When you have entered the scores for the last student,\n"); printf("press Return at the initials prompt.\n\n"); printf("Press Return when you are ready to proceed:\n\n"); printf("Fall or Spring semester? (F or S): "); scanf("\n%c", &semester); printf("Year: "); scanf("%d", &year); printf("\n"); printf("Which class: "); className = fgets(inputArray, 50, stdin); printf("\n"); for (i = 0; i < 4; i++) { if (i == 0) printf("Low score for an '%c' (out of 300) : ", i + 65); else printf("Low score for a '%c' (out of 300) : ", i + 65); scanf("%d", &lowGrades[i]); } for (i = 0; i < 4; i++) { printf("Student's initials: \n"); studentInitials[i] = fgets(inputArray, 50, stdin); printf("Scores:\n"); printf(" Test #1: "); scanf("%d", &student[numStudents][0]); printf(" Test #2: "); scanf("%d", &student[numStudents][1]); printf(" Test #3: "); scanf("%d", &student[numStudents][2]); printf("\n"); numStudents++; } return 0; } Also, is there a way to tell if only the enter key has been pressed? I want the user to be able to add students until they press only enter for student initials (I know the loop won't really work for that right now, and I'll change it later when I know how to get only enter as input). Thanks
  6. Games Similar to Zelda

    What are some games that are similar to Zelda but are targeted towards an older audience?
  7. Rotating a Rectangle

    Thanks for the reply. Quote:If it's for collision detection purposes, you don't need to store the corners. If the rectangle will always be axis-aligned, you can store the rectangle in min/max form or center/extents form. If the rectangle can be arbitrarily orientated, a standard representation is a center point, two (or three) basis vectors describing the orientation, and two (or three) extents, one for each basis vector. Can you give an example or provide a link to an example/tutorial (preferably with code)? I don't want to just copy the code, but I think it might make more sense if I can see the actual code for something like this along with the description.
  8. Rotating a Rectangle

    Quote:As a side note, applying incremental transforms to geometry over multiple updates is rarely the right approach. I'm not saying there are no circumstances where it's appropriate, but it's far more typical to store a local-space copy of the geometry and then transform it 'from scratch' as needed. Is that the same thing as instancing? I was making this so that I could use it for collisions. Would it be better to just store four points and use those to make the collision rectangle? For example: class Object { ... D3DXVECTOR2 mCollisionPoint[4]; }; Each point would be offset a certain distance from the object to match where it needs to be to make the collision work properly.
  9. Rotating a Rectangle

    Ok, here's my updated code (only the updates are shown, the rest is the same as above). void CustomRectangle::rotate(float degrees) { mRotation += degrees; D3DXVECTOR2 temp = mPos; mPos.x = 0.0f; mPos.y = 0.0f; updateCorners(); for (int i = 0; i < 4; i++) { mVertices[i].x = (mCornerPos[i].x * cosf(mRotation) - mCornerPos[i].y * sinf(mRotation)); mVertices[i].y = (mCornerPos[i].x * sinf(mRotation) + mCornerPos[i].y * cosf(mRotation)); } mPos.x += temp.x; mPos.y += temp.y; updateCorners(); } Now it moves to the origin, but doesn't move back. The other problems that I've found is that calling the move function doesn't move the rectangle, and when I create a rectangle with a rotation, it doesn't start with the rotation applied. void CustomRectangle::create(float x, float y, float width, float height, float rotation) { mPos = D3DXVECTOR2(x, y); mWidth = width; mHeight = height; rotate(rotation); mCornerPos[0] = D3DXVECTOR2(x, y); mCornerPos[1] = D3DXVECTOR2(x + width, y); mCornerPos[2] = D3DXVECTOR2(x + width, y + height); mCornerPos[3] = D3DXVECTOR2(x, y + height); for (int i = 0; i < 4; i++) mVertices[i] = mCornerPos[i]; } void CustomRectangle::create(const D3DXVECTOR2 &pos, float width, float height, float rotation) { mPos = pos; mWidth = width; mHeight = height; rotate(rotation); mCornerPos[0] = D3DXVECTOR2(pos.x, pos.y); mCornerPos[1] = D3DXVECTOR2(pos.x + width, pos.y); mCornerPos[2] = D3DXVECTOR2(pos.x + width, pos.y + height); mCornerPos[3] = D3DXVECTOR2(pos.x, pos.y + height); for (int i = 0; i < 4; i++) mVertices[i] = mCornerPos[i]; } Any ideas? I don't know why I'm having such a hard time with this, it seems like it shouldn't be this hard.
  10. Rotating a Rectangle

    I'm trying to create a custom rectangle class that allows the rectangle to rotate. I got it to rotate, but it's rotating around the origin, not its center. I've tried everything I can think of, but I'm not really good at the math involved. What do I need to do to get it to rotate around its center and not the origin? #include "CustomRectangle.h" #include "Colors.h" CustomRectangle::CustomRectangle() { mRotation = 0.0f; mCenter = D3DXVECTOR2(0.0f, 0.0f); mPos = D3DXVECTOR2(0.0f, 0.0f); mScaleX = 1.0f; mScaleY = 1.0f; } void CustomRectangle::create(float x, float y, float width, float height, float rotation) { mPos = D3DXVECTOR2(x, y); mWidth = width; mHeight = height; mRotation = rotation; mCornerPos[0] = D3DXVECTOR2(x, y); mCornerPos[1] = D3DXVECTOR2(x + width, y); mCornerPos[2] = D3DXVECTOR2(x + width, y + height); mCornerPos[3] = D3DXVECTOR2(x, y + height); for (int i = 0; i < 4; i++) mVertices[i] = mCornerPos[i]; } void CustomRectangle::create(const D3DXVECTOR2 &pos, float width, float height, float rotation) { mPos = pos; mWidth = width; mHeight = height; mRotation = rotation; mCornerPos[0] = D3DXVECTOR2(pos.x, pos.y); mCornerPos[1] = D3DXVECTOR2(pos.x + width, pos.y); mCornerPos[2] = D3DXVECTOR2(pos.x + width, pos.y + height); mCornerPos[3] = D3DXVECTOR2(pos.x, pos.y + height); for (int i = 0; i < 4; i++) mVertices[i] = mCornerPos[i]; } void CustomRectangle::rotate(float degrees) { mRotation += degrees; for (int i = 0; i < 4; i++) { mVertices[i] -= mCenter; mVertices[i].x = (mCornerPos[i].x * cosf(mRotation) - mCornerPos[i].y * sinf(mRotation)); mVertices[i].y = (mCornerPos[i].x * sinf(mRotation) + mCornerPos[i].y * cosf(mRotation)); mVertices[i] += mCenter; } } void CustomRectangle::draw(LPD3DXLINE line) { D3DXVECTOR2 rectPoints[5] = { D3DXVECTOR2(mVertices[0]), D3DXVECTOR2(mVertices[1]), D3DXVECTOR2(mVertices[2]), D3DXVECTOR2(mVertices[3]), D3DXVECTOR2(mVertices[0]) }; line->Draw(rectPoints, 5, GREEN(255)); } void CustomRectangle::updateCorners() { mCornerPos[0] = D3DXVECTOR2(mPos.x, mPos.y); mCornerPos[1] = D3DXVECTOR2(mPos.x + mWidth, mPos.y); mCornerPos[2] = D3DXVECTOR2(mPos.x + mWidth, mPos.y + mHeight); mCornerPos[3] = D3DXVECTOR2(mPos.x, mPos.y + mHeight); mCenter.x = (mCornerPos[0].x + mCornerPos[1].x) / 2; mCenter.y = (mCornerPos[0].y + mCornerPos[4].y) / 2; }
  11. Help with C output

    While I was waiting for a response I kept working and I ended up using the division operator and got it to work. Thanks for the response though. I'll check out those links just to make sure I completely understand it.
  12. Help with C output

    Hi everyone, I'm trying to learn C, and I'm working through a book and one of the exercises is to write a program that takes an integer and outputs it with a decimal before the last two digits. For example, 1523 would be 15.23. I can't seem to figure this out for some reason. I'm sure it's really easy, but I just can't see how to do it right now. How would you do this? Thanks
  13. Games with good melee combat

    Thanks for the recommendations, I'll look into those. Quote:Also, next time you ask for game recommendations, please list what platforms you have. I have a PC and Xbox 360. I denfinitely will include that info if there is a next time.
  14. Games with good melee combat

    I'm looking for some new games to play, and I'm looking for some that have good melee combat. I've been playing Dragon Age Origins recently, and it's ok, but I'd prefer something with more freedom in combat. With Dragon Age, the character really does all of the fighting, you just click once and the character practically does the rest. I'm looking for games where the character attacks with each button press (similar to the old Lord of the Rings games). Do you know of any? Thanks
  15. Windows Via C/C++ Book

    Hi everyone, Has anyone here read Windows Via C/C++? Is it any good? Thanks