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
Related
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;
}
Background Info
I am designing a program in C++ to generate a weekly rota for my work.
It begins by specifying certain criteria, such as how many hours each person is required to work as well as what types of shifts they should work i.e. only day shifts, only at the weekend, etc.
The way I was thinking of designing the part of the code that randomly generates the rota was to initialize a 7 x 18 array (7 days in the week and 18 hours of working hours to cover a day). The program would then start with a for loop to determine what day and then a nested for loop to determine what hour of the day we are focussing on (sorry I know the terminology usage here is a bit sketchy). Each hour of each day it would generate a random number and assign it to the element. (Note: each person at the start of the program is assigned an integer to represent them.)
My question
I want to assign elements in the array more than one value (I require more than one person to work at any given time.) So I was wondering if this is even possible and if so, how would I go about doing it.
The code that I have so far is inserted below.
(Note: I do realise that this is at its very beginning stages. I first want to see if my general idea works before spending a lot of time writing it up.)
Thank you in advance.
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{ ofstream outFile;
outFile.open("array.dat");
srand(time(NULL));
int NoOfDaysInWeek=7;
int NoOfHoursinDay=18;
int array[NoOfDaysInWeek][NoOfHoursinDay];
James=1;
Mark=2;
Will=3;
Jamie=4;
Lisa=5;
Sandra=6;
Chris=7;
Julia=8;
Jane=9;
Anne=10;
Alexandra=11;
Jenny=12;
for (int i=0; i<NoOfDaysInWeek; i++) { // loop for day of week
for (int j=2; j<12; j++) { // loop for hours 2 to 12 (already
array[i][j]=5;
}
for (int j=0; j<8; j++) {
array[i][j]=(rand() % 4)+1; // only want mark, james, will or jamie to get these shifts.
}
}
}
#include<iostream>
using namespace std;
int main(){
int i,x,max=0;
cin>>x;
int a[x];
for(i=0;i<x;i++){
cin>>a[i];
if(max<a[i]){
max=a[i];}
}
int b[max+1];
for(i=0;i<max+1;i++){
b[i]=-1;
}
for(i=0;i<x;i++){
if(b[a[i]]==-1){
b[a[i]]=1;
}
else{
b[a[i]]++;
}
}
i=0;
while(i<=max){
while(b[i]>0&&b[i]!=-1){
cout<<i<<endl;
b[i]--;
}
i++;
}
return 0;
}
Guys I tried indexing method for sorting and codechef shows tle .. complexity of this problem is not o(n) but closer to it ... the question has a time limit of 5 sec and source limit is 50000 bytes..
Any help on how to improve the performance either by faster i/o or code computations ...
I'm pretty certain your code is problematic because you are using cout << x << endl; in a loop that will print a huge number of lines.
I will be back with "difference" in a few minutes.
Edit: Not sure I can make much of a difference either way. Obviously, depending on compiler, it may vary greatly, but with my g++ -O2 and 100000 input numbers, it takes 0.16 - 0.18s to use endl; and 0.06 - 0.07s to use '\n' for the output.
Using printf isn't faster than cout, but scanf is a little faster than cin (0.04s +/- 0.05).
However, that is really related to sync_with_stdio. If we use cin.sync_with_stdio(false); then the results are the same for scanf and cin.
All measurements are made with a file as input and a file as output - it takes much longer to write to the shell, but that's because it's scrolling 100k lines of text past me.
(Your program will crash with eithr "large" inputs or with large number of inputs - if max is greater than about 1 million, the code will crash due to out of stack - on many systems, that may happen for lower values too)
Avoid cin and cout, and use the C IO functions scanf and printf instead. You'll find they can be up to 5x faster than the slow c++ functions.
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.
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.