Jump to content

Finding means,improving this code

- - - - -

  • Please log in to reply
17 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
Hello guys vaironl here, I wanted to know if I could make this code a bit better.
as in abridging it or use other type of commands, etc...
By the way I'm using
System("Pause")  
because of Dev c++



Here is the code

#include <iostream>

#include <string>


using namespace std;

int main()

{

    double y,c,k,l,sum ;

    double result;

    cout<<"Please enter a 4 numbers at the end there will be a mean for the sum of the numbers."<<endl<<endl<<endl;

    cout<<"First number "<<endl;

    cin>> y;

    cout<<"Second number"<<endl;

    cin>> c;

    cout<<"Third number"<<endl;

    cin>> k; 

    cout<<"Fourth number"<<endl;

    cin>> l;

    result = (y + c + k + l) / 4;

    

    

       cout<<"the mean of the numbers is:"<<result<<endl;

    

    

    

    

    system("pause");

    return 0;

}


Edited by vaironl, 05 June 2011 - 03:23 PM.
Typos


#2
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
cin.get()
Be a joke unto yourself!
Check out my blog at Flashcore

#3
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
double num[4];

for(i = 0; i<4; i++){
cout << "Enter the "<< i << "number: ";

#4
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Why did you included string?
Well! Several ways to achieve the same task, have a look,


double num[4];

double sum = 0, mean = 0;


for(int i = 0; i<4; i++){

     cout << "\nEnter the "<< i << "number: ";

     cin >> num[i];

     sum += num[i];

}

mean = sum / 4;

cout << "\nThe mean of the numbers is : " << mean;


or even without an array and calculating the mean seperately,

double num = 0, sum = 0;


for(int i = 0; i<4; i++){

     cout << "\nEnter the "<< i << "number: ";

     cin >> num;

     sum += num;

}

cout << "\nThe mean of the numbers is : " << (sum / 4);


Want to print it as double?

cout << "\nThe mean of the numbers is : " <<static_cast<double> (sum / 4);


Hope That Helped!

#5
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
The farthest reply sent mistakenly.

#6
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
If you use solutions suggested above i.e. computing sum in loop it would be a little more impressive to compute and keep displaying the running mean. :)

#7
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

AKMafia001 said:

double num[4];

for(i = 0; i<4; i++){
cout << "Enter the "<< i << "number: ";

Is the num[4] the same thing as num4? or is it just saying to the computer only 4 characters allowed?

and the for loop I'm trying to understand it could I get a bit more detail, sorry for not being able to recognize it.

#8
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
You need to learn some basics - Arrays and Loops

float num[4] means you are creating a group of 4 variables num[0], num[1]... num[3] each of which is capable of storing a float.

Each of these variables is accessed the same way i explained them above i.e. num[index].

A loop is a piece of code which executes itself again and again from a starting value to ending value.

In this case, the loop executes 4 times (from 0 to 3) doing the job you were trying to do by hard coding every step.

Imagine if you had to input 100 numbers instead of 4. Certainly loop is the right way.

#9
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

fayyazlodhi said:

You need to learn some basics - Arrays and Loops

float num[4] means you are creating a group of 4 variables num[0], num[1]... num[3] each of which is capable of storing a float.

Each of these variables is accessed the same way i explained them above i.e. num[index].

A loop is a piece of code which executes itself again and again from a starting value to ending value.

In this case, the loop executes 4 times (from 0 to 3) doing the job you were trying to do by hard coding every step.

Imagine if you had to input 100 numbers instead of 4. Certainly loop is the right way.

I understand loops but I wasn't aware that if you did the num[4] object it would create 4 groups. I have seen the loop for and the ++ commands which I think it's to add on to itself.
I'm curious of how exactly it takes the number do you have to do 4+4+4+4? or do you just do 4enter... So on?


Sorry again for confusing myself , and asking so many question that's me ...

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Well yes, I would personally suggest:


  • Use an STL vector instead of an array, it can accommodate resizing automatically, i.e. entering varying amount of numbers and not having to do strange memory operations on the array.
  • Initialize all variables, += is dangerous in your code as it is now if you do not.
  • Use more meaningful variable naming, such as mean instead of result.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Alexander said:

Well yes, I would personally suggest:


  • Use an STL vector instead of an array, it can accommodate resizing automatically, i.e. entering varying amount of numbers and not having to do strange memory operations on the array.
  • Initialize all variables, += is dangerous in your code as it is now if you do not.
  • Use more meaningful variable naming, such as mean instead of result.

STL vector Ill look it up and when you mean initialize all the variables do you mean give it a name?

#12
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

vaironl said:

STL vector Ill look it up and when you mean initialize all the variables do you mean give it a name?

It sometimes is a common mistake to do this:
int foo;

...

...

foo += some_number;

This will generate an incorrect garbage number, so it is wise to initialize everything prior to use.
int foo = 0;
You must be careful otherwise what variables are and are not initialized.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users