UG Community @ Ultimate-Guitar.Com (https://www.ultimate-guitar.com/forum/index.php)
-   The Pit (https://www.ultimate-guitar.com/forum/forumdisplay.php?f=11)
-   -   Programming Help (C++) (https://www.ultimate-guitar.com/forum/showthread.php?t=1565124)

 VillainousLatin 09-27-2012 12:55 PM

Programming Help (C++)

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 09-27-2012 01:14 PM

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.

 Cb4rabid 09-27-2012 01:15 PM

yeah i failed pre algebra in 6th grade idk wat this is

 vince1991 09-27-2012 01:31 PM

Asians...

Quote:
 Originally Posted 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

 Avedas 09-27-2012 01:55 PM

Quote:
 Originally Posted 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 09-27-2012 02:06 PM

Quote:
 Originally Posted by Smeghead64 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:

Code:
```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.

 sashki 09-27-2012 02:18 PM

I haven't done any programmzorz in a while but this worked for me
Code:
```#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 09-27-2012 02:25 PM

^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++.

 sfaune92 09-27-2012 02:25 PM

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 09-27-2012 02:26 PM

Quote:
 Originally Posted 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 09-27-2012 02:27 PM

^Ahh, right, I knew it was something.

 darkstar2466 09-27-2012 02:29 PM

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.

 RRRone 09-27-2012 02:33 PM

Its nice to see so many programmers in the pit :D:

 VillainousLatin 09-27-2012 06:56 PM

I'll a few of your ideas and then is practice, practice, practice for my exam. Thanks everyone, you're always helpful when needed.

 All times are GMT -4. The time now is 07:51 AM.