# Maximum PC

 It is currently Sat Aug 23, 2014 5:41 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 18 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Exponentiation program, C++Posted: Fri Apr 17, 2009 2:24 pm
 8086

Joined: Sat Dec 27, 2008 7:17 pm
Posts: 28
Ok, I am "trying" to create a C++ program to determine exponentiation. I cannot use the pow function, and must use for loops.

/******************************************************/
/* Exponentiation.cpp */
/* */
/* Created by: Larry R Newell */
/* Date: 12 April 2009 */
/* */
/* Program to compute the x ^ y */
/* */
/* Inputs: (keyboard) */
/* 1. One integer exponent (y), y >= 0 */
/* 2. One float number (x) */
/* */
/* Output: */
/* Print x^y as a float, on the screen */
/* */
/* Algorithm: set a variable equal to 1, then multiply*/
/* that variable by x, y times */
/* */
/******************************************************/

#include <iostream>

using namespace std ;
int main ()
{
int y ;
float x ;
float result ;

// read in the two numbers

cout << endl ;

cout << "Enter number to be raised to power(float) :"
cin >> x ;

cout << "Enter the exponent (non-negative integer) : " ;
cin >> y ;

Int y = 0;

for (y > 0)
for (int i = 1; i <= y; i++)
result = result * x;

}

// display the result

cout << endl ;
cout << "The value of" << x << " to power " << y
<< " is : " << result << endl ;

return (0); // terminate with success
}

That is what I have right now. I have read the chapters and searched the internet to try and grasp the concept that I am still obviously missing. I am doing something (or many things) wrong with the for-loop initializations. If someone could please provide some guidance (not asking for answers or exact code, but maybe help understanding. I am aware of the problem and how to get the solution, I am just confused on how to write the for-loops. Any help is much appreciated.

Top

 Post subject: Posted: Fri Apr 17, 2009 2:41 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
I suggest writing it out using pseudo code and seeing if it logically sounds right.

Looking at your for loop area, I would translate it this way:

Y = 0

for all situations where Y is greater than 0, enter the loop (does this tell you anything?).

For i = 1 to Y, step through the loop where i<=Y.

result = result * x; (this looks fairly correct, but it's been a while since I have dealt with C. What is the beginning value of result? You don't set it anywhere, and I forget if C defaults it to a value or not.)

} end of the internal for loop, go back to the top of it and increment i by 1 .

Display the result
return (0); // terminate with success
} end of the first loop

Top

 Post subject: Posted: Fri Apr 17, 2009 2:56 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
Further info on loops.

Something to remember about programming. There's usually no single answer/solution to a problem. It's just that some ways are more efficient than others. Smart programmers tend to be "lazy" programmers in that they don't repeat code if they don't need to.

Loops are used to repeat processes multiple times and not repeat code. You may or may not know more than that you need to repeat the code block more than once, however, you don't use them if you know for sure you are executing a block of code only once.

In your exponentiation example, let's say you have fixed numbers (as a sample), and you want to get the value 5 to the exponent of 10.

If you iterated through it the non-efficient way, you'd do code like:
Result = 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5
Result = Result * 5

If you did it the lazy way, you'd code like
Result = 5
for (i =1, i<=10, i++)
Result = Result * 5
}

So, you would go from 11 lines of code (mostly doing the same thing) to 4 lines.

Loop #1, it looks like the intent you are saying "Execute the following code only if the exponentiation value is greater than 0".
Loop #2, it looks like the intent you are saying is "Repeat the following code until i = Y".

When it is spelled out like that, does it help in figuring out which one is a correct idea for a loop?

Top

 Post subject: Posted: Fri Apr 17, 2009 3:00 pm
 8086

Joined: Sat Dec 27, 2008 7:17 pm
Posts: 28
One thing I do not understand is where does the i come from? I never declare i as a variable, yet in every example that I see regarding exponent programs, I see

for (int i = 1; i <= y; i++)

Also you mentioned to increment i by 1, isn't that the purpose of i++ ?

Thanks for your time and help.

Top

 Post subject: Posted: Fri Apr 17, 2009 3:10 pm
 8086

Joined: Sat Dec 27, 2008 7:17 pm
Posts: 28
#include <iostream>

using namespace std ;
int main ()
{
int y ;
float x ;
float result ;

// read in the two numbers

cout << endl ;

cout << "Enter number to be raised to power(float) :"
cin >> x ;

cout << "Enter the exponent (non-negative integer) : " ;
cin >> y ;

Int y = 0;

{
for (int i = 1; i <= y; i++)
result = result * x;

}

// display the result

cout << endl ;
cout << "The value of" << x << " to power " << y
<< " is : " << result << endl ;

return (0); // terminate with success
}

I changed some settings after reading your posts and have definitely made some progress. I am now receiving 3 errors which aren't really making sense to me. 2 errors say I am missing ; however I don't believe I am. Also it has int as an undeclared identifier , however I thought int was declaring the variable as an integer, so I'm not sure how that is undeclared?

1>d:\my c++ projects\module3exercise3\module3exercise3\exponentiation.cpp(36) : error C2146: syntax error : missing ';' before identifier 'cin'

1>d:\my c++ projects\module3exercise3\module3exercise3\exponentiation.cpp(41) : error C2065: 'Int' : undeclared identifier

1>d:\my c++ projects\module3exercise3\module3exercise3\exponentiation.cpp(41) : error C2146: syntax error : missing ';' before identifier 'y'

Top

 Post subject: Posted: Sat Apr 18, 2009 1:31 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
larry_ray30 wrote:
One thing I do not understand is where does the i come from? I never declare i as a variable, yet in every example that I see regarding exponent programs, I see

for (int i = 1; i <= y; i++)

Also you mentioned to increment i by 1, isn't that the purpose of i++ ?

Thanks for your time and help.

You can define i in different places. In the for, it's defined inline (the "int i" part). You can also write that line as:

int i;
for (i=1;i<=y;i++)

Different people do it different ways, personally, I define everything at the top of the function/procedure, all in one area. Others define them near the point they are first used (or when they are first used). Personally, I find it easier to track things down when I know where they are all supposed to be (you can do a Search/Find, but meh).

In re. the increment by 1, yes, that corresponds to i++ in this particular example. However, remember, we're doing pseudocode. You're writing out the logic/thought process, not necessarily the code. Depending on the project needs/algorithm, you may be incrementing by more than 1, or you may be going down (stepping backwards in an array for example).

Top

 Post subject: Posted: Sat Apr 18, 2009 1:55 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
larry_ray30 wrote:
I changed some settings after reading your posts and have definitely made some progress. I am now receiving 3 errors which aren't really making sense to me. 2 errors say I am missing ; however I don't believe I am. Also it has int as an undeclared identifier , however I thought int was declaring the variable as an integer, so I'm not sure how that is undeclared?

1>d:\my c++ projects\module3exercise3\module3exercise3\exponentiation.cpp(36) : error C2146: syntax error : missing ';' before identifier 'cin'

1>d:\my c++ projects\module3exercise3\module3exercise3\exponentiation.cpp(41) : error C2065: 'Int' : undeclared identifier

1>d:\my c++ projects\module3exercise3\module3exercise3\exponentiation.cpp(41) : error C2146: syntax error : missing ';' before identifier 'y'

Welcome to the joys of debugging. Something you'll find as you get into programming is that often, you won't see the simplest/stupidest errors unless you walk away for a bit, then come back to it again. Especially syntax errors. You'll also find that sometimes the error's not happening on the line that it says it is, or that it isn't really related to what it says it is.

In re. the ";" errors. First, remember that you pretty much need to finish off every command in C++ with a ";". If you see it saying that it is missing a ";", you want to look around that line to see if there's any commands missing a ";" at the end.

In the error on line 36, look around that area to see if there's a missing ";".

In the error on line 41, this is an example where the error's not really necessarily related to a missing ";". You have to look at the line to see what you're really intending to do there, to see if it is correct. It looks like you are trying to define variable y as an int and assign it the value of 0.

That's fine, but you already defined it at the top of the function, why are you trying to define it again? Incidentally, I forget if the types are case sensitive or not. Int might not be the same as int.

Also, there's another issue there that you'll need to step through logically to find. Plug in values to the variables and walk through the pseudocode.

Finally, are you sure the curly braces are being used correctly?

Top

 Post subject: Posted: Sun Apr 19, 2009 4:45 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
This looks fun. One idea though. OOP.

What if I want to declare ^ as a mathematical operator?

Top

 Post subject: Posted: Mon Apr 20, 2009 10:23 am
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
Not sure what you mean there. Do you mean use ^ in C++ or to use it as an input to reflect that you want to see an exponentiation (ie 5^4)? Got an example?

Top

 Post subject: Posted: Mon Apr 20, 2009 10:43 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Dwood15 wrote:
This looks fun. One idea though. OOP.

What if I want to declare ^ as a mathematical operator?

This is the 'proper' way to do it but WELL beyond what the OP is asking ..

I assume that you mean you want to define '^' as an operator?

Top

 Post subject: Posted: Mon Apr 20, 2009 11:14 am
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Jipstyle wrote:
Dwood15 wrote:
This looks fun. One idea though. OOP.

What if I want to declare ^ as a mathematical operator?

This is the 'proper' way to do it but WELL beyond what the OP is asking ..

I assume that you mean you want to define '^' as an operator?

Yeah, so in something such as Java you wouldn't have to use math.pow(x, c);

Top

 Post subject: Posted: Mon Apr 20, 2009 5:26 pm
 8086

Joined: Sat Dec 27, 2008 7:17 pm
Posts: 28
Thanks for the great advice, I did get the code correct much in part to your responses.

I have seen many posts regarding the way programming is taught and I am agreeing more and more that the idea of understanding the problem and developing a solution should be taught thoroughly before expecting students to write code. I am finding myself trying to write programs when I have no idea really why I am entering some of the information =/

Top

 Post subject: Posted: Mon Apr 20, 2009 8:08 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
Dwood15 wrote:
Yeah, so in something such as Java you wouldn't have to use math.pow(x, c);

Well, to do that sort of thing really depends on the language. I think for the most part, on the common languages's, you can't extend them like how you are thinking. You would have to create your own compiler to parse the code (which no one pretty much does). I did that for a Compiler course way back in the day. It was interesting for background info, but in the end, way too much damn work for me to want to get into...

For the most part, you either create a function/procedure call you use in the language, or you use a #DEFINE like in C/C++ that acts as a code macro (when compiling your code, it scans through and replaces all those defined values with a function call or the code to do whatever it is you want).

I don't know enough about Java to say how you'd go about it in that language.

Top

 Post subject: Posted: Mon Apr 20, 2009 8:11 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
larry_ray30 wrote:
I am finding myself trying to write programs when I have no idea really why I am entering some of the information =/

Yeah, I see posts alluding to that way of teaching it as well (program a language first). It's been a while, but my recollection was I did algorithms, math (set and boolean theory), and assembler type classes prior to the third gen language type classes and compilers. Different strokes for different universities I guess.

Just remember, try to write it in pseudocode first. You can't solve a problem by tossing code at it until you understand what the problem is, and how you'll solve it in general. Once you've got the general outline and walk through it with test numbers, then you can code to it.

Top

 Post subject: Posted: Tue Apr 21, 2009 3:14 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
jcollins wrote:
Dwood15 wrote:
Yeah, so in something such as Java you wouldn't have to use math.pow(x, c);

Well, to do that sort of thing really depends on the language. I think for the most part, on the common languages's, you can't extend them like how you are thinking. You would have to create your own compiler to parse the code (which no one pretty much does). I did that for a Compiler course way back in the day. It was interesting for background info, but in the end, way too much damn work for me to want to get into...

For the most part, you either create a function/procedure call you use in the language, or you use a #DEFINE like in C/C++ that acts as a code macro (when compiling your code, it scans through and replaces all those defined values with a function call or the code to do whatever it is you want).

I don't know enough about Java to say how you'd go about it in that language.

Defining an operator is a basic function of C++, actually ... it is quite easy.

Doing it in Java, though, is quite difficult because it is considered an 'unnecessary complication.'

Top

 Post subject: Posted: Tue Apr 21, 2009 9:55 am
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Jipstyle wrote:

Defining an operator is a basic function of C++, actually ... it is quite easy.

Doing it in Java, though, is quite difficult because it is considered an 'unnecessary complication.'

Yeah... whoever says it's unnecessary should be shot. Math.pow(x, y); Might get the job done but the world would be done a huge favor by having defined ^ to operate with exponents. in programs made solely for exponentiation ^ is obviously the more practical solution. When I get done with my Comp Sci course I'm going to work in python muahahaha

Top

 Post subject: Posted: Tue Apr 21, 2009 3:47 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Dwood15 wrote:
This looks fun. One idea though. OOP.

What if I want to declare ^ as a mathematical operator?

GREAT IDEA!!!
The only problem is, when C was being implemented (and C++ and Java both rely on C syntax), "^" was defined as the bitwise exclusive-or operator. So writing a compiler that used "^" for exponentiation would with almost certainty, break lots of programs that use the "^" as a bitwise operator.
For example:
52 ^ 12 = 56
Math.pow(52, 12) = 390877006486250192896

Obviously, 56 != 390877006486250192896.

Top

 Post subject: Posted: Tue Apr 21, 2009 4:21 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Quertior wrote:
Dwood15 wrote:
This looks fun. One idea though. OOP.

What if I want to declare ^ as a mathematical operator?

GREAT IDEA!!!
The only problem is, when C was being implemented (and C++ and Java both rely on C syntax), "^" was defined as the bitwise exclusive-or operator. So writing a compiler that used "^" for exponentiation would with almost certainty, break lots of programs that use the "^" as a bitwise operator.
For example:
52 ^ 12 = 56
Math.pow(52, 12) = 390877006486250192896

Obviously, 56 != 390877006486250192896.

then remove the bitwise and change it to something like the @ key.

Whoever declared ^ in the first place didn't know their operators. Perhaps we could use @ symbol for exponentiation instead then? xD. Anything with a one-symbol fix is better than math.pow(56, 12) = x ;

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 18 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets