Sign in to follow this  
frankiej

Explanation

Recommended Posts

Hi Everyone Im still quite new at programming and was wondering if someone can explain why int X & Y are used when coding. I have read a few books on these but just cant get my head around it. Many Thanks

Share this post


Link to post
Share on other sites
Varibles do what you want them to do, simply put. You could declare your int variables RUTABEGGA and NANTUCKET for all it's worth to the program. I think introductory programming gets you used to them because a)they're short to type, b)they're easy to remember, and c)because that's what's been done for so many years.

I'm not sure why X and Y became such popular names for variables in mathematics, but since they are used so much, it's just natural for people to name simple programming variables X and Y as well.

In more complex, specific situations, X and Y are very often used to denote spatial co-ordinates. You can position something on a grid using X and Y as axes. This is a convention almost everybody uses when programming.

Hope you find this helpful
As ever,
**Cosmic**

Share this post


Link to post
Share on other sites
An int (integer variable) is a location in system memory that you can use to store an integer value, i.e. any "whole" number. You can name a variable anything you want (except that there are limitations on the size of a name and there are certain characters that can not be included). You have to declare a variable so that the compiler knows what it is and what type it is. In C you most declare a variable at the beginning of the scope that it is used in. In C++ you can declare it anywere in the scope before it is used. THe code
 int X,Y; 
declares two integer variables one named X and the other named Y.

Share this post


Link to post
Share on other sites
They are variables yes. As Cosmic One said, X and Y are typically used to represent spatial co-ordinates. In geometry, 'x' is often used to represent the width or horizontal position of an entity, 'y' represents the height or vertical element of an entity and 'z' represents 'depth'.

So, in game programming we will often use x and y to represent the 2d position of an object in worldspace, where 'x' denotes how far 'across' the screen (or world) a point is and 'y' denotes how far 'up' it lies.

Note that this isn't always the case, but it is the common way of doing things. As Cosmic One said, you could choose to call these variables anything - but x,y and z are often used because their meaning is a globally understood.

Share this post


Link to post
Share on other sites
Hi I have an example from a book that Im reading here is the problem Im not understanding how int x, int y work????

#include <iostream>

int Add (int x, int y)
{
std::cout << "In Add(), received "<< x << " and " << Y << "\n";
Return (x+y);

}

int main()
{
std::cout << "I'm in main()!\n";
std::cout << "\nCalling Add()!\n";
std::cout << "The value returned is: " << Add(3,4);
std::cout << "\nBack in main().\n";
std::cout << "\nExiting...\n\n";
return 0;
}

Share this post


Link to post
Share on other sites
main is passing the parameter 3 and 4 to the function add, and is temporarily calling them x and y. It then adds them and prints out the result. X and Y aren't coordinates just variable names that could be anything.

****Im assuming the capital Y should be y****

Share this post


Link to post
Share on other sites
think in this way.computer is a really stupid pile of shit.if you want it to remember to something,you will have to tell him every single detail. Type int x will be equvilent to say "I want you to remember a integer from now one called x." and type int x = 1 is same as "I want you to remember a integer called x which is equal to 1". similarly int y will be a integer called y, int y = 1 is a integer called y which is 1.

in case of your example program, that means you have a function which takes 2 integer nameingly x and y. and output the value of x+y.

hope this helps.

Share this post


Link to post
Share on other sites
#include <iostream>

using namespace std;

int main()
{
int x = 0, y = 4;
int thisIsAlsoAnIntVariable = 10;
y = y * 8;

cout << "The number stored in x is " << x << ".\n";
cout << "On a 32 bit system, an int is " << y << " bits.\n";
cout << "An int can be any name you want (following some rules of course).\n"
<< "Such as thisIsAlsoAnIntVariable = " << thisIsAlsoAnIntVariable << endl;

return 0;
}


Without using a function, you can see that an int is just a place to store a number. You can use it for math (y = y * 8), and ask the computer to show you what it is. The name is your choice, but usually long names are not used because they are slow to type and remember, easier to screw up typing, and make the code look messier.

Note: the 'int' before main just tells what type of value my function will return. This corresponds with 'return 0', which is saying the int that it is returning is now 0.

Share this post


Link to post
Share on other sites
I think what's confusing you is that you're trying to think of it from a math perspective. X and Y represent something hidden, and I'm trying to figure it out. Here's really what's going on:
int x, y; x and y are blank spaces in memory. If you're using a 32-bit CPU, then they're probably 32 bits each. You've just allocated 64 bits of memory. Now, you store data in that memory; you can only store integers. So X = 4; is storing the value 4 in those 32-bits. It's just a way to represent a number.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this