Jump to content
  • Advertisement
Sign in to follow this  
Danishpx

need help on C++ exercise

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the Dev-C++ compiler and I am trying to do the following: Write a program that gets three scores from the user then display the average. Can someone show me the code so I can learn how to do this. I swear I am a major newb and I'm trying to figure this out. A list of the actual code and what it means would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Danishpx
I am using the Dev-C++ compiler and I am trying to do the following:
Write a program that gets three scores from the user then display the average. Can someone show me the code so I can learn how to do this. I swear I am a major newb and I'm trying to figure this out. A list of the actual code and what it means would be greatly appreciated.



Lots of ways to do this.


#include <iostream>
using namespace std;

int main()
{
float x,y,z;

cout << "Enter three numbers: ";
cin >> x >> y >> z;

cout << "average: " << (x+y+z)/3 << endl;

return 0;
}

Share this post


Link to post
Share on other sites
and you can do this way and is the better way to learn object oriented:

mathematics.h file:

#ifndef mathematics_h
#define mathematics_h

class mathematics
{
private:

float value;

public:

mathematics();
~mathematics();

float average();
};

#endif

mathematics.cpp file:

#include "mathematics.h"
#include <iostream.h>

mathematics::mathematics()
{
}
mathematics::~~mathematics()
{
}
float mathematics::average()
{
int num;
float total;
float sum;

cout<<"How mutch numbers?"<<endl;
cin>>num;
for(i=0;i<num;i++)
{
cout<<"write the number:"<<endl;
cin>>value;
sum=sum+value;
}
total=sum/num;
cout<<"Result:"<<total<<endl;
}

main.cpp file:

#include "mathematics.h"
#include <iostream.h>

void main()
{
mathematics m;

m.average()
}

The other way is right too but this way is more object oriented.
I didnt test it in the compiler.

Share this post


Link to post
Share on other sites
Thanks alot for yer help both of you. I just have one more question. What does it mean when you put float before x, y, and z?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Dude, thats why books and tutorials are for! Dont ask questions like that anymore :) but still, float means its a floating point number - for you - a number that can have digits after the comma: like 1.5 and 8.6

int a; //means a can only hold values that are whole numbers: 1,2,3 etc

float a;//means a can hold values like 1, 1.1, 1.2, 1.4364576 and stuff :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Danishpx
What does it mean when you put float before x, y, and z?


The way your problem statement is written:

Quote:

I am using the Dev-C++ compiler and I am trying to do the following:
Write a program that gets three scores from the user then display the average.


very, very clearly indicates a copy-and-paste from some outside source (either a teacher or a book). This indicates that you have either a teacher to ask (or pay attention to in class) or a book to read (up until the exercise section at the end of the chapter). Yes? :s

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!