samedi 31 août 2019

How to change python code for creating arrays over a range combined with if/else statements to C?

I have code previously done in python that I need to redo in C but im not sure how exactly to go about it. First section is the python code I need to convert to C. Second section is what I have already re-written in C. Third contains the entire original python code if needed.

Has to use a for loop.

for i in range (12):
    x = (int(input("Enter site 3 monthly expenditure: ")))
    site_month.append (x)
    if x < int(monthly_target):
        print ("This site is Under the organization’s monthly target!")
        target.append ("Under")
    elif x == int(monthly_target):
        print ("This site is Exactly the organization’s monthly target!")
        target.append ("Exactly")
    else:
        print ("This site is Over the organization's monthly target!" )
        target.append ("Over!")

#include <stdio.h>
int main()


{
    int annual_target = 35500;
    int monthly_target = (annual_target/12);
    int expenditure_month [12];
    int loop;
    char *month[12];
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";

    char target [12] ={};

   for(loop = 0; loop < 12; loop++)
      printf("%s %d %s  \n", month[loop]);}

#annual expediture target
annual_target = 35500
#monthly expenditure target found by dividing the annual target
monthly_target = (annual_target/12)
print ("Monthly Expenditure Target: ", monthly_target)

#creates an array to hold the monthly expenditures
site_month = []
#creates an array for the 12 months entered represented by numbers using y as a base entry of zero before the first entry is input
month = []
y=0
#creates an array based on how the entered expenditure compares to the monthly target.
target = []

#for loop that continues asking for monthly expenditures till all 12 month slots are filled and uses an if/else statement
#depending on how the entered expenditure compares to the given target expenditure
for i in range (12):
    x = (int(input("Enter site 3 monthly expenditure: ")))
    site_month.append (x)
    if x < int(monthly_target):
        print ("This site is Under the organization’s monthly target!")
        target.append ("Under")
    elif x == int(monthly_target):
        print ("This site is Exactly the organization’s monthly target!")
        target.append ("Exactly")
    else:
        print ("This site is Over the organization's monthly target!" )
        target.append ("Over!")
#uses for loop to print individual cells of 3 different arrays in a 3 column format   
for a,b,c in zip(month, site_month, target):
     print (a, b, c)

Aucun commentaire:

Enregistrer un commentaire