Need some advice, storing numbers.

Hello everyone, so I have this coding assignment that I have to for class. The issue I'm having is the last part of the question where it asks for storing the 125 into num1, 28 into num2, and –25 into num3. I want to able to learn how to do this, not just getting the answer right off the bat you know. Tell me what you think, pretty sure I missed something. Thank you, coders.


Write a C++ statement that includes the header file iostream.

Write a C++ statement that allows you to use cin, cout, and endl without the prefix std::.

Write C++ statement(s) that declare the following variables: num1, num2,num3, and average of type int.

Write C++ statements that store 125 into num1, 28 into num2, and –25 into num3.

Write a C++ statement that stores the average of num1, num2, and num3 into average.

Write C++ statement(s) that output the values of num1, num2, num3, and average.

Then compile and run your program by clicking the blue run button on the right side of the screen.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Programming Exercise 2-3
//include statement(s)
#include <iostream>
//using namespace statement
using namespace std;

int num1 = 125;
int num2 = 28;
int num3 = -25;
int average;

int main ()
{
    //variable declaration
    cout << "Enter the first nummber of your choice " << endl;
    cin >> num1;

    cout << "Now enter another number of your choice" << endl;
    cin >> num2;

    cout << "And finally enter the last number of your choice" << endl;
    cin >> num3;

    //executable statements
    average = num1 + num2 - num3;

    cout << "Your lucky number is " << average << endl;

    //return statement
    return 0;
}
Hello Frank5093,

You are mostly there, but not quite.

Points 1 and 2 are covered. Although i differ on point 2.

Point 3. A good start, but try to avoid global variables. Any line of code that follows global variables can change them and that could become hard to find a problem. Better to define them in "main" or where they are used.

You missed on point 4. Write C++ statements that store 125 into num1, 28 into num2, and –25 into num3.. I do not see anything here that says that you need a "cin" to store the values. Also the "cin", lines 16, 19 and 22, could store a different number than what you need.

Point 5, line 25, does not compute an average. There is no division.

Point 6 is only part of what you need.

Andy
double average = (num1 + num2 +num3) / 3.0;

1. double because the average of integers might not be an integer
2. 3.0 instead of 3 to force a double
3) Write C++ statement(s) that declare the following variables: num1, num2,num3, and average of type int.


This is almost certainly an incorrect requirement. I suspect it should be 'that defines the', not declares.

To be correct for the requirement you'd need something like:

 
extern int num1;


which declares num1 but is probably not what is expected - and which will not compile without these variables defined elsewhere! Defines and declares are different and not interchangeable.

Also, why is average supposed to be an int? An average of ints is most usually a non-integer??

Sorry, professor but for you a C-, could do better.

Last edited on
Thank you so much everyone, i really appreciate your time and effort. I will consider these advice from ya.
Here is the project, I went ahead and change some things around. thanks you for your time :D

//Programming Exercise 2-3
//include statement(s)
#include <iostream>
//using namespace statement
using namespace std;

int main ()
{
int num1 = 125;
int num2 = 28;
int num3 = -25;

//variable declaration
cout << "num1 =" << 125 << endl;
cout << "num2 =" << 28 << endl;
cout << "num3 =" << -25 << endl;

//executable statements
int average = (num1 + num2 + num3) / 3;

cout << "Your lucky number is " << average << endl;

//return statement
return 0;
}
For the num cout statements, you should use the variable names and not the given values.

To comply with the letter of the requirements (assuming define and not declare), then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Programming Exercise 2-3

// 1)
#include <iostream>

// 2)
using namespace std;

int main()
{
	// 3)
	int num1;
	int num2;
	int num3;
	int average;

	// 4)
	num1 = 125;
	num2 = 28;
	num3 = -25;

	// 5)
	average = (num1 + num2 + num3) / 3;

	// 6)
	cout << "num1 =" << num1 << endl;
	cout << "num2 =" << num2 << endl;
	cout << "num3 =" << num3 << endl;
	cout << "average = " << average << endl;

	return 0;
}

(125 + 28 + -25)/3 = 128/3 = 42
But,
42 X 3 = 126
Yep. That's what the assignment requires. I agree nonsensical for average but.... I guess the 'professor' has only covered int so far and not double/float.
The assignment explicitly says that average should be an int, so you'll need to do that for the assignment, but pay attention to what againtry is saying: The mathematical average is not an integer so in a "real world" program, you'd store the average as a double.

Also, note a common pitfall when computing averages. If you did this:
double average = (num1+num2+num3) / 3; You'd still get the wrong value, even though you're storing it in a double! The reason is that the program computes the right side of the equal sign first. Since all those values are ints, the type of the expression computed is an int. Then it takes that integer answer and converts it to a double.
Topic archived. No new replies allowed.