Click to See Complete Forum and Search --> : C++ Coding Help Me
Eric
March 29th, 1999, 08:59 AM
I am new to c++ and need some help.
I need to create a code that asks the user
How many numbers would you like to Enter?
and then the user enters them one at a time.
And the program is supposed to give
the smallest , Largest , Average and the difference
between each number and the average.
I have done a bit of he code but it doesnt work...
Could you help create this code......But in as simple code as possible as
i am very new to programming....
Thank You for reading
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
int a,b,min;
cout<<"How many numbers would you like to enter?\t";
cin>>a;
{
cout<<"Enter a number please:\t";
cin>>b;
for (int count=1;count<=a;count=count+1)
if (count==1) min=b;
if (b<min) min=b;
}
cout<<"Smallest number is\t"<<min;
getch();
return 0;
}
Michael Decker
March 29th, 1999, 09:07 AM
Here's a start:
main()
{
clrscr();
int a,b,min,max,avg,sum=0;
cout<<"How many numbers would you like to enter?\t";
cin>>a;
for (int count = 0; count < a; count++)
{
cout<<"Enter a number please:\t";
cin>>b;
sum += b;
if (count == 0)
{
min = max = b;
}
else
{
if (b < min)
min = b;
if (b > max)
max = b;
}
}
avg = sum / a;
cout<<"Smallest number is\t"<<min;
getch();
cout<<"Largest number is\t"<<max;
getch();
return 0;
}
Michael Decker
March 29th, 1999, 11:42 AM
No problem.
After some experience, you'll be able to whip problems like that out in no time! Now, if I could just master MFC, COM, ATL, etc...
Jerry Coffin
March 29th, 1999, 11:54 AM
To do this, you'll have to store all the numbers as you receive them. You can figure the min, max and average without storing the numbers, but you can't figure the difference between each number and the averge until you have the final average, which you don't have until all the numbers are entered.
Since you're letting the user enter the number of entries (s)he's going to make, you can use that to allocate space to store the numbers. You'll have a structure something like this:
define a pointer to a double
ask the user for number of entries
double_pointer = new int[num_entries];
do a loop reading in one entry at a time into pointer[i]
figure your statistics from the array of doubles
print out the results.
Homework!
March 29th, 1999, 03:42 PM
Make him do his own homework, spoon feeding produces moronic programmers :)
Eric
March 29th, 1999, 09:36 PM
Thank YOU to all who helped. I was in a jam but your responces helped me alot.
To the guy who wrote HOMEWORK.
I am not being spoon fed. I am sure you have had problems where somebody
gave you a hand. Well this time I needed a hand and some good people helped
me. Maybe oneday I can help somebody with simmilar problems.
Thank You Guys again....
cdmahesh
April 10th, 2001, 01:41 AM
#include <iostream.h>
#include <conio.h>
main()
{
//clrscr();
int a,b,min;
cout<<"How many numbers would you like to enter?\t";
cin>>a;
cout<<"Enter a number please:\t";
cin>>min;
for (int count=2;count<=a;count=count+1)
{
//if (count==1) min=b;
cout<<"Enter a number please:\t";
cin>>b;
if (b<min) min=b;
}
cout<<"Smallest number is\t"<<min;
getch();
return 0;
}
this is the code for finding the smallest integer
u can modify it for other things.
it's better u should trace wat u do.
it'll help u a lot
enjoy
Mahesh Kumar C D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.