Go Back   UG Community @ Ultimate-Guitar.Com > UG Community > The Pit
User Name  
Password
Search:

Reply
Old 09-27-2012, 12:55 PM   #1
VillainousLatin
That Latino Guy
 
VillainousLatin's Avatar
 
Join Date: Jul 2010
Location: The Unreal World
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.
__________________
Clicky for sexy pr0nz!
VillainousLatin is offline   Reply With Quote
Old 09-27-2012, 01:14 PM   #2
muymal
Registered User
 
Join Date: Sep 2012
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 : 09-27-2012 at 01:19 PM.
muymal is offline   Reply With Quote
Old 09-27-2012, 01:15 PM   #3
Cb4rabid
VibraTHORus Rex
 
Cb4rabid's Avatar
 
Join Date: May 2009
Location: Florida
yeah i failed pre algebra in 6th grade idk wat this is
__________________
Quote:
Originally Posted by BladeSlinger
It's people like you that make this stuff frustrating.


You don't understand this? How about now? Why don't you understand? This is so easy 4y(20x) graph it fggt

#teamLAD
Cb4rabid is offline   Reply With Quote
Old 09-27-2012, 01:31 PM   #4
vince1991
The pube in your yogurt
 
vince1991's Avatar
 
Join Date: Aug 2011
Asians...
__________________
Quote:
Originally Posted by psyks
You are filthy.
vince1991 is offline   Reply With Quote
Old 09-27-2012, 01:39 PM   #5
Smeghead64
I wanna be a boat
 
Smeghead64's Avatar
 
Join Date: Jan 2011
Location: Bangor, Wales
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
__________________
.
OSCILLATE WILDLY!
Smeghead64 is offline   Reply With Quote
Old 09-27-2012, 01:55 PM   #6
Avedas
UG's Half Asian
 
Avedas's Avatar
 
Join Date: Nov 2007
Location: Vancouver, BC
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.
Avedas is offline   Reply With Quote
Old 09-27-2012, 02:06 PM   #7
jazz_rock_feel
Micropolyphoner
 
jazz_rock_feel's Avatar
 
Join Date: Jun 2006
Location: Winnipeg, Canada
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.
__________________
I don't know what music theory is.


Soundcloud. Look at it. Or don't.
jazz_rock_feel is offline   Reply With Quote
Old 09-27-2012, 02:18 PM   #8
sashki
Look mum, no brakes!
 
Join Date: Feb 2005
Location: at home
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.
__________________
Member of UG's Gain \/\/hores - pm gpderek09 to join
sashki is offline   Reply With Quote
Old 09-27-2012, 02:25 PM   #9
jazz_rock_feel
Micropolyphoner
 
jazz_rock_feel's Avatar
 
Join Date: Jun 2006
Location: Winnipeg, Canada
^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.


Soundcloud. Look at it. Or don't.

Last edited by jazz_rock_feel : 09-27-2012 at 02:26 PM.
jazz_rock_feel is offline   Reply With Quote
Old 09-27-2012, 02:25 PM   #10
sfaune92
XYHeteroCisScum
 
Join Date: Oct 2008
Location: England/Norway
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
sfaune92 is offline   Reply With Quote
Old 09-27-2012, 02:26 PM   #11
RRRone
Registered User
 
Join Date: May 2006
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.
RRRone is offline   Reply With Quote
Old 09-27-2012, 02:27 PM   #12
jazz_rock_feel
Micropolyphoner
 
jazz_rock_feel's Avatar
 
Join Date: Jun 2006
Location: Winnipeg, Canada
^Ahh, right, I knew it was something.
__________________
I don't know what music theory is.


Soundcloud. Look at it. Or don't.
jazz_rock_feel is offline   Reply With Quote
Old 09-27-2012, 02:29 PM   #13
darkstar2466
Fly, you fools
 
darkstar2466's Avatar
 
Join Date: Mar 2007
Location: (-∞, ∞)
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:
Originally Posted by denizenz
I'll logic you right in the thyroid.

Art & Lutherie
darkstar2466 is offline   Reply With Quote
Old 09-27-2012, 02:33 PM   #14
RRRone
Registered User
 
Join Date: May 2006
Its nice to see so many programmers in the pit :
RRRone is offline   Reply With Quote
Old 09-27-2012, 06:56 PM   #15
VillainousLatin
That Latino Guy
 
VillainousLatin's Avatar
 
Join Date: Jul 2010
Location: The Unreal World
I'll a few of your ideas and then is practice, practice, practice for my exam. Thanks everyone, you're always helpful when needed.
__________________
Clicky for sexy pr0nz!
VillainousLatin is offline   Reply With Quote
Reply


Thread Tools Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -4. The time now is 09:34 AM.

Forum Archives / About / Terms of Use / Advertise / Contact / Ultimate-Guitar.Com © 2014
Powered by: vBulletin Version 3.0.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.