Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<stdio.h> #include<stdlib.h> int main() { int i, n , c=0; double sum = 0

ID: 3870981 • Letter: #

Question

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, n , c=0;
double sum = 0.0, sum1 = 0.0, max, min ,number;
char answer;
printf(" Do you know how many stocks you would like to put in? Enter y or n: ");
scanf(" %c",&answer);
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)//if the number is negative
{
c++;//increase count of the negative number
continue;//it is used to come out from the loop and further execution for next itearation
}
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)//now we are considering n-c elements for our further calculation
{
printf("Average price is: $%.2f ",sum/(n-c));//average for n-c elements
}
else
{
printf("Average price is 0.00 ");
}
//we are going to take n-c elements in consideration for variance too
double variance = ((sum1/(n-c)) -((sum/(n-c))*(sum/(n-c))));//it's a good habit to keep it in this way .. yours was also right
printf("Variance is: $%.2f ",variance);
printf("The highest price is: $%.2f ",max);
printf("The lowest price is: $%.2f ",min);
}

Question: with this code can I take out if, else statements and can I insert Do, while statements, and if I can may I have a example on how to do this with my code?

Explanation / Answer

No, in this code the if-else statements can not be replaced with do-while statements because -

In above program there can be variation in the results therefore it will not work.