we are going to discuss this program

https://icpcarchive.ecs.baylor.edu/external/23/2362.pdf

source code is below-:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//Program #include<stdio.h> #include<conio.h> #include<iostream> // Preprocessor Directive using namespace std; // Using C++ Standard Library int main() // Main function { double avg, sum=0.0, a[12]={0}; // Real variable avg and sum, //and real array a int i; // Integer variable i for(i=0;i<12;i++) { // Input the income of 12 months a[0..11] //and summation cin>>a[i]; sum+=a[i]; } avg=sum/12; // Calculate the average monthly //income printf("$%.2f",avg); // Output the average monthly //income return 0; } |

What we did here was, we followed the following steps-:

We are needed to find the average monthly income, so we made array of income in 12 months ..say it is X[12]…and then we took the input of array elements from 1 to 12 i .e from month january to month december.

Now, we want to find the average monthly income of that person,and we know that

avg=sum/total n

so, we need to find sum before finding average

Hence,for that, we calculated sum as sum=sum+a[i]..addition of array with a simple variable is possible here! some one may get confused here…here a[i] is not the whole array, it will depend on the value of i,what a[i] is showing to us.

Now, we want to display the income of that person.

We don’t want our income to be displayed with 8 decimal figures, do we?

Remember that keeping many decimal values is necessary where even a small change of order of micro or nano, can have a great effect, and yes there are technologies which get affected by these amount of changes, let’s not get diverted towards that.

So, for showing only 2 decimal values we did “%.2f”-i.e we used the format specifiers here.

Hence done.

Though this may not seem as a good project like the one I did in past, but these small projects will also help you to learn data structures aand algorithms as well as programming.

They will also be suitable for programming contests also.

## Leave a Reply