HELP! (w/C program)
Geeky1
University of the Pacific (Stockton, CA, USA)
Ok, so I have a lab that was assigned today for my C class, and it's due tomorrow. The requirements of the lab are as follows:
Write a program that uses standard functions. The program may be written entirely in main and must follow the pseudocode below. Give the output appropriate captions, and align the data.
Pseudocode:
1. Prompt the user to enter a number
2. Read number.
3. Display number.
4. Get a random number and scale to range 3...37.
5. Display random number.
6. Set product to number + random number.
7. Display product.
8. Display ceiling of product.
9. Display floor of product.
10. Display number raised to power 5.
11. Display square root of random number.
My code:
The compiler is Visual C++ .net student edition, if it matters.
Here's the issue (look at the last few lines of the program):
Write a program that uses standard functions. The program may be written entirely in main and must follow the pseudocode below. Give the output appropriate captions, and align the data.
Pseudocode:
1. Prompt the user to enter a number
2. Read number.
3. Display number.
4. Get a random number and scale to range 3...37.
5. Display random number.
6. Set product to number + random number.
7. Display product.
8. Display ceiling of product.
9. Display floor of product.
10. Display number raised to power 5.
11. Display square root of random number.
My code:
#include stdio.h #include stdlib.h #include time.h #include math.h int main (void) { int user_num1 = 0; int rand_num1 = 0; int product = 0; printf("Enter a number: "); scanf("%d", &user_num1); printf("\nThe number you entered was: %d", user_num1); srand (time (NULL)); rand_num1 = rand() % 35+3; printf("\nA random number: %d", rand_num1); product = user_num1 * rand_num1; printf("\nProduct of your number x the random number: %d", product); double ceil (product); double floor (product); printf("\nThe ceiling of the product is: %10.2f", ceil); printf("\nThe floor of the product is: %10.2f", floor); pow (user_num1, 5); sqrt (rand_num1); printf("\nYour number raised to the 5th power is: %10.2f", pow); printf("\nThe square root of the random number is: %10.2f", sqrt); return 0; }It's not completed, but I'm trying to debug it as I go. You can see the issue that I'm running into in the attached screenshot. Any suggestions on why it's doing this?
The compiler is Visual C++ .net student edition, if it matters.
Here's the issue (look at the last few lines of the program):
0
Comments
Anyone got any ideas?
Blah!
/me goes to bed!
Your problem is in your use of the functions pow and sqrt and the meaning of the statement function(argument). The function pow(), or sqrt() or whatever return data of a set type , however, if you want to want use this in your program you either have to assign the returned value to some variable in your program, or use the value directly. I've included examples of both in the following corrected code:
double ans = pow (user_num1, 5);
printf("\nYour number raised to the 5th power is: %10.2f",ans) ;
printf("\nThe square root of the random number is: %10.2f", sqrt(rand_num1));
When you called pow(a,b) you just called the function but never did anything with is output.
Just to be sure (been over a year since i've touched C) I did compile this and it worked on gcc.
You'll need to correct your use of the pow and sqrt functions accordingly and I'll leave that to you. (Note that I didn't check anything else, i only corrected the error in function use). GL and HF.
Thanks again.
//Edit
Oh, I did have the > and < symbols in there, but for some reason it wouldn't show up. Oh well...
/me really goes to bed, LOL