|
|
|
/* Module: tr4_q1a.cpp Author: Yariv Amar What? This program reads a series of 10 numbers and calculates the mathematical average of the first half and the geometric average of all the non zero numbers. Input : 10 numbers Output: mathematical and geometric average Assumptions on input: Valid 10 integer nonnegative numbers. How? Using for to get the data */ #include<iostream.h> #include<math.h> const int NUMBERS=10; void main()
{
int Num;
int NZ_counter=0; //declering on counter for Non Zero numbers.
double M_average=0 , G_average=1;
cout<<"Please enter the series of numbers and press enter:\n"; cout<<"---------------------------------------------------\n"; for (int i=1;i<=NUMBERS;i++)
{//start loop of NUMBERS
cin>>Num;
if (i<=NUMBERS/2) M_average+=Num;
if (Num)
{// case number is not zero
NZ_counter++;
G_average*=Num;
}//end if not zero
}//end loop
M_average/=(NUMBERS/2); cout<<"\nThe mathematical average for the first half is: "<<M_average; if (NZ_counter)
{//check if series is not empty
G_average=pow(G_average,1/double(NZ_counter));
cout<<"\nThe geomtric average is: "<<G_average<<"\n";
}
else cout<<"\nCannot make geomtric average cuse the series is empty.\n";
}
פלטים מיצגים Please enter the series of numbers and press enter: Please enter the series of numbers and press
enter:
/* Module: tr4_q1b.cpp Author: Yariv Amar What? This program reads a series of numbers and calculates the mathematical average of the first half and the geometric average of all the non zero numbers except for the first number. Input : series of numbers. Output: mathematical and geometric average Assumptions on input: Valid integer nonnegative numbers. How? The first number of the input is representing the length of the series, then using 'for' to get the data. */ #include<iostream.h> #include<math.h> void main()
{
int Num , Numbers;
int NZ_counter=0; //declering on counter for Non Zero numbers.
double M_average=0 , G_average=1;
cout<<"Please enter the series of numbers and press enter:\n"; cout<<"---------------------------------------------------\n"; cin>>Numbers;
if (Numbers>0)
{
for (int i=1;i<=Numbers;i++)
{
cin>>Num;
if (i<=Numbers/2) M_average+=Num;
if (Num)
{// case number is not zero
NZ_counter++;
G_average*=Num;
}//end if not zero
}//end for
M_average/=(Numbers/2);
cout<<"\nThe mathematical average for the first half is: "<<M_average;
if (NZ_counter)
{//check if series is not empty
G_average=pow(G_average,1/double(NZ_counter));
cout<<"\nThe geomtric average is: "<<G_average<<"\n";
}//end if not zero
else cout<<"\nCannot make geomtric average cuse the series is empty.\n";
}//end if have numbers
else cout<<"\nThere are no numbers on the series";
}
פלטים מיצגים
:Please enter the series of numbers and press enter
/* Module: tr4_q1c.cpp Author: Yariv Amar What? This program reads a series of numbers and calculates the mathematical and geometric average of them except of the last number that endes the series (-1) Input : series of numbers Output: mathematical and geometric average Assumptions on input: Valid integer nonnegative numbers.except for the last How? using 'while' to read the data until finds '-1' than stops to get data and calculate the averages . */ #include<iostream.h>
#include<math.h>
void main()
{
int Num , Numbers=0;
int NZ_counter=0;
double M_average=0 , G_average=1;
cout<<"Please enter the series of numbers and then enter.\n"; cout<<"* Notice that '-1' is ending the series\n"; cout<<"--------------------------------------------------\n"; cin>>Num;
while (Num!=-1)
{
Numbers++;
M_average+=Num;
if (Num)
{// case number is not zero
NZ_counter++;
G_average*=Num;
}//end if not zero
cin>>Num;
}
if (Numbers)
{//start printing results
M_average/=Numbers;
cout<<"\n---------------------------------";
cout<<"\nThe mathematical avarage is: "<<M_average;
if (NZ_counter)
{//check if series is not empty
G_average=pow(G_average,1/double(NZ_counter));
cout<<"\nThe geomtric average is: "<<G_average<<"\n";
}//end if not zero
else cout<<"\nCannot make geomtric average cuse the series is empty.\n";
}//end printing results
else cout<<"\nThere are no numbers on the series\n";//No results
}
פלטים מיצגים Please enter the series of numbers and then enter Please enter the series of numbers and then enter |
|
© Copyright YAProductions 2000 |