how to use map in Go - loops

I want to make a simple program to count debt installments. The requirements are:
Input the debt value
Input how long the installments
The first half of the installment bank interest is 11% and the rest are 8%
Must use maps
Here's my code
package main
import "fmt"
func main() {
fmt.Print("Input the debt value : ")
var debt int
fmt.Scanln(&debt)
fmt.Print("Input how long the installments : ")
var installment int
fmt.Scanln(&installment)
fmt.Println("====================================================")
fmt.Println("Total debt : ", debt)
fmt.Println("Installments : ", installment)
fmt.Println("====================================================")
var firstHalf = installment / 2
var pay int
for i := 1; i <= installment; i++ {
value := map[string]int{
"month": i,
"payment": pay,
}
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
fmt.Println(value["month"],"Installment with bank interest (11%) is", value["payment"])
} else {
pay = (debt / installment) + (debt * 8 / 100)
fmt.Println(value["month"],"Installment with bank interest (8%) is", value["payment"])
}
}
}
If I run the code and for example :
The debt is 10.000.000
The installments are 7 months
Here's the output :
1 Installment with bank interest (11%) is 0
2 Installment with bank interest (11%) is 2528571
3 Installment with bank interest (11%) is 2528571
4 Installment with bank interest (8%) is 2528571
5 Installment with bank interest (8%) is 2228571
6 Installment with bank interest (8%) is 2228571
7 Installment with bank interest (8%) is 2228571
I don't know why the first index is always 0, even the next calculation is right. So, I guess that either I am using the wrong syntax or I am trying to do something that can not be done. Maybe most likely experienced people will see right away what is wrong.

if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
value["payment"] = pay
fmt.Println(value["month"],"Installment with bank interest (11%) is",
value["payment"])
} else {
pay = (debt / installment) + (debt * 8 / 100)
value["payment"] = pay
fmt.Println(value["month"],"Installment with bank interest (8%) is",
value["payment"])
}

It is printing the the payment value of map as 0 because it is assigned with pay which has no value initially.You can fix this by declaring the map beneath the if else condition and then print your values,here is the modified code for the same logic:
package main
import "fmt"
func main() {
fmt.Print("Input the debt value : ")
var debt int
fmt.Scanln(&debt)
fmt.Print("Input how long the installments : ")
var installment int
fmt.Scanln(&installment)
fmt.Println("====================================================")
fmt.Println("Total debt : ", debt)
fmt.Println("Installments : ", installment)
fmt.Println("====================================================")
var firstHalf = installment / 2
var pay int
for i := 1; i <= installment; i++ {
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
} else {
pay = (debt / installment) + (debt * 8 / 100)
}
value := map[string]int{
"month": i,
"payment": pay,
}
if i <= firstHalf {
fmt.Println(value["month"], "Installment with bank interest (11%) is", value["payment"])
} else {
fmt.Println(value["month"], "Installment with bank interest (8%) is", value["payment"])
}
}
}
Output:
Input the debt value : 1000
Input how long the installments : 5
====================================================
Total debt : 1000
Installments : 5
====================================================
1 Installment with bank interest (11%) is 310
2 Installment with bank interest (11%) is 310
3 Installment with bank interest (8%) is 280
4 Installment with bank interest (8%) is 280
5 Installment with bank interest (8%) is 280

Related

Parking fee using C

Beginner here. I just wanted to know how and where I went wrong with this code. I’m having problem with using the modulo for the time. The following is the problem and the code I created.
A company wants to create a system that automatically computes for the total fee of the parking services in their new mall.
The user first needs to input their vehicle type, time-in and time-out. Then, it will compute for the total fee of the parking service based on the time the vehicle spent parked. Use the table below as basis:
Vehicle Type First 3 hours After 3 hours
M-otorcycle FREE PHP 10.00/hr
C-ar PHP 30.00 PHP 10.00/hr
T-ruck PHP 50.00 PHP 25.00/hr
Assumptions:
User will not input a value wherein Timeout < Timein.
Max Inputs will for time will be from 0000 to 2359, and it will always follow that form (user will not input values like 13, 17, 110).
Input Format
Vehicle Type, Time-in, and Time-out
Input Sample
M
1230
1430
Output Format
Payment, hours and minutes in this format.
Output Sample
Payment:·0.00
Hours:·2
Minutes:·0
Code:
#include<stdio.h>
int main()
{
double payment;
int time, entry,exit;
int hours, minutes;
char type;
scanf("%c %d %d", &type, &entry, &exit);
hours = (exit-entry)/100;
minutes = (exit-entry) %60;
if (type == 'M' && time > 3)
payment = hours*10;
else
payment = hours*0;
if (type=='C' && time >= 3)
payment = ((hours-3)*10)+30;
else
payment = 30;
if (type == 'T' && hours >= 3)
payment = ((hours-3)*25)+50;
else
payment = 50;
printf("Payment: %.2f \nHours: %d \nMinutes: %d ", payment, hours, minutes);
return 0;
}
Can u send the table clearly.
And also,
minutes = abs((exit/100) - (entry/100));
No use of modulus!

Why does the sum of calendar days from 1. AD to 1.1.2001 differ from tropical days by 3?

As a little background: in October 1582 the Gregorian calendar was introduced to correct problems with the Julian calendar, which was in use until then. The Gregorian calendar specifies the length of a year more precisely, adds a new leap year calculation and removes 10 days from the Julian calendar on transition (moving from 4.Oct.1582 to 15.Oct.1582)
// Julian leap year calculation
((year % 4) == 0)
// Gregorian leap year calculation
((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0)
To sum up all calendar days from 1 AD to 2001 AD I use:
#include <stdio.h>
int main( void)
{
int year;
int daysInYear;
int total;
total = 0;
for( year = 1; year <= 2001; year++)
{
//
daysInYear = 365;
if( year < 1582)
daysInYear += ((year % 4) == 0);
else
if( year > 1582)
daysInYear += ((year % 4) == 0 && (year % 100) != 0) || ((year % 400) == 0);
else
daysInYear -= 10; // 1582 exactly
total += daysInYear;
printf( "%d: +%d = %d\n", year, daysInYear, total);
}
return( 0);
}
Now running this exhaustively over all years from 1 to 2000 gives this pastebin: https://pastebin.com/bR7hwNr1
The most interesting bits show that the leap year and day-skip calculation is correct and also produces the result:
1: +365 = 365
2: +365 = 730
3: +365 = 1095
4: +366 = 1461
...
100: +366 = 36525
...
400: +366 = 146100
...
1581: +365 = 577460
1582: +355 = 577815
1583: +365 = 578180
...
1900: +365 = 693962
...
1996: +366 = 729026
1997: +365 = 729391
1998: +365 = 729756
1999: +365 = 730121
2000: +366 = 730487
Now the question is this: the 1.1.2001 is at 730487 elapsed calendar days. Given the length of the year as defined by the Gregorian calendar as 365.2425, I would expect to see 2000*365.2425 = 730485 days. If one uses tropical days or solar days it would be 730484. But 740487 is clearly out of range.
Assuming that no-one miscounted any days in the Gregorian Calendar range, there are some extra days in the Julian Calendar, that don't match with the tropical days. But the Gregorian was created to correct the Julian calendar and I don't think they would have made that large an error in 1582.
This could be more a historical question than a programming question I would guess.
The goal of the Gregorian reform of the calendar was to reset the date of the northern hemisphere vernal equinox to March 21, and keep it there. This date was chosen because that was the approximate average date of the equinox at the time of the Council of Nicaea in AD 325. At the time of the council, the date of the equinox had slipped from about March 25 in AD 1.
For all the details read the book Gregorian Reform of the Calendar which is the proceedings of a conference held by the Vatican Observatory and published in 1983.
To clarify, when evaluating the Gregorian calendar compared to the Julian, using a mix of Julian and Gregorian dates incorporates the one-time correction of 10 days which was intended to correct for accumulated excess leap years from about AD 325 to AD 1582. If one wishes to use some start date other than AD 325, the entire calculation should use ONLY Gregorian dates or ONLY Julian dates.

Monthly Auto Loan Payment Amount

I am trying to calculate an auto loan monthly payment amount of the front end using angularJS. My calcualtions are as follow and im not getting the right amount, the Results are returning 4
$scope.model.autoLoan.borrowAmount = 15000
$scope.model.autoLoan.interestRate = 3.25
$scope.model.autoLoan.loanLength = 60
var monthlyPayment = $scope.model.autoLoan.borrowAmount *
(($scope.model.autoLoan.interestRate / 12) *
(Math.pow((1 + ($scope.model.autoLoan.interestRate / 12)), $scope.model.autoLoan.loanLength))) /
(Math.pow((1 + ($scope.model.autoLoan.interestRate / 12)), $scope.model.autoLoan.loanLength) - 1);
console.log(monthlyPayment);
the console returns: 4062.502309286213 which should be retrunging something like 47.00. Not sure what I'm doing wrong here.

Insights on approaching fare calculations

Basically I'm attempting a question on how to compute fee. However, my code is extremely long. I ended up using a heck tons of 'if' and 'else' for my code. I was wondering if there is any better way on approaching this question.
I initially attempted to use loops to keep stacking the fee but I face a problem when my time cross the critical juncture. I added time to TimeIn along with my fees, but as it crosses the 7am mark, I need to reset it back to 7am so that my fees will be charge properly. And that was when I gave up when my code became very long too.
Eg. loop adds from 0630 to 0730, fees are properly increased, the first 30 minutes of 0700 will be skipped and fees are wrongly charged.
The question I attempted:
Mr. Wu has been going to work every day by taxi for many years. However, the taxi fare has been increasing rather quickly in recent years. Therefore, he is considering driving to work instead.
One of the costs for driving is the parking fee. The parking rates of the car park at Mr. Wu’s workplace are as shown in the table below.
Weekday Saturday Sunday
4am ~ 7am $2.00 per hour $2.50 per hour $5
7am ~ 6pm $1.20 per 30 minute $1.50 per 30 minutes per
6pm ~ midnight $5.00 per entry $7.00 per entry entry
Special note:
1. The car park opens at 4am and closes at midnight. All vehicles must leave
by midnight.
2. There is a grace period of 10 minutes on any day (i.e., it is completely
free to park for 10 minutes or less regardless of day and time.)
3. There is a 10% surcharge for parking more than 10 hours on a weekday and
20% for Saturday. There is no surcharge for Sunday.
4. There is an additional $3.00 fee for exiting after 10pm on any day.
(Surcharge is not applicable on this fee.)
Your program should read in one integer, which is an integer between 1 and 7
representing the day of the week (1 being Monday and 7 being Sunday). It should also
read in two numbers representing the time-in and time-out in 24-hour format. It
should then calculate and display the parking fee (with two decimal places).
You may assume that the inputs are valid (i.e., the day is within the specified range,
both time-in and time-out are between 4am and midnight in 24-hour format, and timeout
is no earlier than time-in).
My extremely long code:
#include <stdio.h>
#include <math.h>
double computeFee(int, int, int);
int main(void){
int day, timeIn, timeOut;
scanf("%d %d %d", &day, &timeIn, &timeOut);
printf("Enter day: %d\n", day);
printf("Enter time-in: %d\n", timeIn);
printf("Enter time-out: %d\n", timeOut);
printf("Parking fee is $%.2lf\n", computeFee(day, timeIn, timeOut));
return 0;
}
double computeFee(int day, int timeIn, int timeOut){
double fee = 0;
double TimeIn, TimeOut;
TimeIn = 60*(floor(timeIn/100)) + (timeIn%100);
TimeOut = (60*(floor(timeOut/100)) + (timeOut%100));
if((day>=1)&&(day<=5)){
if(TimeIn<420){
if(TimeOut<420){
fee += 2*ceil((TimeOut - TimeIn)/60);
}
else{
fee += 6;
if(TimeOut>=1080){
fee += 31.4;
}
else{
fee += 1.2*ceil((TimeOut - 420)/30);
}
}
}
if(TimeIn>=420){
if(TimeIn>=1080){
fee = 5;
}
else{
if(TimeOut>=1080){
fee += (1.2*ceil((1080 - TimeIn)/30) + 5);
}
else{
fee += (1.2*ceil((TimeOut - TimeIn)/30));
}
}
}
if((TimeOut-TimeIn)>=600){
fee *= 1.1;
}
}
if(day == 6){
if(TimeIn<420){
if(TimeOut<420){
fee += (2.5*ceil((TimeOut - TimeIn)/60));
}
else{
fee += 7.5;
if(TimeOut>=1080){
fee += 40;
}
else{
fee += (1.5*ceil((TimeOut - 420)/30));
}
}
}
if(TimeIn>=420){
if(TimeIn>=1080){
fee = 7;
}
else{
if(TimeOut>=1080){
fee += (1.5*ceil((1080 - TimeIn)/30) + 7);
}
else{
fee += (1.5*ceil((TimeOut - TimeIn)/30));
}
}
}
if((TimeOut-TimeIn)>=600){
fee *= 1.2;
}
}
if(day == 7){
fee = 5;
}
if((timeOut/100)>=22){
fee += 3;
}
if((timeOut - timeIn)<=10){
fee = 0;
}
return fee;
}
Examples on how fees are calculated:
Example 1: Tuesday, 4:29am to 7:50am.
• 4:29am to 7am is charged as 3 1-hour slots: $2.00 * 3 = $6.00
• 7am to 7:50am is charged as 2 30-minute slots: $1.20 * 2 = $2.40
• Total fee = $6.00 + $2.40 = $8.40
Example 2: Saturday, 7:01am to 7:49pm.
• 7:01am to 6pm is charged as 22 30-minute slots: $1.50 * 22 = $33.00
• 6pm to 7:49pm is charged as one entry: $7.00
• 20% Surcharge for parking more than 10 hours: ($33.00 + $7.00) * 20% =
$8.00
• Total fee = $33.00 + $7.00 + $8.00 = $48.00
Example 3: Sunday, 3pm to 10:01pm.
• 3pm to 10:01pm is charged as one entry: $5.00
• Additional fee for exiting after 10pm: $3.00
• Total fee = $5.00 + $3.00 = $8.00
Example 4: Thursday, 11:49pm to 11:59pm.
• Grace period
• Total fee = $0.00
Example 5: Monday, 12pm to 10:01pm.
• 12pm to 6pm is charged as 12 30-minute slots: $1.20 * 12 = $14.40
• 6pm to 10:01pm is charged as one entry: $5.00
• 10% Surcharge for parking more than 10 hours: ($14.40 + $5.00) * 10% =
$1.94
• Additional fee for exiting after 10pm: $3.00
• Total fee = $14.40 + $5.00 + $1.94 + $3.00 = $24.34
Thanks for reading my long question. And thanks in advance for the help.
note: I havent learn arrays and anything beyond that. Only learn loops and selection statement so far(reading K&R programming tutorial, until chapter 17, using Online GeekforGeek as compiler). However, I will still greatly appreciate solutions using other methods.

Find highest earning company from while loop

I need to find the company with the highest earnings.
So far I am able to pull out the highest total Earnings from the loop but have no idea how to get the company name that ties with this highest earnings.
while( count <= noOfComp)
{
System.out.print("Enter Company: ");
companyName = kb.nextLine();
System.out.print("Number of hires: ");
noOfHires = kb.nextInt();
kb.nextLine();
//calculations
totalEarnings = noOfHires * 2500 + 10000;
System.out.println("Total Earnings of company is :" + totalEarnings);
totalEarned = "" + totalEarnings;
if(totalEarnings > large ) //If statement for largest number
{
large = totalEarnings;
}
allTotalEarnings += totalEarnings;
count++;
}
You can assign the highest earning company name and its earnings in variables after the calculation by comparing with previous highest with calculated one.
String highCompanyName = "";int highCompanyEarning = 0;
while( count <= noOfComp)
{
System.out.print("Enter Company: ");
companyName = kb.nextLine();
System.out.print("Number of hires: ");
noOfHires = kb.nextInt();
kb.nextLine();
//calculations
totalEarnings = noOfHires * 2500 + 10000;
System.out.println("Total Earnings of company is :" + totalEarnings);
totalEarned = "" + totalEarnings;
if(totalEarnings> highCompanyEarning ) //If statement for largest number
{
highCompanyEarning = totalEarnings;
highCompanyName = companyName;
}
allTotalEarnings += totalEarnings;//the purpose of it is not clear so left as it is.
count++;
}
System.out.println("Highest Earnings company is :" + highCompanyName );
System.out.println("Earning of that company is :" + highCompanyEarning );

Resources