Hey everyone. First post on these boards! I recently got into very basic C-programming because I'm taking a mandatory course in college for C-programming. The professor has assigned a few simple programs for us to create with the limited knowledge of C-programming that we have so far. Anyways, on to what brought me here...
I've had a couple minor problems with a couple of the programs he's assigned. The first one here, all the code looks perfect. It looks like it should run perfectly. But then when I run it, it asks for the first input, waits to take it in (and takes it in), waits for the second input, then skips right over that and moves on to asking the third user input and waiting for that. What's up with that? Please look at this code and tell me what's wrong with it, because I just can't see it.
Code:
// Description: Calculates how many pictures you can store on a CD.
#include <stdio.h>
#define MB 1048576
#define PX 3
int main()
{
// Delcares necessary variables
int cd_space, pic_length, pic_width;
int pic_size, number_pics;
// Asks user for input
printf("How many megabytes of free space is on the CD? ");
scanf("&d", &cd_space);
printf("What is the length of each picture in pixels? ");
scanf("%d", &pic_length);
printf("What is the width of each picture in pixels? ");
scanf("%d", &pic_width);
// Calculates how many whole pictures can be placed on the CD
pic_size = pic_width * pic_length * PX;
number_pics = (cd_space * MB) / pic_size;
printf("You can store %d pictures on your CD.", number_pics);
return 0;
}
Second program here, I'm getting different numbers in the results than my professor's practice run of the program. Can you please look at this one and tell me if all the math and everything looks correct, and it might be my professor's program that was wrong?
Code:
// Description: Calculates the number of jelly beans in a container.
#include <stdio.h>
#define PI 3.14159265
int main()
{
// Declare necessary variables
double jb_radius, conta_radius, conta_height, percent_air;
double jb_volume, conta_volume, conta_volume_noair, jb_number;
// Request user input
printf("What is the radius of a single jelly bean? ");
scanf("%lf", &jb_radius);
printf("What is the radius of the container? ");
scanf("%lf", &conta_radius);
printf("What is the height of the container? ");
scanf("%lf", &conta_height);
printf("What percentage of the container is filled with air? ");
scanf("%lf", &percent_air);
// Calculate volumes and number of jelly beans
conta_volume = PI * conta_radius * conta_radius * conta_height;
conta_volume_noair = conta_volume - (conta_volume * (percent_air * .01));
jb_volume = 4 / 3 * PI * jb_radius * jb_radius * jb_radius;
jb_number = conta_volume_noair / jb_volume;
// Tell user how many jelly beans are in the jar
printf("There are %.0lf jelly beans in the jar.", jb_number);
return 0;
}
Thanks to any and everyone who helps!
- Randy