To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
Geeky1
No comment.
Geeky1
7,972 Posts

HELP! (w/C program)

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:
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):
Attached Thumbnails
Click image for larger version

Name:	problem.jpg
Views:	230
Size:	36.7 KB
ID:	3280  
__________________ -MCCCXXXVII
"Ignorance is forgivable. Stupidity is inexcusable."

Resident Dual CPU Addict: 3 working duallies, 2 more in the parts drawer, and looking to add an Opteron dually to that list as soon as I can find a good motherboard...
Geeky1
No comment.
Geeky1
7,972 Posts


Anyone got any ideas?
a2jfreak
madasiemanym
a2jfreak
3,351 Posts
I told you I was going to sleep, and now I see this thread.

Blah!

* a2jfreak goes to bed!


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;
	double exponents = 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);
	

	exponents = pow (user_num1, 5);
	printf("\nYour number raised to the 5th power is: %10.2f", exponents);
	exponents = sqrt (rand_num1);
	printf("\nThe square root of the random number is: %10.2f", exponents);

	return 0;

}
__________________
One thing kids like is to be tricked. For instance, I was going to take my little nephew to Disneyland, but instead I drove him to an old burned-out warehouse. "Oh, no," I said. "Disneyland burned down." He cried and cried, but I think that deep down, he thought it was a pretty good joke. I started to drive over to the real Disneyland, but it was getting pretty late.
Jack Handey
[folding_sig1]Child Search Ministries.
A Christian Charity for Missing Children.

Get Firefox!
qparadox
I'll Think of Something
qparadox
814 Posts
Hope this isn't too late.


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.
Geeky1
No comment.
Geeky1
7,972 Posts
Thanks for the help guys. I'll try that out tomorrow morning (class starts @ 10:30, I have to leave @ 10, It's too late to work on it now... I'll get up early tomorrow morning and do it). This function thing has really thrown me for a loop. The class was dead easy up until we hit this chapter. Aggh!

Thanks again.

//Edit
Oh, I did have the > and < symbols in there, but for some reason it wouldn't show up. Oh well...
a2jfreak
madasiemanym
a2jfreak
3,351 Posts
Just copy my code, it works.

* a2jfreak really goes to bed, LOL
Go Back   Icrontic Forums > Tech: Software > Web & Digital Media
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 9:39pm (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.