#include<stdio.h> #include<stdlib.h> int main() { int i, n , c = 0, bacon = 1; d
ID: 3871558 • Letter: #
Question
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, n , c = 0, bacon = 1;
double sum = 0.0, sum1 = 0.0, max, min ,number;
char answer;
//----------------------------------------------------------------------------------------//
do
{
printf("********************* Welcome to the Stock Program ********************** ");
printf(" Do you know how many stocks you would like to put in? Enter y or n: ");
scanf(" %c",&answer); //User input
printf("Enter the number of stocks you liked to be analzyed: ");
scanf("%d",&n);
for(i = 0;i<n;++i){
printf("Enter the stock %d: ", i+1);
scanf("%lf",&number);
if(number<0)
{
c++;
continue;
}
sum = sum + number;
sum1 = sum1 + number*number;
if(i==0)
{
max = number;
min = number;
}
else
{
if(max < number)
max = number;
if(min > number)
min = number;
}
}
if((n-c)>0)
{
printf("Average price is: $%.2f ",sum/(n-c));
}
else
{
printf("Average price is 0.00 ");
}
double variance = ((sum1/(n-c)) -((sum/(n-c))*(sum/(n-c))));
printf("Variance is: $%.2f ",variance);
printf("The highest price is: $%.2f ",max);
printf("The lowest price is: $%.2f ",min);
}
while(answer == 'y');
else if(answer=='n')
{
while(1){ //User input the value of the stocks if they want to stop the program
// But it will still calculate the user input before the negative.
printf("please note that a negative value will End program but will calculate the Values you entered ");
printf(" Enter Stock Value please:" );
scanf("%lf",&number);
if(number<0){
break;
}
sum = sum + number;
sum1 = sum1 + number*number;
if(bacon==1)// <------------------Its bacon because I was having bacon at the time and it turned into on of my int
{
max = number;
min = number;
bacon=0;
}
else
{
if(max < number)
max = number;
if(min > number)
min = number;
}
c++;
}
//Display Calcutlations below.
printf("***********************Calculation Complete****************************** ");
printf("Average price is: $%.2f ",sum/c);
double variance = ((sum1/c) -((sum/c)*(sum/c)));
printf("Variance is: $%.2f ",variance);
printf("The highest price is: $%.2f ",max);
printf("The lowest price is: $%.2f ",min);
}
return 0;
}
how do I get my Do statements and while statements to work and my n to work user enters yes it works, user enters no it doesn't. may someone fix this to where my do statement works please having a little bit of trouble
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, n , c = 0, bacon = 1;
double sum = 0, sum1 = 0, max, min ,number;
char answer;
double variance;
//----------------------------------------------------------------------------------------//
printf("********************* Welcome to the Stock Program ********************** ");
printf(" Do you know how many stocks you would like to put in? Enter y or n:If you want to exit enter x ");
scanf(" %c",&answer); //User input
while(answer=='y'||answer=='n')
{
if(answer=='y')
{
printf("Enter the number of stocks you liked to be analzyed: ");
scanf("%d",&n);
for(i = 0;i<n;++i){
printf("Enter the stock %d: ", i+1);
scanf("%lf",&number);
if(number<0)
{
c++;
continue;
}
sum = sum + number;
sum1 = sum1 + number*number;
if(i==0)
{
max = number;
min = number;
}
else
{
if(max < number)
max = number;
if(min > number)
min = number;
}
}
if((n-c)>0)
{
printf("Average price is: $%.2f ",sum/(n-c));
}
else
{
printf("Average price is 0.00 ");
}
variance = ((sum1/(n-c)) -((sum/(n-c))*(sum/(n-c))));
printf("Variance is: $%.2f ",variance);
printf("The highest price is: $%.2f ",max);
printf("The lowest price is: $%.2f ",min);
}
else if(answer=='n')
{ //User input the value of the stocks if they want to stop the program
// But it will still calculate the user input before the negative.
printf("please note that a negative value will End program but will calculate the Values you entered ");
printf(" Enter Stock Value please:" );
scanf("%lf",&number);
if(number<0){
break;
}
sum = sum + number;
sum1 = sum1 + number*number;
if(bacon==1)// <------------------Its bacon because I was having bacon at the time and it turned into on of my int
{
max = number;
min = number;
bacon=0;
}
else
{
if(max < number)
max = number;
if(min > number)
min = number;
}
c++;
//Display Calcutlations below.
printf("***********************Calculation Complete****************************** ");
printf("Average price is: $%.2f ",sum/c);
variance = ((sum1/c) -((sum/c)*(sum/c)));
printf("Variance is: $%.2f ",variance);
printf("The highest price is: $%.2f ",max);
printf("The lowest price is: $%.2f ",min);
}
printf(" Do you know how many stocks you would like to put in? Enter y or n:If you want to exit enter x: ");
scanf(" %c",&answer); //User input
}
return 0;
}
Output:
********************* Welcome to the Stock Program **********************
Do you know how many stocks you would like to put in? Enter y or n:If you want to exit enter x: y
Enter the number of stocks you liked to be analzyed:2
Enter the stock 1:567
Enter the stock 2: 800
Average price is: $683.50
Variance is: $13572.25
The highest price is: $800.00
The lowest price is: $567.00
Do you know how many stocks you would like to put in? Enter y or n:If you want to exit enter x : n
please note that a negative value will End program but will calculate the Values you entered
Enter Stock Value please:567
***********************Calculation Complete******************************
Average price is: $567.00
Variance is: $0.00
The highest price is: $567.00
The lowest price is: $567.00
Do you know how many stocks you would like to put in? Enter y or n:If you want to exit enter x : x
I hope its better to use while instead of do while
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.