UG Community @ Ultimate-Guitar.Com Programming Help (C++)
 Register FAQ Members List Search Today's Posts
Search:

 09-27-2012, 12:55 PM #1 VillainousLatin UG Newbie     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!
 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.
 09-27-2012, 01:15 PM #3 Cb4rabid K♦️     Join Date: May 2009 yeah i failed pre algebra in 6th grade idk wat this is __________________ pinga
09-27-2012, 01:31 PM   #4
vince1991

Join Date: Aug 2011
Asians...
__________________
Quote:
 Originally Posted by psyks You are filthy.

09-27-2012, 01:39 PM   #5
I wanna be a boat

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!

09-27-2012, 01:55 PM   #6
Avedas
yoloswag420

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.

09-27-2012, 02:06 PM   #7
jazz_rock_feel
UG Resident

Join Date: Jun 2006
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.

 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 #include #include 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 Bass build 2014
 09-27-2012, 02:25 PM #9 jazz_rock_feel UG Resident     Join Date: Jun 2006 ^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.
 09-27-2012, 02:25 PM #10 sfaune92 Used Register   Join Date: Oct 2008 Location: NO 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
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.

 09-27-2012, 02:27 PM #12 jazz_rock_feel UG Resident     Join Date: Jun 2006 ^Ahh, right, I knew it was something. __________________ I don't know what music theory is. Soundcloud. Look at it. Or don't.
09-27-2012, 02:29 PM   #13
darkstar2466
Fly, you fools

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

 09-27-2012, 02:33 PM #14 RRRone Registered User   Join Date: May 2006 Its nice to see so many programmers in the pit :
 09-27-2012, 06:56 PM #15 VillainousLatin UG Newbie     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!

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- UG Community     New Members Q&A Forum     The Pit     Archives Of The Best Threads         Music Theory Archives         Technique Archives         Bands & Artists Archives         Electric Guitar Archives         Bass Archives         Songwriting & Lyrics Archives         Bandleading Archives         Community Archives Music     Bands & Artists         Rock & Indie         Hardcore & Punk         Blues, Jazz & Other Guitar Styles         Metal         Other     Songwriting & Lyrics         Songwriting & Lyrics Contests         Songwriting & Lyrics Techniques     Recording         Tabs & Chords         Recording Contests         Original Recordings         Cover Songs     Tab Talk     Musician Talk         Bandleading         Singing & Vocals     Promote YOUR Band     Classifieds         Musician Ads         Gear Ads Instruments     Guitar Techniques     Acoustic & Classical Guitar     Electric Guitar     Bass Guitar     Guitar Gear & Accessories     Gear Building & Customizing     Drums Misc     Ultimate Guitar Feedback         Site Suggestions         Mobile Apps Feedback         Tab Pro Service Feedback         Bug Reports     UG Contribution

All times are GMT -4. The time now is 09:03 PM.

 -- UG Black Style -- UG Classic Style
Forum Archives / About / TOS / Advertise with us / Customer Support / Ultimate-Guitar.Com © 2016