Question
Change the C program below where it is exactly the same except it uses the two different loops. while, and do while. Make two different
Change the C program below where it is exactly the same except it uses the two different loops. while, and do while.
Make two different sets of code respectivly
#include
int main(int argc, char* argv[])
{
//Variables being declared
int counter,
numStocks;
char answer;
float stockPrice = 0.0f,
sum = 0.0f,
mean = 0.0f,
variance = 0.0f,
varianceSum = 0.0f,
min = 9999,
max = -9999;
// Introduction to the program for the user
printf(" ");
printf("This program calculates the mean, variance, min, and max of the given stock.");
printf("Do you know how many stocks you want analyzed? ");
printf("Type lower case y for yes or anything else for no. ");
scanf("%c", &answer);
if (answer == 'y')
{
printf("How many stocks would you liked analyzed");
scanf("%d", &numStocks);
for (counter = 1; counter <= numStocks; counter++)
{
printf("Please enter stock %d: ", counter);
scanf("%f", &stockPrice);
if (stockPrice >= 0) // checking for negative numbers
{
sum += stockPrice;
}
// determining the min and max
if (min > stockPrice)
{
min = stockPrice;
}
if (max < stockPrice)
{
max = stockPrice;
}
// finding the mean
mean = (sum / numStocks);
//finding the variance
variance += (stockPrice * stockPrice);
varianceSum = (variance / numStocks) - (mean * mean);
}
}
//if the user doesn't know how much stock they have
else
{
numStocks = 0;
//infinte loop
for(;;)
{
printf("Please enter stock price or a -1 to exit program:");
scanf("%f", &stockPrice);
if (stockPrice >= 0)
{
if (stockPrice > max)
{
max = stockPrice;
}
if (stockPrice < min)
{
min = stockPrice;
}
sum += stockPrice;
variance += (stockPrice * stockPrice);
// increment by one
numStocks++;
}
else{
break;}
mean = (sum / numStocks);
varianceSum = (variance / numStocks) - (mean * mean);
}
}
// displays results
printf("mean is: $ %.2f ", mean);
printf("Variance is: $ %.2f ", variance);
printf("The minimum price is: $ %.2f ", min);
printf("The maximum price is: $ %.2f ", max);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started