LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2005, 02:11 AM   #1
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Rep: Reputation: 15
please help


dose anyone know who to write loop to raise power using +/- operator only?
(eg: 2^3 = 8, 7^2 = 49, 5^4 = 625, 15^7 = 170859375 and so on)
thanks in advance
 
Old 04-19-2005, 02:33 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Another one of those homework assignments, I suppose :P I will tell you without the solution :P 2^3=8 could be writen 2+2+2+2=8 and like. Try to figure this out on your own.
 
Old 04-19-2005, 02:42 AM   #3
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
i already knew that, and i tried to use nested for loops but it didn't work. but thanks for your reply
 
Old 04-19-2005, 02:48 AM   #4
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi thtr2k
can you send your code ??

Regards
Mehul
 
Old 04-19-2005, 04:11 AM   #5
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
I can put this in C code:

#include <stdio.h>

int main(int argc, char** argv[])
{

int n=atoi(argv[1]);
int p=atoi(argv[2]);
int result=0;

int i,j;

for(i=0; i<p; i++)
for(j=0; j<n/2; j++)
if((i==0) && (j==0))
result=n;
else
result=result+result;

printf("%d na %d = %d",n,p,result);
return 0;
}

This works like a charm :P
 
Old 04-19-2005, 09:46 AM   #6
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
to marghorp,

no / opertor only + or - i should say sorry for the typo, but thanks
 
Old 04-19-2005, 09:48 AM   #7
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
example for multiplication code
Code:
int result = 0;
           //loop to calculate the multiply() method   
           for(int i = 1; i <= secondNumber; i++)
           {
              result += firstNumber;
                            
           }
           System.out.println("The result is: "+ result);
 
Old 04-19-2005, 10:27 AM   #8
mrgoodtrips
LQ Newbie
 
Registered: Mar 2005
Posts: 11

Rep: Reputation: 0
I would do something like this:

Code:
//usage <filename> x y where you want x^y
int main(int argc, char *argv[])
{
  int answer;
  answer = function add_power(argv[0], argv[1]);
  printf("%d to the power %d is %d\n", argv[0], argv[1], answer);
  return 0;
}

int add_power(x, y)
{
  int i;
  int retval;

  if(y == 1)
  {
    return x;
  }
  else
  {
    for(i = 0; i < x; i++)
    { 
      retval += add_power(x, y-1)     
    }
   return retval;
  }
}
The recursive function might need a little tweaking (I only spent a minute on this) but that's the basic idea.
 
Old 04-19-2005, 01:35 PM   #9
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I think you mean argv[1] and argv[2]. argv[0] will be the executable...
 
Old 04-19-2005, 04:10 PM   #10
mrgoodtrips
LQ Newbie
 
Registered: Mar 2005
Posts: 11

Rep: Reputation: 0
Hehe... fair enough. My mistake.
 
Old 04-19-2005, 04:27 PM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Actually, I felt a bit bad about posting that since you did say that it would require some tweaking. Didn't notice that until after I posted, and then I felt like I was nitpicking...

I can see at least a few other things that would need to be tweaked, but it would probably be better to leave it up to the original poster to figure those things out...

Last edited by deiussum; 04-19-2005 at 04:31 PM.
 
Old 04-19-2005, 08:13 PM   #12
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
Thanks guys, i already have figured out the problem. i really appreciate your replies. Thank you once again
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration