help get me started

Feb 18, 2013 at 7:44pm
I know I need the header and namespace std, but how many int and double varibles will I need?

This is what I got so far?
#include <iostream>

using namespace std;

int main()
{
int score, weight;
int total=0;
int weighttotal=0;

while(cin >> score >> weight)
{
weighttotal+=weight;
total+=score*weight;
}
total=total/weighttotal;
cout << "The weighted average of these test scores is " << total << endl;

return 0;
}

How do I add the constPi into the above code?

Write a program to implement and test the algorithm that you designed for Exercise 15 of chapter 1. (You may assume the value of pi=3.141593. in your program, declare a named constant PI to store this value.)

sample data is as follows:
75 0.20
95 0.35
85 0.15
65 0.30
Last edited on Feb 18, 2013 at 7:56pm
Feb 18, 2013 at 8:02pm
At the top after your iostream declaration, you can do:

#define PI 3.1415926535897

or

const double PI = 3.1415926535897

You are just required to declare a constant.
Feb 19, 2013 at 11:58pm
Where does the sample data go in the program? do i put const double PI by the other varibles? or right after the iostream? Thanks for the help!
Last edited on Feb 20, 2013 at 12:01am
Feb 20, 2013 at 2:09am
Your're reading score and weight from cin, so you enter those values on the console.
You can also redirect standard input to read from a file without changing your program. Just use command line redirection.

You can put the const double PI anywhere before you reference it. After namespace std is a conventional place.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Feb 20, 2013 at 2:35am
closed account (3qX21hU5)
Like ABstraction said you can put it anywhere before you reference it. Ill explain this further. But first it is almost always best to use const int or whatever type instead of a macro for many reasons but that is a different topic.

Anyways if you put const double before int main() it will be consider a global variable which means it is availible anywhere in that .cpp file. Like for example if you had a function in the same source file as int main() the const double PI would be visible to that function meaning that it can use it. This is called Global Scope.

Now if you had const double PI inside of int main() (Meaning inside of the brackets {}) it would be available only to whatever is inside int main()'s scope (Whatever is inside the brackets). There are other different scopes that you should read up on and determine what they do. You can find them here http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm or by googling it.
Feb 20, 2013 at 8:29am
Okay, thanks for the help, I will run in c++ and check it for bugs!
Topic archived. No new replies allowed.