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 am trying to write a python code to create a function that returns all possible...
I am trying to write a python code to create a function that returns all possible dimensions (reshaping) for an input matrix, can someone explains the math and write a python code with explaining all the steps and give me a test cases, and please explain if there is more than one approach. so if we have 1*8 matrix we should return 1*8, 2*4,4*2,8*1 def reshaping(M) should return a n ORDERED list AND the all possible reshaped matrices
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...
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)) }
I need this code to be written in Python: Given a dataset, D, which consists of...
I need this code to be written in Python: Given a dataset, D, which consists of (x,y) pairs, and a list of cluster assignments, C, write a function centroids(D, C) that computes the new (x,y) centroid for each cluster. Your function should return a list of the new cluster centroids, with each centroid represented as a list of x, y: def centroid(D, C):
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT