Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

An Introduction to Programming through C++ | Week 5 Programming Assignment | Jan-2022 | NPTEL


 NPTEL » An Introduction to Programming Through C++



Programming Assignment 5.1

Due on 2022-03-03, 23:59 IST
Write a function that takes a number b, and a number N, and returns true/false indicating whether N is a power of b, ie bk=N for some non negative integer k. The function returns true if N can be expressed as a power of b, and false otherwise.
Use the following function signature :
bool isPower(int b,int N).
The following code will be there, but won’t appear in your editor :
main_program{
int b,N;
cin>>b>>N;
if(isPower(b,N))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
This invisible code will test your function. This code will read the values b and N from the test case and print out “Yes” if your function returns true and “No” otherwise.

You don’t need to write/copy the main program. You just need to write the isPower function, without the main_program or any of the header files.

Note :
 Ensure that the name of the function is isPower and it takes the two parameters and returns true or false without printing anything.

Explanation of Sample Testcase 1
Input : b=2, N=1024
Output : 1024 can be expressed as 210. So the function returns true and Yes gets printed out by
the invisible code.
Your last recorded submission was on 2022-02-27, 20:56 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~

bool isPower(int b,int N)
{
  while(N!=1)
  {
    if(N%b!=0)
      return false;
    else
      N/=b;
  }
  return true;
}
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   

 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2 1024
Yes\n
Yes\n
Passed
Test Case 2
2 100
No\n
No\n
Passed
Test Case 3
5 625
Yes\n
Yes\n
Passed
Test Case 4
5 55
No\n
No\n
Passed



Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed





Programming Assignment 5.2

Due on 2022-03-03, 23:59 IST
Write a void function that takes two value parameters : a and b, and two reference parameters :
q and r. It performs the integer division of a by b, and stores the quotient in q and the remainder in r. All the values : a, b, q and r can be stored in the int data type.
Use the following function signature :
void div(int a, int b, int &q, int &r).
The following code will be there, but won’t appear in your editor :
main_program{
int a,b,q,r;
cin>>a>>b;
div(a,b,q,r);
cout<<q<<" "<<r<<endl;
}
This invisible code will test your function. This code will read the values a and b from the test case, call your div function and will print out the modified values of q and r.

You don’t need to write/copy the main program. You just need to write the div function, without the main_program or any of the header files.

Note : Ensure that the name of the function is div, and it takes the four parameters and stores the quotient and remainder of the division a/b in q and r without printing anything.

Explanation of Sample Testcase 1
Input : a=30, b=7
Output : 30/7 gives 4 as the quotient and 2 as the remainder. After the function returns, contains 4 and r contains 2
Your last recorded submission was on 2022-02-27, 21:15 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
void div(int a, int b, int &q, int &r)
{
  q=a/b;
  r=a%b;
}
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   

 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
30 7

4 2\n
4 2\n
Passed
Test Case 2
25 5
5 0\n
5 0\n
Passed
Test Case 3
54 4
13 2\n
13 2\n
Passed
Test Case 4
100 7
14 2\n
14 2\n
Passed



Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed

See the Quiz Assignment Answers with proof and programs passing test cases.

Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making quality content...

Have a great day !


No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.