Question

Write a C++ program In this assignment you will complete the definition of two functions that...

Write a C++ program In this assignment you will complete the definition of two functions that implement the repeated squaring algorithm described in the Stamp textbook on pages 98-99. Note that your implementation should not use the pow() function, the powl() functions, or any other built-in exponentiation functions. program4-driver.cpp - This file contains a completed main() function that serves as the driver to parse the command line, pass the values to the powerModN() function, and print the result. Make no changes to this file. powerUtility.h - This file contains the function prototype for the powerModN() function. Make no changes to this file. student4.cpp - This file contains two stubbed-out functions. This is the file where you will put your source code additions.

What would you use instead of the pow() functions, or any other built-in exponentiation functions?

This is what I have so far.

#include <iostream>
#include <math.h>
#include <assert.h>
#include "powerUtility.h"

using namespace std;

// Function Prototypes
void displayUsageMessage(void);


// ##################################################################
int main(int argc, char *argv[])
{
long baseValue;
long exponentValue;
long modulusValue;
long result;

if (argc == 4)
{
baseValue = atoi(argv[1]);
exponentValue = atoi(argv[2]);
modulusValue = atoi(argv[3]);

result = powerModN(baseValue, exponentValue, modulusValue);

cout << endl;
cout << " " << baseValue << '^' << exponentValue << " mod "
<< modulusValue << " = " << result << endl;
cout << endl;

return 0;
} // End if

else
{
displayUsageMessage();
return 1;
} // End else

return 0;
} // End main


// ##################################################################
void displayUsageMessage(void)
{
cout << endl;
cout << "Usage: a.exe <base value> <exponent value> <modulus value>" << endl;
cout << endl;
} // End displayUsageMessage


#ifndef POWER_UTILITY_HEADER
#define POWER_UTILITY_HEADER

#endif // POWER_UTILITY_HEADER// Function Prototypeslong powerModN(long base, long exponent, long modulus);

#endif


#include "powerUtility.h"

#define MAX_BITS 1000

// Function Prototypes
void binary(long decimalValue, int bits[], int &bitCount);


// ##########################################################
// Implements the repeated squaring algorithm
long powerModN(long base, long exponent, long modulus)
{
int bits[MAX_BITS];
int bitCount;
long result = 0;

binary(exponent, bits, bitCount);


return result;
} // End powerModN


// ##########################################################
// Converts the exponent into its binary number representation
void binary(long decimalValue, int bits[], int &bitCount)
{

} // End binary

0 0
Add a comment Improve this question Transcribed image text
Answer #1

long powerModN(long base, long exponent, long modulus){
   int bits[MAX_BITS];
   int bitCount;   
   long result = 1; //Initial Result is 1 not 0
   binary(exponent, bits, bitCount);
   for(int i = 0;i<bitCount;i++){
       result = (result*result)%modulus; //repeated squaring
       if(bits[i])
       result = (result * base)%modulus;
   }
   return result;
}

void binary(long decimalValue, int bits[], int &bitCount){
   int count = 0;
   long temp = decimalValue;   
   while(temp){
       temp = temp/2; //finding the total number of bits
       count++;
   }
   bitCount = count;
   temp = decimalValue;
   while(temp){ //Finding the bit array
       bits[--count] = temp%2;
       temp = temp/2;
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program In this assignment you will complete the definition of two functions that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the program segment below, write two short functions to complete the program. Use the test...

    Using the program segment below, write two short functions to complete the program. Use the test cases to ensure the program works properly. Prototypes: void int2bin(int, int[8]); The first function will convert an integer that is between 0 - 255 to its binary representation. You are to store the binary number in the array passed to the function. void printBinary(int[8]); The second function will print the binary number stored in the array passed to the function. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Test 1: 13...

  • 1. In ANSII standard C++, there is no library function to convert an integer to a...

    1. In ANSII standard C++, there is no library function to convert an integer to a string. Your program should develop such a function. In other words complete the following program using your itos function. (Use of other C functions is prohibitted) // this program will read in an integer and convert it to a string #include <iostream> #include <cstdlib> #include <string> using namespace std; string itos(int n) { } int main(int argc, char* argv[]){ int n = atoi(argv[1]); cout...

  • Question 19 4 pts Using the program below, please complete the program by adding 2 functions...

    Question 19 4 pts Using the program below, please complete the program by adding 2 functions as follow: 1. A function named getAverage that calculates and returns the Average. 2. A function named getMaximum that returns the maximum of the three numbers. Make sure to use the proper data types for all variables and functions. #include <iostream> using namespace std; //function getAverage //function getMaximum int main(int argc, char** argv) { int x, y, z; cout << "Enter three whole numbers:...

  • For this lab, you will need to write the following functions in table.cpp, and add function...

    For this lab, you will need to write the following functions in table.cpp, and add function prototypes for them to table.h and invoke the functions in main.cpp. * int countNodes(node * root) Recursively count the number of the nodes in the tree. * int sumLeaves(node * root) Recursively sum all of the leaf nodes in the tree. You must use the functions with these exact function prototypes. ****************************************************************************** main.cpp #include "table.h" #include <iostream> using namespace std; int main() { node...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT