My professor wants us to write a program without using arrays or vectors like this:
Write a program using functions that calculates and prints parking charges for each of the n customers who parked their cars in the garage.
Parking rates:
a parking garage charges a $5.00 minimum fee to park for up to five hours.
the garage charges an additional $0.50 per hour for each hour or part thereof in the excess of five hours
the maximum charge for any given 24hr period is $10.00. Assume that no car parks longer that 24 hours at a time.
You should enter the hours parked for each customer. Your program should print the results in a neat tabular format and should calculate and print the total of your receipts.
The program output should look like this:
car------Hours------Charge
1--------2.00--------$5.00
2--------5.00--------$5.00
3--------5.30--------$5.50
etc.
total: 3---12.30----$15.50
I only managed to get this far:
include <iostream>
include <conio.h>
include <cmath>
include <iomanip>
using namespace std;
double calculate(double);
int main()
{
double hours,charge;
int finish;
double sumhours;
sumhours=0;
finish=0;
charge=0;
int cars;
cars=0;
do
{
cout<<"Enter the number of hours the vehicle has been parked: "<<endl;
cin>>hours;
cars++;
sumhours+=hours;
finish=cin.get();
if(hours>24)
{
cout<<"enter a time below 24hrs."<<endl;
cars--;
sumhours=sumhours-hours;
}
}
while(finish!=EOF);
double total=calculate(hours);
cout<<total<<": "<<(cars-1)<<": "<<sumhours;
while(!_kbhit());
return 0;
}
double calculate(double time)
{
double calculate=0;
double fees;
if(time<=5)
return 5;
if(time>15)
return 10;
time=ceil(time);
fees=5+(.5*(time-5));
return calculate;
}
Since this is homework, here is an algorithm:
1. Print header.
2. Clear running total variables.
3. While not end of file
3.1 read a record.
3.2 print record contents
3.3 add record field values to running total variables (Hint! Hint!)
3.4. end-while
4. print out running total variables.
You may have to do some additional calculations with the running total variables, especially for averages.
Edit 1: Example of a running total variable
int sum = 0; // This is the running total variable.
const unsigned int QUANTITY = 23;
for (unsigned int i = 0; i < QUANTITY; ++i)
{
cout << "Adding " << i << " to sum.\n";
sum += i;
}
cout << "Sum is: " << sum << "\n";
cout.flush();
In this example, the data 'i' is not stored only used. The sum variable is a running total.
Look for similarities in your assignment.
Edit 2: Example of detecting end of input on cin
char reply = 'n';
while (tolower(reply) != 'y')
{
cout << "Do you want to quit? (y/n)";
cout.flush();
cin >> reply;
cin.ignore(1000, '\n'); // Eat up newline.
}
cout << "Thanks for the answer.\n";
cout.flush();
Since you can't use arrays or vectors, I think you should print the parking data for each car as it's being processed. Pseudocode:
While more cars:
Read data for next car
Calculate cost
Print data
Add to running totals
End while
Print totals
On every iteration, generate the relevant output, but don't stream it to std::cout. Instead, stream it to a std::stringstream object. Then, at the end, stream that object to std::cout. The maths can be done simply by maintaining a running accumulation of the input values.
This, of course, assumes that using a std::stringstream is not considered "cheating" in the context of this homework exercise.
You can try storing your values in a linked list structure instead of an array. Linked lists work great for dynamic storage.
Try this tutorial, http://www.cprogramming.com/tutorial/lesson15.html
My suggestion then is to use a recursive method, the method first accepts input, asks if there is any more input. If there is more input, it then calls itself. If there is no more input, it outputs it's current car and then returns a sum that's added so far in a structure.
The only problem with this method is that it would output entered cars in reverse of input, but it would do so without an array or a file to save to.
Related
so I wrote out this c++ program which works, but after submitting it, It was returned back saying it needs to be rewritten since it did not use adjacent_difference, the problem asked is as follows:
The Rinky Dooflingy Company records the number of cases of dooflingies produced each day over a four-week period. Write a program that reads these production levels and stores them in an STL container. The program should then find and display:
a. The lowest, highest, and average daily production level.
b. A sequence that shows how much the production level rose or fell each day.
c. A sequence that shows, for each day, the total number of dooflingies produced up to and including that day.
You must use an Standard Container and you must use standard algorithms to do all the calculations shown in a, b, and c above. Solutions not using a standard container and not using standard algorithms for calculations are not acceptable.
And my feed back was:
A standard algorithm (adjacent_difference) exists to calculate the daily changes but you don't use it.
Does not meet problem spec requiring the use of standard algorithms where available.
Please help, I'm stumped and tried to rewrite it, but haven't succeded
here is my code as follows:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main(){
vector<int> product; //set up vector
cout<<"Please enter the number of cases of Dooflingies produced each day:\n"; //prompt user to enter amount produced in a day
for(int i=0; i<5; i++){ //math for vector
int temp;
cin>>temp;
product.push_back(temp);
}
cout<<"-------------------------------";
cout<<"\nHighest Production Level: " <<*std::max_element(product.begin(),product.end())<<"\n"; //display highest production level
cout<<"Lowest Production Level: " <<*std::min_element(product.begin(),product.end())<<"\n"; //display lowest production level
cout<<"Average Production Level: " <<std::accumulate(product.begin(),product.end(),0.0)/product.size()<< "\n"; //display average production level
cout<<"-------------------------------";
cout<<"\nProduction Level Influx:\n"; //display rise in production, used spaces to make it easier to see
for(int i=1; i<28; i++){ //math for vector
cout <<"Day " <<i<<": "<<product[i]-product[i-1]<<"\n";
}
int cumulative_sum[28]; //the cumulative sum of the whole program
std::partial_sum(product.begin(),product.end(),cumulative_sum);
cout<<"-----------------------------\n";
cout<<"Total Production Until:\n"; //total amount of dooflingies produced
for(int i=0;i<28; i++){
cout<<"Day " <<i+1<<": "<<cumulative_sum[i]<<"\n"; //math
}
return 0;
}
Thank you in advance
Ok, after working on this for an embarrassing number of hours, I think I came up with something less cringe-worthy to ya'll "real" programmers.
Allow me to submit my humble and probably awful code.
It totally works, but now my issue is that I'm trying to make it go back to an initial question if the response is a negative number. I got it to say, "Hey! Don't put in a negative number!", but then it goes to the next prompt. Here's my current output for a negative input:
** Welcome to the Consumer Loan Calculator **
How much would you like to borrow? $-100
Please enter a positive loan amount.
What is your annual percentage rate? %
...And for a positive input:
** Welcome to the Consumer Loan Calculator **
How much would you like to borrow? $100
How much would you like to borrow? $100
How much would you like to borrow? $
I want it to go back to "How much would you like to borrow?" if the user input is negative and only go to the next question if the input is positive. What am I doing wrong now?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;
void get_input (double &principal, double &APR, double mon_pay);
int main()
{
double loan; // principal
double APR; // APR
double mon_pay; //monthly payment
cout << "** Welcome to the Consumer Loan Calculator **"<<endl;
do {
cout << "How much would you like to borrow? $";
cin >>loan;
if (loan < 0)
cout <<"Please enter a positive loan amount.";
}
while (loan > 0);
cout << "What is your annual percentage rate? %";
cin >>APR;
cout << "What is your monthly payment? $";
cin >> mon_pay;
APR = APR/100;
get_input (loan, APR, mon_pay);
}
void get_input (double &principal, double &APR, double mon_pay)
{
double total, add=0; // Total payment
int tpay=1; //Total months of payment
while (principal > 0)
{
add = principal * (APR/100);
principal = ((principal+add) - mon_pay);
tpay++;
}
total = mon_pay + principal;
cout << "Your debt will be paid off after "<< tpay << " months, with a final payment of just $" <<setprecision(3)<<total<<endl;
cout <<"Don't get overwhelmed with debt!"<<std::endl;
}
this line is definately wrong:
while (int tp=1, double p <= double m, double sub--m)
Fundamentally, there are a lot of problems with this code. I would recommend for starts to eliminate all global variables. They'll make your code more confusing to debug and it's considered bad practice generally to use them.
Furthermore, I would choose more descriptive identifiers for your variables -- it'll make the logic less abstruse. For example, m is a poor choice for a variable name. Why not choose monthly_pay or something more clear?
Additionally, while loops take arguments that are boolean. What you've written doesn't make sense and I'm honestly not sure what the compiler would do if it isn't screaming now. My guess is that it would be in an infinite loop from the int tp=1 always evaluating to true.
Finally, it's worthwhile to learn to modularize code. Based on your code, I'd venture to say you're a beginner in the realm of code. It's very good practice (and follows in logic nicely) to modularize your code. What logical steps would you follow if you were doing this by hand?
Greet user
Get input
Do some calculations
Output to user
Say goodbye
If there are more details, expected outputs, etc., I'd recommend adding them to your question or risk being flagged as too broad.
Good luck on your homework.
erip
EDIT
Totally forgot about functions.
Functions, like in math, require arguments.
f(x) = x^2 + 2x - 1. The argument to this function is obviously x.
In programming, some functions require arguments as well.
Let's say you're trying to model this equation...
You might consider doing something like this:
#include <math.h>
double compound_interest(double r, int n, double t, double P)
{
return P*pow((1+ r/n), n*t);
}
So if you want to call this in your main
//insert header stuff, function declarations, etc.
int main()
{
double rate = 0.1, // 10%
time = 10.0, // 10 months
principle = 125.00, // $125.00
accumulated; // This is what we want to know
int payment_period = 12; // 12 months
accumulated = compound_interest(rate, payment_period, time, principle);
cout << "Total accumulated cost is " << accumulated << endl;
return 0;
}
Before I get into details about the code let me explain the issue. I'm using Microsoft Visual Studio 2013 to compile and run this code. This code executes perfectly when I bookmark any line from the function that calculates the probability. But when I don't bookmark any line or bookmark some lines from the main function the program doesn't work. By doesn't work I mean it gives the probability as either 100% or 0% no matter how many times I run it. The answer is 50% so the probability should be close to 50%. (When I simulated it 300 times while bookmarked it gave 42%) It seems like the issue is memory related. But I'm only a CS student yet so I am really just guessing at this point. Any type of feedback or solution is appreciated.
Let me explain what the code is suppose to do. My friend recently asked me a probability question. I was not able to solve it using a certain type of logic that would make it very easy to solve. So I decided to make a small program that would calculate the odds of it by simulating it a given number of time. So here is the question.
"There are 100 seats on a plane. The first passenger has lost his ticket. He doesn't know which seat is his. So he sits randomly. Everybody after this person comes to the plane one by one. If their seat is empty they sit to their designated seats. If their seat is occupied they sit randomly. What is the probability of the last person being able to sit on his designated seat?"
Here is all of the code with comments on how it works.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <string.h>
#include <string>
#define PlaneCapacity 100 //Plane capacity is now changeable. It helped me discover that this number is irrelevant to the answer of the question
using namespace std;
int PlacePeople(int SimNumber);
int SimNumber;
int Seat;
int PersonNo;
int SuccessNo = 0;
int Seats[PlaneCapacity];
int LatestSeat;
int main(){
double Probability;
cout << "Please enter the number of times you want the simulation to run:" << endl;
cin >> SimNumber;
PlacePeople(SimNumber); // Function that does the calculation
Probability = (SuccessNo * 100) / (SimNumber); //Calculate the probability in precentage form
cout << "Probability: ";
cout << Probability << endl;
system("pause");
return EXIT_SUCCESS; //Exit with success...I wish :(
}
int PlacePeople(int SimNumber){
for (int a = 0; a < SimNumber; a++){ //Number of simulations. The code doesn't work when I bookmark this line
Seats[PlaneCapacity] = { 0 }; //Array with elements that corresponds to the plane seats. Code works when I bookmark any line from this point onward. This line included.
PersonNo = 1; //Last person that sit down
LatestSeat = 0; //The seat the last person sit on
srand(time(NULL));
Seat = rand() % PlaneCapacity + 1; //Randomizer that determines where the first passanger will sit
if (Seat == 1){ //If he sits in his place everything is golden!
SuccessNo++;
}
if (1 < Seat && Seat < PlaneCapacity){ //If he doesn't sit in his place or the last passengers place things gets a bit messy
Seats[Seat - 1] = PersonNo; //Put him to his seat e.g 45th seat
for (int b = 1; b < Seat - 1; b++){ //Everybody until that seat (43 people, from 2 to 44) sits in their regular place. 45th person has no where to sit :(
PersonNo++;
Seats[PersonNo - 1] = PersonNo;
}
PersonNo++;
LatestSeat = PersonNo;
while (PersonNo < PlaneCapacity){ //The same process for the first passanger will be repeated until the last person is seated
Seat = rand() % (PlaneCapacity - LatestSeat + 1) + LatestSeat; //I tried to lower the random number interval so the code would work a little more efficiently
if (Seat == LatestSeat){ //The first guys seat might still be empty. So my interval is 1 bigger than it should be. Normally
SuccessNo++; //i would just place him when the random number says 1. But to make the interval shorter I now place the
break; //next guy to the first seat when the random number generator gives the latest seat number
} //So in my example if the random number is 45 I place the 45th guy to the 1st seat. Once the first seat is occupied
Seats[Seat - 1] = PersonNo; //We are guarenteed to have the last guy sit in his place so code can exit after that and increase the success counter
for (int b = LatestSeat; b < Seat - 1; b++){
PersonNo++;
Seats[PersonNo - 1] = PersonNo;
}
PersonNo++;
LatestSeat = PersonNo;
}
}
}
return SuccessNo; //return the number of succesfull attepmts to the main function
}
Thanks for your time!
To follow up on my comment, move the srand call to main prior to the call to PlacePeople(). When you don't break into the debugger, I think the function is executing fast enough that all iterations of the look get the same random number. To verify this, try printing out the value of seat and see if it is distributed randomly over available seats or not.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today I'm working on making my program faster. This program scans in a 100,000 fake social security numbers, first names, last names and gpa's. My professor has started talking about pointer's, referencing and dereferencing and has said that using these can help speed a program up by passing addresses. My explanation probably sucks because I am not really understanding the topics in class, so while you guys help me out I will be reading in my book for chapter 9 on call by value and call by reference. Any help would be appreciated. Thanks!!!
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
struct nameType{
string ssno;
string fName;
string lName;
double gpa;
};
int load(istream &in,nameType []);
void shellsort(nameType [],int);
void exchange(nameType &, nameType &);
void print(ostream &out,nameType [],int);
int main(void)
{
ifstream in;
ofstream out;
char infile[40],outfile[40];
nameType name[100000];
clock_t start, stop;
double secl=0;
double secs=0;
double secp=0;
double total=0;
int n;
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
if(in.fail()) {
cerr<<"problem input file\n"<<endl;
exit(1);
}
cout << "Please enter the output data file name(NO SPACES): ";
cin >> outfile;
out.open(outfile);
if(out.fail()) {
cerr<<"problem output file\n"<<endl;
exit(1);
}
start = clock();
n = load(in,name);
stop = clock();
secl = (double)(stop - start)/CLOCKS_PER_SEC;
cout << "Load Time: " << secl << endl;
start = clock();
shellsort(name,n);
stop = clock();
secs = (double)(stop - start)/CLOCKS_PER_SEC;
cout << "Sort Time: " << secs << endl;
start = clock();
print(out,name,n);
stop = clock();
secp = (double)(stop - start)/CLOCKS_PER_SEC;
cout << "Print Time: " << secp << endl;
total = secl + secs + secp;
cout << "Total Time: " << total << endl;
in.close();
out.close();
return 0;
}
int load(istream &in,nameType name[])
{
int n=0;
in >> name[n].ssno >> name[n].fName >> name[n].lName >> name[n].gpa;
while(!in.eof()){
n++;
in >> name[n].ssno >> name[n].fName >> name[n].lName >> name[n].gpa;
}
return n;
}
void shellsort(nameType name[],int n)
{
int gap = n/2;
bool passOk;
while(gap>0){
passOk=true;
for(int i=0; i<n-gap; i++){
if(name[i].lName>name[i+gap].lName){
exchange(name[i],name[i+gap]);
passOk=false;
}
else if(name[i].lName == name[i+gap].lName && name[i].fName > name[i+gap].fName){
exchange(name[i],name[i+gap]);
passOk=false;
}
}
if(passOk){
gap/=2;
}
}
}
void exchange(nameType &a, nameType &b)
{
nameType temp;
temp = a;
a = b;
b = temp;
}
void print(ostream &out,nameType name[],int n)
{
for(int i=0;i<n;i++){
out << name[i].ssno << " " << name[i].fName << " " << name[i].lName << " " << name[i].gpa << endl;
}
out << endl;
}
Exact assignment details--- I'm on bullet number 5---
Efficiency - time and space
Time and space are always issues to consider when writing programs.
In this assignment, you will be modifying the sort_v4.cpp program created in the first programming assignment. In that assignment, you were required to process an array of integers. Let's update the program with several items:
Build a structure containing an social security number, first name, last name, and a GPA.
The new sort technique that uses the gap concept as described in class that sorts the information in ascending order based on the last name (if last names are the same then the first names need to be checked). The sort technique is know as shell sort.
Improve the efficiency - time and space as described below.
Each struct element contained an ID, first name, last name and gpa. Suppose you had to process 100,000 students. The program is inefficient for two reasons
Space issue: if social security number takes up 12 characters, first and last take up 20 characters each, and the gpa as a double takes up 8 bytes then the main array of structure elements consumes (12+20+20+8)*100000 bytes of memory. This may be OK if we load up 100,000 names. But if the average number of students we process is <50,000, then there is a considerable amount of wasted memory.
Time issue: When you exchange two elements that are out of order, 60 bytes are moved around 3 times. Total of 180 bytes are moved in memory. Again, inefficient.
As the number of member variables in the struct increase the problem gets worse.
The focus of this assignment is to
Read a file containing the social security number, first, last, and a gpa into an array of structure elements. Process until EOF. Each line contains information about one student. Inside the struct, the elements may be define as char [] or strings. Do you think it will make a difference in performance if we use char [] vs strings? Make a prediction.
Dump the array into a file along with timing information.
Try out the new sort technique - sort structure elements based on two items - first and last name.
Time each function to see where the most time is being spent.
Attempt to improve the use of memory by using an array of pointers to the structs.
Attempt to improve the efficiency by making the exchange faster by swapping pointers instead of elements.
With the above list in mind, there will be three possible grades for the assignment. For a maximum grade of a C - 14/20 points, you must complete the first two bullets. For a maximum grade of a B - 16/20 points, you must complete the first 4 bullets. For an A, you must complete all bullets.
I would recommend to start the assignment by implementing what I call a non pointer version. Define a struct above the main function to hold the items. The main function should declare the array of struct elements. Call the load, sort, and print functions as we did in the first assignment making adjustments to accommodate the struct and the new sort technique where there are two items to consider before exchanging two elements. NOTE: I want to see an exchange function this time. This will give you a total of 4 functions. Make sure you follow the guidelines stated in the first programming assignment.
For all that are attempting the A program, make sure you have the basic program for a B completed and tested before continuing. Make a copy of that program and modify the program as follows:
Change the array of struct elements to an array of pointers to struct elements by placing a * in the definition.
Using new (or malloc), dynamically create space for one structure element just before you read in the values for the members of the struct.
When you exchange two elements, exchange two pointers to struct elements instead of the struct elements themselves.
Take a look at your times for the non-pointer version and the pointer version. Is there a significant difference?
Following is the information about timing one function.
#include<ctime>
//Create a couple variables of clock_t type.
clock_t start, stop;
start = clock();
load(x, n); //call a function to perform a task
stop = clock();
cout << "load time: " << (double)(stop - start)/CLOCKS_PER_SEC << endl;
The function clock() returns the number of cpu clock cycles that have occurred since the program started. Ideally, start in the above code should be 0. To be able to make some sense of how much time a function takes, you have to convert the elapsed time into seconds. This is accomplished by taking the difference between start and stop, typecasting, then dividing by the system defined CLOCKS_PER_SEC. On linus public (or an alien ware machine in the lab) is 1,000,000. Also see class notes on the topic. The following is an example of what should appear at the end of the sorted data.
load time: 0.05
sort time: 2.36
print time: 0.01
Total Run time: 2.42
The Penalty for missing deadline, 1 pt per day for a max of 7 days. Programs will not be accepted 7 days after the deadline.
If you look here: How are arrays passed?
Arrays are passed as pointers already, and your implementation of std::swap (which you called "exchange") is already passing by reference. So that is not an issue in your case. Has your professor complained about your program execution speed?
A place that takes a lot of execution time in a program is called a bottleneck.
A common bottleneck is file input and output. You can make your program faster by reducing or optimizing the bottleneck.
Reading many small pieces of data from a file takes more time than reading one large piece of data. For example, reading 10 lines of data with one request is more efficient that 10 requests to read one line of data.
Accessing memory is fast, faster than reading from a file.
The general pattern is:
1. Read a lot of data into memory.
2. Process the data.
3. Repeat at 1 as necessary.
So, you could use the "block" read command, istream::read into an array of bytes. Load a string with a line of data from the array of bytes and process the line. Repeat loading from the array until you run out and then reload from the file.
I've just started learning how to program a few months ago and this is my first programming language. I've written up this program to pull a customer number from "BeginningBalance.dat" along with customer purchases and payments.
After reading in the charges for each customer it will add a finance charge and total everything, then it will write the new ending balances for each customer number in the opposite order.
So beginningdata.dat looks like
111
200.00
300.00
50.00
222
300.00
200.00
100.00
and NewBalance.dat should look like
222
402.00
111
251.00
Now all that is being read into NewBalance.dat is
-858993460-9.25596e+061333655222402111251-858993460
Heres my code if anyone can help.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int count = 0;
const int size = 100;
double customer_beg_balance, customer_purchases, customer_payments, finance_charge_cost, finance_charge = .01;
int customer_number[size];
double new_balance[size];
double total_beg_balances=0,total_finance_charges=0,total_purchases=0,total_payments=0,total_end_balances=0;
ifstream beginning_balance;
beginning_balance.open("beginningbalance.dat");
while(beginning_balance>>customer_number[count])
{
beginning_balance >> customer_beg_balance;
beginning_balance >> customer_purchases;
beginning_balance >> customer_payments;
finance_charge_cost = customer_beg_balance * finance_charge;
new_balance[count] = customer_beg_balance + finance_charge_cost + customer_purchases - customer_payments;
total_beg_balances+=customer_beg_balance;
total_finance_charges+=finance_charge_cost;
total_purchases+=customer_purchases;
total_payments+=customer_payments;
total_end_balances+=new_balance[count];
cout<<fixed<<setprecision(2)<<setw(8)
<<"Cust No "<<"Beg. Bal. "<<"Finance Charge "<<"Purchases "<<"Payments "<<"Ending Bal.\n"
<<customer_number[count]<<" "
<<customer_beg_balance<<" "
<<finance_charge_cost<<" "
<<customer_purchases<<" "
<<customer_payments<<" "
<<new_balance[count]<<endl;
count++;
}
cout<<"\nTotals "
<<total_beg_balances<<" "
<<total_finance_charges<<" "
<<total_purchases<<" "
<<total_payments<<" "
<<total_end_balances<<endl;
ofstream new_balance_file;
new_balance_file.open("NewBalance.dat");
while(new_balance_file << customer_number[count] && count >= 0)
{
new_balance_file << new_balance[count];
count--;
}
new_balance_file.close();
system("pause");
return 0;
}
while(new_balance_file << customer_number[count])
{
new_balance_file << new_balance[count];
count--;
}
Is not right. You'll run over the beginning of the array and start negative accesses since you don't limit count.
In general, an access violation during a memory read means that either you've made a pointer math mistake (not likely in this case since you don't do any pointer math) or you've read significantly past the end or before the beginning of an array.
Learn to compile with all warnings enabled and debugging information. For GCC, that means g++ -Wall -g. Then learn how to use a debugger (like gdb for Linux) and run a program step by step.