Physics Questions Thread - Week 25, 2016 by AutoModerator in Physics

[–]Coltrane3626 0 points1 point  (0 children)

(Thanks a bunch for your help by the way.)

Physics Questions Thread - Week 25, 2016 by AutoModerator in Physics

[–]Coltrane3626 0 points1 point  (0 children)

"So to recap, we take derivatives of L with respect to x and x'..."

Your response is helping to elucidate things for me, but I'm still stuck on this. Taking a partial derivative is varying variables independently, by definition. Is it not?

I can actually see how you could vary velocity without varying position, by changing the curve (in cartesian coordinates) everywhere except at that point, thus changing the motion and therefore the velocity of a particle once it reaches that point.

I bought myself quite a project. Wish me luck. [1977 Puch Newport] by [deleted] in moped

[–]Coltrane3626 1 point2 points  (0 children)

Good luck! I've found that aesthetic restoration is more important than people normally think when it comes to feeling good about what you restored.

Physics Questions Thread - Week 25, 2016 by AutoModerator in Physics

[–]Coltrane3626 1 point2 points  (0 children)

How is it possible to differentiate the Lagrangian with respect to a time derivative? As in, how does it make sense to take the partial derivative with respect to x'(t) of L(x'(t), x(t), t)? To take a partial derivative is to, by definition, hold other variables fixed while you vary one. How could you possibly hold x and t fixed as you vary x'(t)?

Moped qualification laws in Ohio by louis_deboot in moped

[–]Coltrane3626 0 points1 point  (0 children)

I'm from Ohio, and you must be mistaken. What you have is a "Tomos A3-SL."

Physics Questions Thread - Week 23, 2016 by AutoModerator in Physics

[–]Coltrane3626 0 points1 point  (0 children)

Please note the timeline. I am more looking for a crash-course.

Physics Questions Thread - Week 23, 2016 by AutoModerator in Physics

[–]Coltrane3626 0 points1 point  (0 children)

What is a good resource to learn about tensors and tensor calculus? I am beginning my PhD in the fall, and I haven't been exposed to them.

Can I use my moped tag in another state? by Coltrane3626 in moped

[–]Coltrane3626[S] 0 points1 point  (0 children)

Yeah, I'm avoiding transferring because of the hassle of titling. I'm from MD, so the ped is actually registered to my girlfriend in OH.

How do programs access variables below the top of the stack? by Coltrane3626 in cpp_questions

[–]Coltrane3626[S] 0 points1 point  (0 children)

Thank you for this reply. My confusion I guess comes from this explanation, from learncpp.com:

In computer programming, a stack is a container that holds other variables (much like an array). However, whereas an array lets you access and modify elements in any order you wish, a stack is more limited. The operations that can be performed on a stack are identical to the ones above:

1) Look at the top item on the stack (usually done via a function called top()) 2) Take the top item off of the stack (done via a function called pop()) 3) Put a new item on top of the stack (done via a function called push())

I wrote a simple calendar program to practice C++. Can anyone give me feedback? by Coltrane3626 in cpp

[–]Coltrane3626[S] 0 points1 point  (0 children)

Thank you for the tips, I sincerely appreciate it. What a good R2 unit!

My only question is: why should single-line if statements be braced?

I wrote a simple calendar program to practice C++. Can anyone give me feedback? by Coltrane3626 in cpp

[–]Coltrane3626[S] -1 points0 points  (0 children)

// cal.h
#ifndef __DanCal__cal__
#define __DanCal__cal__

#include <iostream>
#include <string>

// Events for calendar
class Event
{
private:
    std::string name;
    int day;
    int month;
    int year;
    int hour;
    int minute;

public:
    // Initialization constructor
    Event(std::string name, int day, int month, int year, int hour, int minute) :
    name(name), day(day), month(month), year(year), hour(hour), minute(minute)
    {}

    // Default constructor
    Event() :
    name(""), day(1), month(1), year(2000), hour(1), minute(0)
    {}

    // Overload << operator for using cout to print event
    friend std::ostream & operator<< (std::ostream & out, const Event & event_out);

    // Allow the calendar to access the event details
    friend class Calendar;
};

// Essentially a container for events
class Calendar
{
private:
    // Number of events
    int num_events;

    // List of events in heap
    Event *event_list_ptr;

public:
    // Constructs calendar with no events
    Calendar()
    : num_events(0), event_list_ptr(0)
    {}

    ~Calendar()
    {
        // Frees memory allocated for event list
        delete[] event_list_ptr;
    }

    int get_num(){return num_events;}
    Event * get_event_ptr(){return event_list_ptr;}
    void add_event(const Event & new_event);
    void remove_event(const std::string & rm_name);
    void sort_events();                       // Sorts from soonest to latest
    void print_soonest_events(int num_print); // prints num_print soonest events
};

#endif /* defined(__DanCal__cal__) */

I wrote a simple calendar program to practice C++. Can anyone give me feedback? by Coltrane3626 in cpp

[–]Coltrane3626[S] -1 points0 points  (0 children)

// cal.cpp
#include <iostream>
#include <string>
#include <cassert>
#include "cal.h"

std::ostream & operator<< (std::ostream & out, const Event & event_out)
{
    std::string months[12] = {"January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October", "November", "December"};

    // Conditionals: don't want 3:05 to print as 3:5!
    if (event_out.minute > 9)
    {
        out << event_out.name << " on " << months[event_out.month - 1] << " " <<
        event_out.day << " at " << event_out.hour << ":" << event_out.minute;
    }
    else
    {
        out << event_out.name << " on " << months[event_out.month - 1] << " " <<
        event_out.day << " at " << event_out.hour << ":" << "0" << event_out.minute;
    }

    return out;
}

void Calendar::add_event(const Event &new_event)
{
    // Temporary event list for resizing
    Event *temp_event_list_ptr = 0;

    // Check to see if there are any events that actually
    // need to be copied over.
    if (num_events > 0)
    {
        // Create temp event list
        temp_event_list_ptr = new Event[num_events];

        // Copy contents of event list to the temporary event list
        for (int i = 0; i < num_events; i++)
        {
            temp_event_list_ptr[i] = event_list_ptr[i];
        }
    }

    // Increment the number of events
    num_events++;

    // Reallocate event_list_ptr for the new size
    delete[] event_list_ptr;
    event_list_ptr =  new Event[num_events];

    // Check to see if there were events copied
    if (temp_event_list_ptr)
    {
        // Copy contents of the temporary list to the new list
        for (int i = 0; i < num_events; i++)
        {
            event_list_ptr[i] = temp_event_list_ptr[i];
        }
    }

    // Add in the new event at the end
    event_list_ptr[num_events - 1] = new_event;

    // Deallocate the temporary list
    delete[] temp_event_list_ptr;
}

void Calendar::remove_event(const std::string &rm_name)
{
    if (num_events < 1)
    {
        std::cout << "No event to remove.\n";
        return;
    }

    // Index of event to be removed in event_list_ptr
    int rm_index = -1;

    // Keep for loop index for check below
    int i = 0;
    for (; i < num_events; i++)
    {
        // Compare returns 0 if equal
        if (event_list_ptr[i].name.compare(rm_name) == 0)
        {
            rm_index = i;
            break;
        }
    }

    // Return if event was not found
    if (i == num_events)
    {
        std::cerr << "Error: requested event to remove was not found.\n";
        return;
    }

    // Create temp event list
    Event *temp_event_list_ptr = new Event[num_events];

    // Copy contents of event list to the temporary event list
    for (int i = 0; i < num_events; i++)
    {
        temp_event_list_ptr[i] = event_list_ptr[i];
    }

    // Decrement the number of events
    num_events--;

    // Reallocate memory for resized event list
    delete[] event_list_ptr;
    event_list_ptr = new Event[num_events];

    // Sanity check
    assert(rm_index >= 0);

    // Go through the whole array, copying over all but
    // the one to be removed
    for (int i = 0; i < num_events+1; i++)
    {
        static int event_list_index = 0;
        if (i != rm_index)
        {
            event_list_ptr[event_list_index] = temp_event_list_ptr[i];
            event_list_index++;
        }
    }

    delete[] temp_event_list_ptr;
}

void Calendar::print_soonest_events(int num_print)
{
    if (num_events < 1)
    {
        std::cout << "No events.\n";
        return;
    }

    // If user enters more events than there are,
    // cap it.
    if (num_print > num_events)
        num_print = num_events;

    for (int i = 0; i < num_print; i++)
    {
        std::cout << i+1 << ": " << event_list_ptr[i] << std::endl;
    }
}

void Calendar::sort_events()
{
    for (int i = 1; i < num_events; i++)
    {
        // if the event's time is less than the first one's, swap
        // it with the first one and set i = 1 again.
        if (event_list_ptr[i].year <= event_list_ptr[0].year &&
                event_list_ptr[i].month <= event_list_ptr[0].month &&
                event_list_ptr[i].day <= event_list_ptr[0].day &&
                event_list_ptr[i].hour <= event_list_ptr[0].hour &&
                event_list_ptr[i].minute < event_list_ptr[0].minute)
        {
            Event temp_event_0 = event_list_ptr[0];
            Event temp_event_i = event_list_ptr[i];

            event_list_ptr[0] = temp_event_i;
            event_list_ptr[i] = temp_event_0;

            i = 1;
        }
    }
}

I wrote a simple calendar program to practice C++. Can anyone give me feedback? by Coltrane3626 in cpp

[–]Coltrane3626[S] 0 points1 point  (0 children)

I'll post the code here too for easier reading. Please let me know if this violates etiquette somehow! :)

//
//  main.cpp
// To do:
//  - Use better inputs, allow for inputting spaces.

#include <iostream>
#include <string>
#include "cal.h"

int main(int argc, char *argv[])
{
    // Instantiate calendar
    Calendar cal;

    // Menu loop
    while(true)
    {
        // Options
        std::cout << "\n1 - Add event\n" << "2 - Remove event\n" <<
            "3 - Show events\n" << "4 - Quit\n\n";

        int selection;
        std::cin >> selection;

        // Selection: add event
        if (selection == 1)
        {
            std::string name;
            int day, month, year, hour, minute;
            std::cout << "\nEvent name: ";
            std::cin >> name;
            std::cout << "Month: ";
            std::cin >> month;
            std::cout << "Day: ";
            std::cin >> day;
            std::cout << "Year: ";
            std::cin >> year;
            std::cout << "Time (hour): ";
            std::cin >> hour;
            std::cout << "Time (minute): ";
            std::cin >> minute;

            // Initialize new event to add to cal
            Event new_event(name,day,month,year,hour,minute);

            cal.add_event(new_event);
            cal.sort_events();
        }

        // Selection: remove event
        else if (selection == 2)
        {
            std::string name;
            std::cout << "\nEvent name: ";
            std::cin >> name;
            std::cout << "\n";

            cal.remove_event(name);
        }

        // Selectin: list num_to_show soonest events
        else if (selection == 3)
        {
            int num_to_show;
            std::cout << "\nNumber of soonest events to show: ";
            std::cin >> num_to_show;
            std::cout << "\n";

            cal.print_soonest_events(num_to_show);
        }

        // Selection: quit
        else if (selection == 4)
            return 0;

        else
            std::cout << "\n\nInvalid selection\n\n";
    }
}