# An Introduction to Programming Through C++ Week 7 Answers

### Assignment 7

**Q1.** Consider the line of code

int x[4] = {10, 20, 30, 40};

What is the value of x[3]?

**Answer:** 40

**Q2.** Suppose students can obtain any (integer) mark from 0 to 100. I would like a histogram giving the number of students getting marks in the range 0-4, 5-9, 10-14, … 95-99, 100. What size array do I need to store the histogram?

**Answer:** 21

**Q3.** Consider the following program fragment: int a[10]; for(int i=0; i<10; i++) cin >> a[i]; bool s=true; for(int i=0; i<9; i++) if(a[i] < a[i+1]) s = false; cout << s; Which of the following are true?

a) The program outputs true if elements of a are in increasing order, i.e. a[i] < a[i+1] for i=0..8

b) The program outputs true if elements of a are in decreasing order, i.e. a[i] > a[i+1] for i=0..8

c) The program outputs true if elements of a are in non-increasing order, i.e. a[i] >= a[i+1] for i=0..8

d) The program always outputs true if elements of a are in non-decreasing order, i.e. a[i] <= a[i+1] for i=0..8

**Answer:** b)

**Q4.** Consider the following function which is supposed to return true if all the numbers in an array A are even and false otherwise: bool allEven(int a[], int n){ int i=0; for(i=0; i<n; i++) if(a[i] %2 != 0) break; if(i == blank) return true; else return false; } What should be in place of blank? Answer without any quotes or spaces.

**Answer:** n

**Q5.** What should blank1 be?

**Answer:** n

**Q6.** What should blank2 be?

**Answer:** i

**Q7.** What should blank3 be?

**Answer:** j

**Q8.** Suppose that we wanted the program to print whether T is obtained by taking the sum of 3 numbers chosen from S with replacement, i.e. the same number can be used several times. The code above will work, but the blanks will need to be filled possibly differently. What should blank2 be for this case?

**Answer:** n Consider the following code fragment. int q[4]={25, 26, 27, 28}; cout << q[0] << endl; cout << &q[2] << endl; cout << q << endl; q[0] = 6; q = 1600; Assume q[0] is stored at address 1500. Also assume that each int is 4 bytes wide, so q[1] will be stored at 1504, q[2] at 1508 and so on. Note that normally when you print an address, it will get printed in hexadecimal; but just for the exercises below assume that addresses are also printed in decimal.

**Q9.** State what value is printed by the statement in line 2.

**Answer:** 25

**Q10.** State what value is printed by the statement in line 3.

**Answer:** 1508

**Q11.** State what value is printed by the statement in line 4.

**Answer:** 1500

**Q12.** Which of the following statements are true?

a) Line 5 contains a valid C++ statement.

b) Line 6 contains a valid C++ statement.

**Answer:** a)

### Programing Assignment 7.1

Write a program that keeps track of the money in your wallet. As an example, suppose only notes of denominations 2000, 500, 200, 100, 50,20, 10 are in use. And suppose you have respectively 2, 5, 8, 4, 1, 1, 1 notes of the denominations. Thus you have a total of 4000 + 2500 + 1600+ 400 + 50 + 20 + 10 = 8580 rupees

```
main_program {
int n, m, i;
int a[10], d[m], sum = 0, rup, max = 0;
char com;
cin>>n;
for(i=0; i<n; i++) {
cin>>a[i];
}
m = a[i-1];
for(i=1; i<=m; i++) {
d[i] = 0;
}
for(i=0; i<n; i++) {
cin>>d[a[i]];
sum = sum + a[i] * d[a[i]];
}
while(1) {
cin>>com;
switch(com) {
case 'P': cout<<sum<<endl;
break;
case 'S': cin>>rup;
if(d[rup]>0) {
d[rup]--;
sum = sum - rup;
}
break;
case 'E': exit(1);
}
}
return 0;
}
```

### Programming Assignment 7.2

Write a function that takes an array of integers as an argument (in the usual manner, so two arguments including the length), and returns the mode of the elements in the array, i.e. an integer that occurs the maximum number of times. If there are several integers that occur the maximum number of times, then you should return that integer whose first appearance in the array is at the largest index. So for example, if the array contains 9, 8, 3, 4, 2, 4, 2, 9, 7, 4, 9, then there are 2 integers, 4 and 9 that appear the maximum number of times, i.e. 3 times. The first appearance of 9 is at index 0, while the first appearance of 4 is at index 3. Thus the function should return 4.

**CODE:**

```
int mode(int arr[], int n) {
int max;
max = arr[0];
int count[max];
int c=1, val=arr[0];
for(int i=0; i<n; i++) {
if(max < arr[i])
max = arr[i];
}
for(int i=0; i<n; i++)
count[arr[i]]=1;
for(int i=0; i<n-1; i++) {
for(int j=i+1; j<n; j++) {
if(arr[i]==arr[j]) {
count[arr[i]]++;
if(c <= count[arr[i]]) {
c = count[arr[i]];
val = arr[i];
}
}
}
}
if(c == 1)
val = arr[n-1];
return val;
}
```

The content uploaded on this website is for reference purposes only. Please do it yourself first. |