C++/C++ count down
Expert: Zlatko - 2/4/2010
QuestionHi,
1. I would like to know how to write a C++ program to "count from 10 to-5".
2. The other quest is, how to write a C++ program the includes the following operations in main function:
V= a * b * c
a, b, c and v are integer varibles. write a function to get the values of a, b, and c. Call by reference is required to write the subroutine.
I will appreciate your quick response
AnswerHello
Your basic program skeleton is something like this
#include <iostream> // for input and output routines
using namespace std; // so that you don't have to put std:: in front of cin and cout
int main(void) // this is one form of main, taking no parameters. main always returns int
{
// do you work here
return 0; // return some value
}
For problem 1, your work is a loop. A "for" loop would work fine. You need to declare an integer counter start it at 10 and count down to 5. Use cout to output your numbers. Read your text book and your class notes on how to use a for loop and how to use cout. You need to put some effort into your work.
For question 2, you need a function to get the input. This is one way do it.
// Note how the variables are being passed by reference.
//Their names don't have to match those in main
void getInput(int& x, int& y, int& z)
{
cout << "Input first number ";
cin >> x;
cout << "Input second number ";
cin >> y;
cout << "Input third number ";
cin >> z;
}
In main you declare integer a, b, and c. Then you call getInput for each:
int a;
int b;
int c;
getInput(a,b,c);
int V = a*b*c;
cout << "The answer is " << V << endl;
I am giving you only pieces of the puzzle. You need to put the pieces together yourself. Beware, I have not checked any of my code with a compiler. If you show me an attempt at the work, then I'll be able to help you more.
Best regards
Zlatko