Question

In: Computer Science

this is a python code that i need to covert to C++ code...is this possible? if...

this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++?


def main():
  endProgram = 'no'
  print
  while endProgram == 'no':
    print
    # declare variables
    notGreenCost = [0] * 12
    goneGreenCost = [0] * 12
    savings = [0] * 12
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
 
    getNotGreen(notGreenCost, months)
    getGoneGreen(goneGreenCost, months)
    energySaved(notGreenCost, goneGreenCost, savings)
    displayInfo(notGreenCost, goneGreenCost, savings, months)
    endProgram = raw_input('Do you want to end program? (Enter no or yes): ')
    while not (endProgram == 'yes' or endProgram == 'no'):
      print 'Please enter a yes or no'
      endProgram = raw_input('Do you want to end program? (Enter no or yes): ')
 
def getNotGreen(notGreenCost, months):
  counter = 0
  while counter < 12:
    print 'Enter NOT GREEN energy costs for', months[counter]
    notGreenCost[counter] = input('Enter now -->')
    counter = counter + 1
  print '-------------------------------------------------'
 
def getGoneGreen(goneGreenCost, months):
  print
  counter = 0
  while counter < 12:
    print 'Enter GONE GREEN energy costs for', months[counter]
    goneGreenCost[counter] = input('Enter now -->')
    counter = counter + 1
  print '-------------------------------------------------'
 
def energySaved(notGreenCost, goneGreenCost, savings):
  counter = 0
  while counter < 12:
    savings[counter] = notGreenCost[counter] - goneGreenCost[counter]
    counter = counter + 1
 
def displayInfo(notGreenCost, goneGreenCost, savings, months):
  counter = 0
  print
  print '                        SAVINGS                      '
  print '_____________________________________________________'
  print 'SAVINGS     NOT GREEN     GONE GREEN       MONTH'
  print '_____________________________________________________'
 
  while counter < 12:
    print
    print '$', savings[counter], '         $', notGreenCost[counter], '         $', goneGreenCost[counter], '         ', months[counter] 
    counter = counter + 1
  print 
 
main()

Solutions

Expert Solution

// C++ code
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

void getNotGreen(int notGreenCost[], string months[])
{
int counter = 0;
while (counter < 12)
{
cout << "Enter NOT GREEN energy costs for " << months[counter] << endl;
cout << "Enter now --> ";
cin >> notGreenCost[counter];
counter = counter + 1;
}
cout << "-------------------------------------------------\n";
}
  


void getGoneGreen(int goneGreenCost[], string months[])
{
int counter = 0;
while (counter < 12)
{
cout << "Enter GONE GREEN energy costs for " << months[counter] << endl;
cout << "Enter now --> ";
cin >> goneGreenCost[counter];
counter = counter + 1;
  
}
cout << "-------------------------------------------------\n";
}


void energySaved(int notGreenCost[],int goneGreenCost[], int savings[])
{
int counter = 0;
while (counter < 12)
{
savings[counter] = notGreenCost[counter] - goneGreenCost[counter];
counter = counter + 1;
}
}
  


void displayInfo(int notGreenCost[], int goneGreenCost[], int savings[] , string months[])
{

int counter = 0;
cout << "\n";
cout << " SAVINGS \n";
cout << "_____________________________________________________\n";
cout << "SAVINGS NOT GREEN GONE GREEN MONTH\n";
cout << "_____________________________________________________\n";


while (counter < 12)
{
cout << "\n";
cout << "$" << savings[counter] << " $" << notGreenCost[counter] << " $" << goneGreenCost[counter] << " " << months[counter];
counter = counter + 1;
}

cout << endl;
}


int main()
{
string endprogram = "no";

while(endprogram == "no")
{
int notGreenCost[12] = {0};
int goneGreenCost[12] = {0};
int savings[12] = {0};
string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  
getNotGreen(notGreenCost, months);
getGoneGreen(goneGreenCost, months);
energySaved(notGreenCost, goneGreenCost, savings);
displayInfo(notGreenCost, goneGreenCost, savings, months);

cout << "Do you want to end program? (Enter no or yes): ";
cin >> endprogram;

while(endprogram != "yes" || endprogram != "no")
{
cout << "Please enter a yes or no\n";
cout << "Do you want to end program? (Enter no or yes): ";
cin >> endprogram;
}

}
}


/*
output:

Enter NOT GREEN energy costs for January
Enter now --> 12
Enter NOT GREEN energy costs for February
Enter now --> 23
Enter NOT GREEN energy costs for March
Enter now --> 32
Enter NOT GREEN energy costs for April
Enter now --> 11
Enter NOT GREEN energy costs for May
Enter now --> 22
Enter NOT GREEN energy costs for June
Enter now --> 12
Enter NOT GREEN energy costs for July
Enter now --> 11
Enter NOT GREEN energy costs for August
Enter now --> 45
Enter NOT GREEN energy costs for September
Enter now --> 33
Enter NOT GREEN energy costs for October
Enter now --> 23
Enter NOT GREEN energy costs for November
Enter now --> 22
Enter NOT GREEN energy costs for December
Enter now --> 12
-------------------------------------------------
Enter GONE GREEN energy costs for January
Enter now --> 23
Enter GONE GREEN energy costs for February
Enter now --> 32
Enter GONE GREEN energy costs for March
Enter now --> 11
Enter GONE GREEN energy costs for April
Enter now --> 10
Enter GONE GREEN energy costs for May
Enter now --> 12
Enter GONE GREEN energy costs for June
Enter now --> 34
Enter GONE GREEN energy costs for July
Enter now --> 54
Enter GONE GREEN energy costs for August
Enter now --> 33
Enter GONE GREEN energy costs for September
Enter now --> 22
Enter GONE GREEN energy costs for October
Enter now --> 21
Enter GONE GREEN energy costs for November
Enter now --> 12
Enter GONE GREEN energy costs for December
Enter now --> 10
-------------------------------------------------

SAVINGS
_____________________________________________________
SAVINGS NOT GREEN GONE GREEN MONTH
_____________________________________________________

$-11 $12 $23 January
$-9 $23 $32 February
$21 $32 $11 March
$1 $11 $10 April
$10 $22 $12 May
$-22 $12 $34 June
$-43 $11 $54 July
$12 $45 $33 August
$11 $33 $22 September
$2 $23 $21 October
$10 $22 $12 November
$2 $12 $10 December


Do you want to end program? (Enter no or yes): not
Please enter a yes or no
Do you want to end program? (Enter no or yes): yes

*/


Related Solutions

I need to write a c/c++ code to find the maximum sum in those possible combinations...
I need to write a c/c++ code to find the maximum sum in those possible combinations of arrays. There are N arrays with S elements. ex2: 4 arrays with 3 elements.We want to find the greater number in the same column of two arrays(may possible be three or four...arrays), and sum those greater number to find the greatest sum in all of the combinations in those 4 arrays, like: A: [50, 60, 70] B: [80, 40, 20] C: [30, 100,...
I need to add this checkpoint to an existing code that i have in python Checkpoint...
I need to add this checkpoint to an existing code that i have in python Checkpoint 1: Once you have created and tested the Bank Account Class create subclasses to the BankAccount class. There should be two subclasses, CheckingAccount and SavingsAccount. You should add interest_rate to the parent BankAccount class. To the CheckingAccount add variables called per_check_fee default to false and allow_overdraft default to True. To SavingsAccount add the variable transactions_per_month, default it to 5. Create instances of CheckingAccount and...
I need the code in python where I can encrypt and decrypt any plaintext. For example,...
I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation. Electronic Code Block Mode (ECB) Cipher block Mode (CBC) Cipher Feedback Mode (CFB) Output feedback Mode (OFB) Counter Mode (CTR) Here is an example, Affine cipher expressed in C. Encryption: char cipher(unsigned char block, char key) { return (key+11*block) } Decryption: char invcipher(unsigned char block, char key) { return (163*(block-key+256)) }
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT