VillainousLatin
UG Newbie
Join date: Jul 2010
317 IQ
So I have this assignment for Intro to Programming class where I need to make something like this:

x z y
---- ---- ---------
1 2 -2.000
1 3 -1.500
1 4 -1.333
1 5 -1.250
1 6 -1.200
2 2 Undefined
2 3 -6.000
2 4 -4.000
.
.
.

Where x goes from 1 - 5 and z goes from 2 - 6 and y = ((x*z)/(x-z)).

Now, I have to use nested for-loops and the x variable must control the outer loop and the z variable must control the inner loop because the assignment specifically ask for it. To be honest this is the first assignment I don' t even know where to start and I don't really have anyone to ask for help, so I thought of you guys since I know there are some people here that knows about programming. Now, I know the pit frowns upon people trying to make The Pit do their homework but that's not what I' m doing here, I just need some pointers and tips on how I should start or view the problem.

And yes, I know this problem should be easy, I've done some harder ones I just think that I grasp the concept of for loops well enough or something.

P.S. I' m going to class now so if I won' t be able to replay for like 2 hours, also, I know there is a thread somewhere about programming but I couldn't find it, so if someone finds it or something give me a link and I' ll ask there.
muymal
Registered User
Join date: Sep 2012
10 IQ
i haven't done c++ for a while but in general this is what you need

for (x = 1; x < (maximum x-range of number)+1; x++)
for (z = 2: z < (maximum z-range of number)+1; z++)

y = equation;
cout << y

end
end

you can ignore the +1 if you use <= and remember to declare x and z before hand or inside.

What this does is the 1st for loop starts, it initializes your x as 1, then goes inside the 2nd for loop. The 2nd for loop is going to calculate all the y for the x=1 z = 2-6 case. After, it exits and goes back outside to the first loop, x is going to increment x++ = x+1. Then step inside the 2nd for loop again and does x = 2 and z = 2-6. rinse and repeats. you can format it to output the result in a table format

edit - i joined just to answer this question ahhahhaha
edit2- you have to get really good at using these conditional statements so study up on them. they are very simple and useful once you get the hang of it.
Last edited by muymal at Sep 27, 2012,
Cb4rabid
K♦️
Join date: May 2009
2,628 IQ
yeah i failed pre algebra in 6th grade idk wat this is
pinga
vince1991
Join date: Aug 2011
84 IQ
Asians...
Quote by psyks
You are filthy.
I wanna be a boat
Join date: Jan 2011
10 IQ
Quote by muymal
i haven't done c++ for a while but in general this is what you need

for (x = 1; x < (maximum x-range of number)+1; x++)
for (z = 2: z < (maximum z-range of number)+1; z++)

y = equation;
cout << y

end
end

you can ignore the +1 if you use <= and remember to declare x and z before hand or inside.

What this does is the 1st for loop starts, it initializes your x as 1, then goes inside the 2nd for loop. The 2nd for loop is going to calculate all the y for the x=1 z = 2-6 case. After, it exits and goes back outside to the first loop, x is going to increment x++ = x+1. Then step inside the 2nd for loop again and does x = 2 and z = 2-6. rinse and repeats. you can format it to output the result in a table format

edit - i joined just to answer this question ahhahhaha
edit2- you have to get really good at using these conditional statements so study up on them. they are very simple and useful once you get the hang of it.

This seems good to me expect wouldn't there need to be a if-statement to to check if x and z are then same before the equation is executed so it doesn't divide by zero.
if (x != z)
y = equation
.
OSCILLATE WILDLY!
Avedas
yoloswag420
Join date: Nov 2007
673 IQ
Quote by Cb4rabid
yeah i failed pre algebra in 6th grade idk wat this is

Hah, loser.

TS, I'd help you but the last thing I want to do is coding from my phone.
jazz_rock_feel
UG Resident
Join date: Jun 2006
2,342 IQ
This seems good to me expect wouldn't there need to be a if-statement to to check if x and z are then same before the equation is executed so it doesn't divide by zero.
if (x != z)
y = equation

Yeah, you would.

I don't know C++, but semi accurate psudo-code would be:

``````for(int x = 1; x <=5; x++)
{
for(int z = 2; z <= 6; z++)
{
print(x);
print(z);
if(x != z)
print((x * z) / (x - z));
else
print("Undefined");
}
}``````

Plus however you want to format it.
I don't know what music theory is.

Join date: Feb 2005
6,586 IQ
I haven't done any programmzorz in a while but this worked for me
``````#include<stdio.h>
#include<stdlib.h>
#include <iostream.h>

main()
{
float x=1;	//define variables
float z=2;
float y;

printf("X \t Z \t Y");

for (x=1;x<=5;x++)	//loop for all x values
{
for (z=2;z<=6;z++)	//loopception for all z values
{
if((x-z)!=0)		//prevents divide by zero errors
{
y=((x*z)/(x-z));	//calculates that shizzle
printf("\n %.0f \t %.0f \t %.2f",x,z,y);	//prints results in a new line
}
else
{
printf("\n %.0f \t %.0f \t Undefined",x,z);	//prints "undefined" if x-z is zero
}
}
}

return 0;	//I dunno what this does but I put it in anyway
}``````

One thing I noticed: If you set X and Z to be integers, then Y will always give integer values, which are rounded off and therefore incorrect. I'm not sure if making them floats is the most efficient way of doing things, but it solves the problem.
jazz_rock_feel
UG Resident
Join date: Jun 2006
2,342 IQ
^In C/C++ the main function is declared to return an int value and when the main function returns 0 it means that the program executed normally.

Sometimes you need it, sometimes you don't, I think it depends on the compiler.

^Oh, and actually it integer division doesn't round it truncates the decimal, so 3/2 is 1 instead of 1.5. Declaring variables as floats solves that problem. There's probably also a way to force it to do float division, but I don't really know C++.
I don't know what music theory is.

Last edited by jazz_rock_feel at Sep 27, 2012,
sfaune92
Used Register
Join date: Oct 2008
616 IQ
The return 0; basically is there to tell the OS that the program ran as it was supposed to.

Edit: Read the oost above. They explained it better
RRRone
Registered User
Join date: May 2006
59 IQ
Quote by jazz_rock_feel
^In C/C++ the main function is declared to return an int value and when the main function returns 0 it means that the program executed normally.

Sometimes you need it, sometimes you don't, I think it depends on the compiler.

The way sashki did it is the old C way of doing it.
jazz_rock_feel
UG Resident
Join date: Jun 2006
2,342 IQ
^Ahh, right, I knew it was something.
I don't know what music theory is.

darkstar2466
Fly, you fools
Join date: Mar 2007
1,145 IQ
ANSI standard requires main() to return an int because that's what a lot of programmers were doing without the standard anyway. Some programs return, and some run infinitely, and some programs that launch other programs that terminate depend on the exit status of the program that was launched, etc.

Current standard does require main to return an int though.
Quote by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
RRRone
Registered User
Join date: May 2006
59 IQ
Its nice to see so many programmers in the pit :
VillainousLatin
UG Newbie
Join date: Jul 2010
317 IQ
I'll a few of your ideas and then is practice, practice, practice for my exam. Thanks everyone, you're always helpful when needed.