PDA

View Full Version : c programmers help




andrewface
Feb 7, 2007, 08:35 PM
i have an assignment due for school and i cant figure out one question (has to be in c)

can someone help me write a program that takes two numbers that are input by the user and raises one to the power of the other...this cant be done with the exponent function it must be done by using looping

thanks :apple:



mduser63
Feb 7, 2007, 08:56 PM
Are they both integers? If so, it's relatively easy and you should figure it out on your own (sorry). If not, I have to figure you're in a pretty advanced programming class...

andrewface
Feb 7, 2007, 09:00 PM
Are they both integers?

ya both integers and its a beginner class...which is why i have no idea what to do.. i have no programming experience what so ever

XnavxeMiyyep
Feb 7, 2007, 09:03 PM
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

EDIT: Now that I see your previous message, do you know how to make for loops?

andrewface
Feb 7, 2007, 09:04 PM
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

awwwww!!! i got that far...i just cant get it to work uhg

CaptainZap
Feb 7, 2007, 09:05 PM
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

Well you could have three...

andrewface
Feb 7, 2007, 09:08 PM
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

EDIT: Now that I see your previous message, do you know how to make for loops?

yes i know the theory but this is my first time trying...i get very confused with the variables tho

i want the program to go

enter a number: x
enter exponent: y

x to the power of y: answer

for (initial statement ; condition ; iterating statement) {
body
} right?

XnavxeMiyyep
Feb 7, 2007, 09:09 PM
awwwww!!! i got that far...i just cant get it to work uhg
Okay. Assuming you already know how to get the user to input the values, the for loop should repeat until it goes through as many times as the exponent's value, and each time, it should multiply an additional integer (which should be initialized to 1) by the base number that the user inputted. This additional integer would be the answer.

This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)

andrewface
Feb 7, 2007, 09:11 PM
Okay. Assuming you already know how to get the user to input the values, the for loop should repeat until it goes through as many times as the exponent's value, and each time, it should multiply an additional integer (which should be initialized to 1) by the base number that the user inputted.

This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)

yes i know how to input values...can you show me what you mean for the for loop?...thats where i get confused the initializing

CaptainZap
Feb 7, 2007, 09:12 PM
This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)

One for the Number/Result, one for the Exponent, and then another variable. I'll message you it since I don't want to spoil it for Andrew.

andrewface
Feb 7, 2007, 09:17 PM
can someone show me the code?

CaptainZap
Feb 7, 2007, 09:22 PM
can someone show me the code?

Andrew, you aren't going to learn by having other people do the work, how about you post your code, and we can see what's wrong with it ^_^

andrewface
Feb 7, 2007, 09:23 PM
Andrew, you aren't going to learn by having other people do the work, how about you post your code, and we can see what's wrong with it ^_^

ok should i try it with 4 or 3 variables?

XnavxeMiyyep
Feb 7, 2007, 09:24 PM
can someone show me the code?
I think I realize what CaptainZap is saying, but for now, I think you should stick with the four variable method.

Time for some of the code, mixed with some pseudo code.

int base = input from user;
int exponent = input from user;
int i =0;
int result = 1;

//For loop goes here, which will modify the result variable

output result;

See if you can figure it out from here (not trying to be mean; just trying to teach a bit)

CaptainZap
Feb 7, 2007, 09:27 PM
ok should i try it with 4 or 3 variables?

Yah, do 4 variables like XnavxeMiyyep said, it will be easier to manage.

andrewface
Feb 7, 2007, 09:29 PM
k i dont understand loops

#include <stdio.h>
main()
{
int n, b, e, i=0;

printf("Please enter a number:");
scanf("%d\n", &b);
printf("Please enter an exponent:");
scanf("%d", &e);

for (i=1

XnavxeMiyyep
Feb 7, 2007, 09:35 PM
k i dont understand loops

#include <stdio.h>
main()
{
int n, b, e, i=0;

printf("Please enter a number:");
scanf("%d\n", &b);
printf("Please enter an exponent:");
scanf("%d", &e);

for (i=1
First of all, you will find that, with the exception of counting variables, like i, you want to use descriptive words to name your variables instead of letters, or confusion will ensue!

Also, the result (which I assume is n), needs to be initialized to 1, not 0. Otherwise, when you multiply it by the base over and over again (HINT HINT) , it would just stay at 0.

for(i=0;i<e;i++) //Although, e should really be named exponent
{
//You're really close now. You can do this with just one line of code.
}

andrewface
Feb 7, 2007, 09:41 PM
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=1;i<exponent;i++) {

printf("%d", base*i);
}

return 0;
}


i know thats wrong...why am i so bad at this?...im having alot of trouble understanding loops
do i put i=0 at the beginning like that?

XnavxeMiyyep
Feb 7, 2007, 09:49 PM
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=1;i<exponent;i++) {

printf("%d", base*i);
}

return 0;
}


i know thats wrong...why am i so bad at this?...im having alot of trouble understanding loops
Okay, a couple things. The printf does not actually save any results, so all this will do is print out the base times i over and over again. Your printf should come after the loop, and you should just print out the answer. (So I guess I was sort of unclear about only needing one more line. You just needed one line IN THE LOOP)

To calculate the answer, you want to keep multiplying it by the base over and over again (as this is how exponents are calculated). Good luck.

andrewface
Feb 7, 2007, 09:53 PM
Okay, a couple things. The printf does not actually save any results, so all this will do is print out the base times i over and over again. Your printf should come after the loop, and you should just print out the answer. (So I guess I was sort of unclear about only needing one more line. You just needed one line IN THE LOOP)

To calculate the answer, you want to keep multiplying it by the base over and over again (as this is how exponents are calculated). Good luck.
ok so

answer=base*i

}

printf("%d", answer); ??

XnavxeMiyyep
Feb 7, 2007, 09:56 PM
ok so

answer=base*i

}

printf("%d", answer); ??

answer = base*answer;

Otherwise, you've just made a randomly long multiplication function.:p

andrewface
Feb 7, 2007, 09:58 PM
answer = base*answer;

Otherwise, you've just made a randomly long multiplication function.:p

i dont understand that tho...answer = base*answer

andrewface
Feb 7, 2007, 10:00 PM
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=0;i<exponent;i++) {
answer=base*answer;

}
printf("%d", answer);
return 0;
}

//hmm ok it works now theres just a problem with my inputs
i get this
Please enter a number:2
2 //should be 2nd input after asks for exponent
Please enter an exponent:4 //answer
q2 has exited with status 0.

XnavxeMiyyep
Feb 7, 2007, 10:01 PM
i dont understand that tho...answer = base*answer
You're setting answer to be a value that's equal to what its current value times base is. The '=' is not the algebraic equal sign, it is a sign that means "get".

XnavxeMiyyep
Feb 7, 2007, 10:04 PM
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=0;i<exponent;i++) {
answer=base*answer;

}
printf("%d", answer);
return 0;
}

This code should work correctly.

andrewface
Feb 7, 2007, 10:05 PM
You're setting answer to be a value that's equal to what its current value times base is. The '=' is not the algebraic equal sign, it is a sign that means "get".

ok...thanks alot that is alot clearer..

one last problem its taking the 2nd input scanf before the second printf how come?

Please enter a number:2
2
Please enter an exponent:4
q2 has exited with status 0.

XnavxeMiyyep
Feb 7, 2007, 10:09 PM
ok...thanks alot that is alot clearer..

one last problem its taking the 2nd input scanf before the second printf how come?

Please enter a number:2
2
Please enter an exponent:4
q2 has exited with status 0.
I'm actually not sure. The code looks correct, but I'm not familiar with plain C.

CaptainZap
Feb 7, 2007, 10:14 PM
Just take out the \n in the first scanf, so it looks like this

#include <stdio.h>

main()
{
int answer = 1, base, exponent, i;

printf("Please enter a number: ");
scanf("%d", &base);
printf("Please enter an exponent: ");
scanf("%d", &exponent);

for (i = 0; i < exponent; i++)
{
answer=base*answer;
}
printf("%d", answer);
printf ("\n");

return 0;
}

andrewface
Feb 7, 2007, 10:15 PM
thanks alot for your help

one last question what is the best code to use to make the window stay open at the end of the program...i used return 0, or getch() at school are those acceptable in the world of c?

CaptainZap
Feb 7, 2007, 10:21 PM
What do you mean stay open? What are you using to compile and run your program?

andrewface
Feb 7, 2007, 10:49 PM
heres another program i need help with for my lab tomorrow

i need to write a program that will take a string that you input and output whether or not it is a palindrome...it only has to work for simple palindromes like radar and not sentences and what not...someone gimme some insight?

scan
Feb 7, 2007, 10:58 PM
heres another program i need help with for my lab tomorrow

i need to write a program that will take a string that you input and output whether or not it is a palindrome...it only has to work for simple palindromes like radar and not sentences and what not...someone gimme some insight?

these are ridiculously easy and common problems. google it. I'm sure you'll find many solutions whether you like it in a for loop, recursive, while loop, etc.

andrewface
Feb 7, 2007, 11:01 PM
these are ridiculously easy and common problems. google it. I'm sure you'll find many solutions whether you like it in a for loop, recursive, while loop, etc.

ya im finding alot of really complicated ones or C++ versions on google..i just want a bit of a head start because everyone in my class have background in programming so im always the last one sitting there trying to figure it out

anyone get me started?

VoidBoi
Feb 7, 2007, 11:21 PM
Have you talked to your teacher? They do get paid to teach you. :)