Need help with C++ program! New to programming :(
"You are to write a complete program that asks the user to input a series of positive whole integers (no decimal places). Your program will stop accepting input when a negative number is input. Once the negative number is input, your program will output the average (mean). Programming Specifications: • Your program must use only pointers • Your program must have at least one function • The output of the program will be a whole integer • All parameters passed in must be passed by reference ( address/pointers) • If a function returns a value it must be returned by reference • There is no need to validate numbers when input. You can assume correct numbers will be input. " I dont really know how to start this/do it. At all.
to begin any programming project, you need the proper tools, mainly the compiler along with some kind of editor/IDE. Do you have an IDE?
I have Visual Studio C++ Express 2010
xlent. Go start it up and create a new C++ project.
I have a project open, and the basic Hello World function set up. #include <iostream> using namespace std; int main(void) { return 0; }
minus the hello world part
xlent. inside the main function, you can declare two integer arrays of size 1, one to hold the value of the total and another to count the number of values entered by the user. int total[1]; int count[1];
Alrighty, so far so good!
I also think another 'temp' size 1 array would be useful
What do you mean? And why?
we need a variable to accept input from the user.
our source would look like this after declaring and initializing the arrays. #include <iostream> using namespace std; int main() { int temp[1]; int total[1]; int count[1]; *temp = 0; *total = 0; *count = 0; return 0; } after that, we can use cin to accept input from the user
I see, that makes sense. Thank you so much!
O.o Wait a minute, why are you using single-element arrays? If you want pointers, use pointers. int temp, total, count; temp = 0; total = 0; count = 0; average(&total, &count); Going through single-element arrays is just obfuscating what you really want to do ;)
Well, the question said only pointers :(
Ah. Fair point. Also a terrible and unclear requirement. Fair enough.
Join our real-time social learning platform and learn together with your friends!