By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

gettin d average!!

P: 29
write a program that will accept 2
integer numbers from the user and
then display out
all the integer
numbers in between
those two integers
excluding the two
numbers, then
display the avg
among the output
displayed.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double one,two,i,j,sum=0,a,b;//declaring variables
  7.  
  8.  
  9. cout<< "first value = ";//ask for first num
  10. cin>>one;               //first input
  11. cout<<"second value = ";//ask for second num
  12. cin>>two;               //second input
  13.  
  14. cout<<'\n';             //skip a line
  15.  
  16.        if(two>one){
  17.             for(i=one;i<=two;i++)
  18.             cout<< i <<endl;
  19.                      for (int i=1; i<=two; i++)
  20.                      sum = sum + i;
  21.                      cout<<sum/i<<endl;
  22.        }else{
  23.             for(j=two;j<=one;j++)
  24.             cout<< j <<endl;
  25.                   }
  26.  
  27.  
  28.  
  29.     system("pause");
  30.     return 0;
  31. }


after this im stuck...i cant get the average part to work out rite! anyone able to help me out i can get it to sum up but i cant get it to divide so i'll get d average
Mar 18 '07 #1
Share this Question
Share on Google+
4 Replies


arne
Expert 100+
P: 315
write a program that will accept 2
integer numbers from the user and
then display out
all the integer
numbers in between
those two integers
excluding the two
numbers, then
display the avg
among the output
displayed.

#include<iostream>
using namespace std;

int main()
{
double one,two,i,j,sum=0,a,b;//declaring variables


cout<< "first value = ";//ask for first num
cin>>one; //first input
cout<<"second value = ";//ask for second num
cin>>two; //second input

cout<<'\n'; //skip a line

if(two>one){
for(i=one;i<=two;i++)
cout<< i <<endl;
for (int i=1; i<=two; i++)
sum = sum + i;
cout<<sum/i<<endl;
}else{
for(j=two;j<=one;j++)
cout<< j <<endl;
}



system("pause");
return 0;
}



after this im stuck...i cant get the average part to work out rite! anyone able to help me out i can get it to sum up but i cant get it to divide so i'll get d average

You were almost there. Try this
Expand|Select|Wrap|Line Numbers
  1.     if (one == two || one+1 == two || one == two+1 ) {
  2.  
  3.         /* nothing to do */
  4.         return 0;
  5.     }
  6.  
  7.     if (two < one) {
  8.  
  9.         /* make sure one < two */
  10.         double tmp;
  11.         tmp = one;
  12.         one = two;
  13.         two = tmp;
  14.     }
  15.  
  16.     for (i=one+1; i<two; i++) {
  17.  
  18.         cout << i << endl;
  19.         sum = sum + i;
  20.     }
  21.  
  22.     cout << sum/(two-one-1) << endl;
  23.  
Note that I changed the conditions in the loop and the divisor for the average.
Mar 18 '07 #2

P: 29
wo thnks alot! really helped...
Mar 18 '07 #3

P: 1
write a program that will accept 2
integer numbers from the user and
then display out
all the integer
numbers in between
those two integers
excluding the two
numbers, then
display the avg
among the output
displayed.

#include<iostream>
using namespace std;

int main()
{
double one,two,i,j,sum=0,a,b;//declaring variables


cout<< "first value = ";//ask for first num
cin>>one; //first input
cout<<"second value = ";//ask for second num
cin>>two; //second input

cout<<'\n'; //skip a line

if(two>one){
for(i=one;i<=two;i++)
cout<< i <<endl;
for (int i=1; i<=two; i++)
sum = sum + i;
cout<<sum/i<<endl;
}else{
for(j=two;j<=one;j++)
cout<< j <<endl;
}



system("pause");
return 0;
}



after this im stuck...i cant get the average part to work out rite! anyone able to help me out i can get it to sum up but i cant get it to divide so i'll get d average

owH! U donT use Printf and scanf?
Mar 19 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
printf and scanf are functions used mainly in C, but have been replaced by cout and cin in C++. You can still use scanf/printf if you include the header file <cstdio>
Mar 19 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.