Question
Student Name: Marks: Lab Assignment 1 - (4 marks) Part 1 - Using Microsoft Visual C++ Debugger (2 marks) In this lab, we will look
|
Lab Assignment 1 - (4 marks)
Part 1 - Using Microsoft Visual C++ Debugger (2 marks)
In this lab, we will look at tracing variables, and watch how they change value as the program proceeds. We will also get some idea of their scope.
The program called factoral.c is listed at the end of these notes. This program calculates the factorial of a number.
Factorials:
Eight factorial means 87654321 For short this is written as 8! "Five factorial" would be 5! or 54321 = 120.
Some other factorials would be:
4! = 4321 four factorial
3! = 321 Three factorial
2! = 21
1! = 1
We keep multiplying by smaller and smaller integers until we reach one. Since we count down, a negative integer is not allowed. Also 0! is defined as 1 for mathematical reasons.
In Microsoft Visual C++, we will run the Debug facility to trace values of the variables. You will build a "watch table" which can list variables and their values.
First, run the program by choosing Debug\Step Into. One line will be pointed by a yellow arrow at a time. The compiler is about to process this line. You can move through the program line by line by pressing the F11 key. Whenever the control goes into a built-in function, click Debug\ Step Out to go back to the source code.
Next, we have to make a watch table of variables. A variable is added to the watch table by the following steps:
(1) Choose Debug\QuickWatch.
(2) Enter a variable like "num" in the Expression: and click "Add Watch"
(3) Repeat for the other variables: n, k, product and result
(4) Click Close
Now the variables will appear in the watch window Watch1 at the bottom of the screen.
- What is the first line pointed by the yellow arrow?
(2) What is the initial value of num after it is declared?
(3) What is the initial value of result after its declared?
(4) What are the values of the local variables of the function fact() at this point(before calling fact())? (k, n, product)
(5) When the fact() function is called, what happens to the value of num?
(6) What are the initial values of k, n, and product when the function fact() is called?
(7) What happens to n, k, and product when control returns to main() ?
(8) Check what part of the program each variable is defined?
| main() | fact() |
num |
|
|
k,n, product
|
|
|
result |
|
|
Section Completed and Witnessed by Instructor
_______________________________________________________
Part 2(2 marks)
A combination of C(m,n) means how many ways you can pick n objects from m objects. For instance if you had 5 poker cards, and chose 3 of them, then the number of ways of picking them would be C(5,3). The formula is
C(m,n) = m! written as 3 factorials
n! (m-n)!
For the cards we would have:
C(5,3) = 5! = 120 = 120 = 10
3! (5-3)! 62! 12
You could have 10 different combinations.
Write a program that asks for the number of cards to choose from, m, and the number of cards to pick, n. Next calculate the number of combinations by calling the fact function for each factorial.
Section Completed and Witnessed by Instructor
_______________________________________________________
/* Program factoral.c to calculate the factorial of an */
/* integer number entered from the keyboard */
#define _CRT_SECURE_NO_WARNINGS
#include
#include
long fact(int n); /* function prototype */
int main(void){
int num;
long result;
system("CLS");
printf("what is the number for factorial > ");
scanf("%d",&num);
result = fact(num);
printf("%d! is %ld ",num, result);
system("pause");
return 0;
}
long fact (int n)
{
long product = 1; /* records factorial */
int k; /* index */
if ((n == 1) || (n == 0))
/* handle special cases */
return 1;
if (n < 0) return 0;
/* send error message back */
for (k = 1; k <= n; k++)
product *= k;
return product;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started