An Introduction To Programming Through C++ Assignment 7 Answers

NPTEL An Introduction To Programming Through C++ Assignment 7 Answers 2022:-In This article, we have provided the answers of An Introduction To Programming Through C++ Assignment 7. You must submit your assignment to your own knowledge.

What is An Introduction to Programming Through C++?

This course provides an introduction to problem-solving and programming using the C++ programming language. The topics include:

  • Basic programming notions. Control flow, variables and assignments statements, conditional execution, looping, function calls including recursion. Arrays and structures. Elementary aspects of classes. Heap memory. 
  • Program design. How human beings solve problems manually. Strategies for translating manual strategies to computer programs. Organizing large programs into units such as functions and classes. Introduction to assertions and invariants.
  • Programming applications. Arithmetic on polynomials, matrices. Root finding. Sorting and searching. Design of editors and simulators, including graphical editors. Elementary animation. A rudimentary graphics system will be discussed.
  • Standard Library of C++. The string, vector and map classes.

CRITERIA TO GET A CERTIFICATE

Average assignment score = 25% of the average of best 8 assignments out of the total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF THE AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met, you will not get the certificate even if the Final score >= 40/100.

An Introduction To Programming Through C++Answers
Assignment 1Click Here
Assignment 2Click Here
Assignment 3Click Here
Assignment 4Click Here
Assignment 5Click Here
Assignment 6Click Here
Assignment 7Click Here
Assignment 8Click Here
Assignment 9NA
Assignment 10NA
Assignment 11NA
Assignment 12NA

NPTEL An Introduction to Programming Through C++ Assignment 7 Answers 2022:-

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 Suppose we wish to know if 3 numbers in a set S add up to a certain target value T. The program below is meant to determine this, by going over all triples of numbers in S. It should check all triples, but not check any triple more than once. int main(){    int n; cin >> n; int S[n]; for(int i=0; i<n; i++) cin >> S[i]; int T; cin >> T; bool addsToT = false; for(int i=0; i<blank1; i++)    for(int j=0; j<blank2; j++)       for(int k=0; k<blank3; k++)           if(S[i] + S[j] + S[k] == T) addsToT = true; cout << addsToT << endl; } Give the simplest answers for the questions below. Do not use quotes or unnecessary spaces.

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)

Programming Assignment Answers:-

Q1. 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.

Code:-

main_program 
{
int n,m, a[10],d[m],i, 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;
}
  

Q2. 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 i, j,max, count [max], c=1, val;

max=val=arr[0]; 
  for(i=0;i<n;i++) 
  {

if(max<arr[i]) 
  	max=arr[i];

}

for(i=0;i<n;i++)

{ 
  count[arr[i]]=1;

}

for(i=0;i<n-1;i++)

{ 
  for(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;
}

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge do your assignment on your own.

For More NPTEL Answers:- CLICK HERE

Join Our Telegram:- CLICK HERE

An Introduction To Programming Through C++ Assignment 7 Answers 2022:- In This article, we have provided the answers of An Introduction To Programming Through C++ Assignment 7 You must submit your assignment to your own knowledge.

Leave a Comment