[QUOTE=Whitefox08;33553237]why does rand always start with 41 in c++ :v:
[code]#include<iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int const cap = 10;
int array1[cap];
for(int i = 0; i < 10; i++)
{
array1[i] = rand()%100;
cout<<array1[i]<<", ";
}
cin.get();// KEEP CMD OPEN FOR MS VISUAL EXPRESS BECAUSE THERE RETARDED, and is also better than system("pause").
return 0;
}
[/code]
[code]41, 67, 34, 0, 69, 24, 78, 58, 62, 64[/code][/QUOTE]
You have to seen the random number generator with some number. You do that with srand(..). To make sure the seed is different every time you can use the current time. So in the beginning of main() you do 'srand(time(0))'. Make sure to include ctime too.
[QUOTE=sim642;33553376]You have to seen the random number generator with some number. You do that with srand(..). To make sure the seed is different every time you can use the current time. So in the beginning of main() you do 'srand(time(0))'. Make sure to include ctime too.[/QUOTE]
thanks :D
[url]http://www.facepunch.com/threads/1092954?p=33552913&viewfull=1#post33552913[/url]
Can anyone help me with this?
Guys, how the hell do I do function in C?
It's [code]int function(int par1, int par2 ) {
x = par1 + par2;
return x;}
So, if I wanted to call this function, it'd be something like this, right?
int main (VOID) {
int num1;
scanf (%d, num1);
int num2
scanf(%d, num2);
printf("%d", function(num1, num2);
return 0;}
[/code]
And it'd just replace par 1 and par2 with num1 and num 2, do the adition and print out x, right?
Why the hell is my function giving out all sorts of crazy erros like
prog.c:103: error: ‘anoNas1’ undeclared (first use in this function)
prog.c:123: error: ‘mesAct1’ undeclared (first use in this function)
prog.c:125: error: ‘diaMes1’ undeclared (first use in this function)
prog.c:140: error: expected expression before ‘/’ token
prog.c:142: error: ‘mesNas1’ undeclared (first use in this function)
prog.c:159: error: redefinition of ‘dias’
prog.c:99: error: previous definition of ‘dias’ was here
prog.c:118: warning: unused variable ‘diaMes’
First, wrap your code in [ code][ /code] tags.
Secondly, you should have an "int " in front of "function(int par1,...".
Thirdly, what line numbers are 103, 123 etc?
[QUOTE=MountainWatcher;33553713]etc[/QUOTE]
First, the second and all additional parameters to scanf need to be references. Second, your printf statement is missing a parenthesis. Third, x is never defined in function. Fourth, function has no return type. Fifth, you're missing a semicolon on declaring num2. Sixth, the first parameter to scanf needs to be a string.
[code]int function(int par1, int par2 ) {
int x = par1 + par2;
return x;}
int main (VOID) {
int num1;
scanf ("%d", &num1);
int num2;
scanf("%d", &num2);
printf("%d", function(num1, num2));
return 0;}[/code]
Oh, that I just ripped right from the code that's giving me the issues. And good eye, I'll change both on the post.
And do you guys want the actual code? Or just the function?
[QUOTE=MountainWatcher;33553713]Guys, how the hell do I do function in C?
It's function(int par1, int par2 ) {
x = par1 + par2;
return x;}
So, if I wanted to call this function, it'd be something like this, right?
int main (VOID) {
int num1;
scanf (%d, num1);
int num2
scanf(%d, num2);
printf("%d", function(num1, num2);
return 0;}
And it'd just replace par 1 and par2 with num1 and num 2, do the adition and print out x, right?
Why the hell is my function giving out all sorts of crazy erros like
prog.c:103: error: ‘anoNas1’ undeclared (first use in this function)
prog.c:123: error: ‘mesAct1’ undeclared (first use in this function)
prog.c:125: error: ‘diaMes1’ undeclared (first use in this function)
prog.c:140: error: expected expression before ‘/’ token
prog.c:142: error: ‘mesNas1’ undeclared (first use in this function)
prog.c:159: error: redefinition of ‘dias’
prog.c:99: error: previous definition of ‘dias’ was here
prog.c:118: warning: unused variable ‘diaMes’[/QUOTE]
Are you sure you're compiling the right file? Those line numbers look way to high if that's all your program is.
Also you should use code tags:
[code]
int function(int par1, int par2 )
{
return par1 + par2;
}
int main(void)
{
int num1;
scanf (%d, &num1);
int num2;
scanf(%d, &num2);
printf("%d", function(num1, num2));
return 0;
}[/code]
I'm a beginner and I haven't been paying too much attention in class, so I apologize if I'm committing obvious mistakes.
Can't I involve x in a calculation right away? Do I have to do
[code]
int x;
int x = a + b;
[/code]
and not just
[code]
int x = a + b;
[/code]
[QUOTE=MountainWatcher;33553972]I'm a beginner and I haven't been paying too much attention in class, so I apologize if I'm committing obvious mistakes.
Can't I involve x in a calculation right away? Do I have to do
[code]
int x;
int x = a + b;
[/code]
and not just
[code]
int x = a + b;
[/code][/QUOTE]
Both are valid.
I have another question.
If I have a function 1 with parameters a and b and if I use the characters a and b as variables in a function 2 that calls out function 1, will that be a problem?
[QUOTE=MountainWatcher;33553972]I'm a beginner and I haven't been paying too much attention in class, so I apologize if I'm committing obvious mistakes.
Can't I involve x in a calculation right away? Do I have to do
[code]
int x;
int x = a + b;
[/code]
and not just
[code]
int x = a + b;
[/code][/QUOTE]
I assume you mean
[code]
int x;
x = a + b;
[/code]
Both result in the same thing.
[QUOTE=MountainWatcher;33554106]I have another question.
If I have a function 1 with parameters a and b and if I use the characters a and b as variables in a function 2 that calls out function 1, will that be a problem?[/QUOTE]
Compiler should not allow you to conflict parameter and local variable names.
Right, right, shouln't have added an int.
So, it'll be a compilation error? Got it, I'll just add a 1 to every parameter :v:
Also, have you guys heard of this website? it's pretty nifty. [url]http://ideone.com/[/url]
I think I my have found out I was fucking up, I forgot a "&" in a printf and I was using "int"'s on the parameter values :v:
[editline]3rd December 2011[/editline]
Shit, that ain't it.
[editline]3rd December 2011[/editline]
prog.c:70: error: lvalue required as unary ‘&’ operand
Hey guys, what does this error means?
Also, I forgot some commentary tags around some code, so, yeah that fucked some shit up :v:
[editline]3rd December 2011[/editline]
wait, guys, if I declare x without a a starting value and I do x = x +1, is it going to fuck up?
[QUOTE=MountainWatcher;33554330]I think I my have found out I was fucking up, I forgot a "&" in a printf and I was using "int"'s on the parameter values :v:[/QUOTE]
You don't pass integers to printf by reference. You pass the integer itself. It's designed this way because an int is only one word (fits in a register or on the stack easily) and printf doesn't need to modify it.
Pass-by-reference is only done if you're passing a large structure or if the function needs to be able to modify the original value (as in the case of scanf).
[QUOTE=MountainWatcher;33554330]prog.c:70: error: lvalue required as unary ‘&’ operand
Hey guys, what does this error means?[/QUOTE]
Post the entire line.
[QUOTE=MountainWatcher;33554330]wait, guys, if I declare x without a a starting value and I do x = x +1, is it going to fuck up?[/QUOTE]
Yes.
printf("%d", &numdias( diaNas, mesNas, anoNas, mesAct, anoAct ));
[editline]3rd December 2011[/editline]
I'm retarded. :v:
[editline]3rd December 2011[/editline]
I was using a variable called day, then added x to dayS :v:
You're still passing integers by reference. Worse yet, you're referencing a return value without storing it anywhere. I have no idea what the computer even [i]does[/i] in that case. It's probably undefined behavior.
I'm having some serious problems with my collision detection code:
[CODE] float distance = Vector2.Distance(zombies[i].center, zombies[j].center);
Vector2 dir = zombies[i].center - zombies[j].center;
float overlap = zombies[i].texture.Width - distance;
dir.Normalize();
zombies[i].pos += dir * overlap;
zombies[j].pos -= dir * overlap;
[/CODE]
When the blocks collide they just disappear...
I've tried and tried, where am I going wrong ?
[QUOTE=joyenusi;33555187]I'm having some serious problems with my collision detection code:
[CODE] float distance = Vector2.Distance(zombies[i].center, zombies[j].center);
Vector2 dir = zombies[i].center - zombies[j].center;
float overlap = zombies[i].texture.Width - distance;
dir.Normalize();
zombies[i].pos += dir * overlap;
zombies[j].pos -= dir * overlap;
[/CODE]
When the blocks collide they just disappear...
I've tried and tried, where am I going wrong ?[/QUOTE]
Do a breakpoint and go through your code, make sure there are no unexpected values.
I think it may have something to do with the "overlap" variable. Since you're using the center position of the zombies, try using half-width instead of full width:
[cpp]float overlap = (zombies[i].texture.Width / 2.0) - distance;[/cpp]
I'd get rid of 'overlap' entirely.
[cpp]
float distance = Vector2.Distance(zombies[i].center, zombies[j].center);
Vector2 dir = zombies[i].center - zombies[j].center;
dir.Normalize();
float radius1 = zombies[i].texture.Width / 2.0f;
float radius2 = zombies[j].texture.Width / 2.0f;
if (distance < radius1 + radius2) {
Vector2 point = (zombies[i].center + zombies[j].center) / 2.0f;
zombies[i].pos = point + dir * radius1;
zombies[j].pos = point - dir * radius2;
}
[/cpp]
usr/lib/gcc/i686-pc-linux-gnu/4.3.4/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
Oh sweet jesus, what the fuck. :suicide:
I tried to run my code, it said it was illegal behavior. Guys, am I a leet hacker now?
Mate, the only concept I fully grasp in that sentence is "integer". I'm sorry, could you explain that in a more simplistic fashion?
Are you saying when I say return (x), I have to define x? I did, I had a x = q + w + e + r with those 4 terms properly coded, at least the compiler let it pass.
[QUOTE=ROBO_DONUT;33555342]I'd get rid of 'overlap' entirely.
[CODE]
float distance = Vector2.Distance(zombies[i].center, zombies[j].center);
Vector2 dir = zombies[i].center - zombies[j].center;
dir.Normalize();
float radius1 = zombies[i].texture.Width / 2.0f;
float radius2 = zombies[j].texture.Width / 2.0f;
if (distance < radius1 + radius2) {
Vector2 point = (zombies[i].center + zombies[j].center) / 2.0f;
zombies[i].pos = point + dir * radius1;
zombies[j].pos = point - dir * radius2;
}
[/CODE][/QUOTE]
They still dissappear off-screen.
[QUOTE=joyenusi;33555591]They still dissappear off-screen.[/QUOTE]
Post any relevant code and have it print out all the information during this routine (the value of i, j, zombies[i] and zombies[j], distance, dir, radius, point, and the positions before and after).
You're checking to make sure that i != j, right? Because having a zombie collide with itself could cause something like this.
[QUOTE=ROBO_DONUT;33555631]Post any relevant code and have it print out all the information during this routine (the value of i, j, zombies[i] and zombies[j], distance, dir, radius, point, and the positions before and after).
You're checking to make sure that i != j, right? Because having a zombie collide with itself could cause something like this.[/QUOTE]
[CODE]
public void Update(Vector2 PlayerPos)
{
playerPos = PlayerPos;
for (int i = 0; i < zombies.Count; i++)
{
for (int j = 0; j < zombies.Count; j++)
{
if (zombies[i] == zombies[j])
{
continue;
}
if (zombies[i].pos.X + zombies[i].texture.Width < zombies[j].pos.X)
{
continue;
}
if (zombies[i].pos.X > zombies[j].pos.X + zombies[j].texture.Width)
{
continue;
}
if (zombies[i].pos.Y + zombies[i].texture.Height < zombies[j].pos.Y)
{
continue;
}
if (zombies[i].pos.Y > zombies[j].pos.Y + zombies[j].texture.Height)
{
continue;
}
else
{
Console.WriteLine("i = " + i + ", zombies [i] = " + zombies[i] +
", j = "+ j + ", zombies[j] = " + zombies[j]);
float distance = Vector2.Distance(zombies[i].center, zombies[j].center);
Console.WriteLine("distance = " + distance);
Vector2 dir = zombies[i].center - zombies[j].center;
Console.WriteLine("dir = " + dir);
dir.Normalize();
Console.WriteLine("after dir.Normalize() dir = " + dir);
float radius1 = zombies[i].texture.Width / 2.0f;
Console.WriteLine("radius 1 = " + radius1);
float radius2 = zombies[j].texture.Width / 2.0f;
Console.WriteLine("radius 2 = " + radius2);
if (distance < radius1 + radius2)
{
Vector2 point = (zombies[i].center + zombies[j].center) / 2.0f;
Console.WriteLine("point = " + point);
Console.WriteLine("Zombies position before:" + "\n" + "[i].pos = " +
zombies[i].pos + "\n" + "[j].pos = " + zombies[j].pos);
zombies[i].pos = point + dir * radius1;
zombies[j].pos = point - dir * radius2;
Console.WriteLine("Zombies position after:" + "\n" + "[i].pos = " +
zombies[i].pos + "\n" + "[j].pos = " + zombies[j].pos);
}
}
}
}
[/CODE]
Also, within the zombie class...:
[CODE]
centerX = (int)pos.X + texture.Width / 2;
centerY = (int)pos.Y + texture.Height / 2;
center = new Vector2(centerX, centerY);
[/CODE]
Output:
[CODE]
i = 0, zombies [i] = ZombieSurvival.Zombie, j = 1, zombies[j] = ZombieSurvival.Zombie
distance = 0
dir = {X:0 Y:0}
after dir.Normalize() dir = {X:NaN Y:NaN}
radius 1 = 10
radius 2 = 10
point = {X:110 Y:110}
Zombies position before:
[i].pos = {X:50.50247 Y:50.50247}
[j].pos = {X:69.59438 Y:69.59438}
Zombies position after:
[i].pos = {X:NaN Y:NaN}
[j].pos = {X:NaN Y:NaN}
[/CODE]
I think the process goes wrong around dir ?
EDIT:
[QUOTE]You're checking to make sure that i != j, right? Because having a zombie collide with itself could cause something like this.[/QUOTE]
Yeah, [CODE]if(zombies[i] == zombies[j]){continue;}[/CODE]
[cpp]Vector2 dir = zombies[i].center - zombies[j].center;[/cpp]
By the looks of it, both of the centers are the same value.
I'm thinking that both centers have the same {X, Y} coords.
You can't normalize {0, 0}.
Lol, I think I've realised now, I had center initialized but never updated by the looks of it.
Okay, now they don't disappear, just push each other off-screen. Making progress, I'll try at it from here.
Also... How do I get my avatar to change ? I uploaded a new one yesterday but it's still not updated ?
You have two zombies with the [i]exact same[/i] position.
So distance is zero, and dir is < 0/0, 0/0 >.
If distance is less than epsilon, some very small value like 0.000001, then you have to use an alternative collision handling routine.
[code]
if (distance < 0.00001) {
zombies[i].pos.x -= radius1;
zombies[j].pos.x += radius2;
}
[/code]
Another update...
[media]http://www.youtube.com/watch?v=FpNENWf6ZU4[/media]
I got it building in release mode so now everyone can share the love!
[QUOTE=MountainWatcher;33555392]usr/lib/gcc/i686-pc-linux-gnu/4.3.4/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
Oh sweet jesus, what the fuck. :suicide:
I tried to run my code, it said it was illegal behavior. Guys, am I a leet hacker now?
Mate, the only concept I fully grasp in that sentence is "integer". I'm sorry, could you explain that in a more simplistic fashion?
Are you saying when I say return (x), I have to define x? I did, I had a x = q + w + e + r with those 4 terms properly coded, at least the compiler let it pass.[/QUOTE]
Post your code.
When you say return x, yes, x has to be defined, and it has to have a type.
And what he meant by saying you're still passing integers by reference is get rid of the '&' in front of your parameters to printf(), it's not necessary. You're passing the address of the integers (a number that represents a location in memory) when you should just be passing the integer variable itself.
Alright, so I guess every program needs a main function, I did not know that.
[editline]3rd December 2011[/editline]
that's what was fucking it up.
[editline]3rd December 2011[/editline]
Also if I wish to call a function without parameters I can just go in the style of
x = function(void);
right?
Also, I'm getting these warnings : warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
Do they mean I'm not working with the values the scanf is giving the code?
[QUOTE=MountainWatcher;33556641]Alright, so I guess every program needs a main function, I did not know that.[/quote]
If it's meant to be executed as a complete program, it needs an entry point. Most of the time this will be main(). Libraries don't need main().
[QUOTE=MountainWatcher;33556641]Also if I wish to call a function without parameters I can just go in the style of
x = function(void);
right?[/quote]
Maybe for function definitions, not for function calls.
[QUOTE=MountainWatcher;33556641]Also, I'm getting these warnings : warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
Do they mean I'm not working with the values the scanf is giving the code?[/QUOTE]
You're ignoring the return code it gives. It's telling you that you're not checking whether the function actually [i]succeeded[/i].
Sorry, you need to Log In to post a reply to this thread.