-2

Here is the code I have down so far:

#include <iostream>
#include <string>

int main()
{
    int num;
    cout<<"Enter number of plots: "<<'\n';
    cin>>num;
    double x[num], y[num];
    double tf, t;
    int nr, v0, x0;
    cout<<"Enter value of initial speed: "<<'\n';
    cin>>v0;
    cout<<"Enter value of initial position: "<<'\n';
    cin>>x0;
    cout<<"Enter value of final time: "<<'\n';
    cin>>tf;
    nr =0;
    t = 0;
    while ((nr <= num) && (t <= tf))
    {
        y[nr] = (1/2 * 9.81 * t * t) + (v0 * t) + x0;
        x[nr] = t; 
        nr++;
        t++;
    }
    gnuplot_one_function ("Position vs Time","linespoints", "t", "x(t)", x, y, num);
}

[From comments]

...the graph is not true to its (expected) representation, as in, if I enter say an initial position of 10 - the graph starts at 0 still. Likewise for other variables like the number of points to plot is not represented correctly.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • first two #include and #include – Chewie Wookie Oct 30 '20 at 11:04
  • the for loop was just a test ignore that. – Chewie Wookie Oct 30 '20 at 11:05
  • The x[nr] is the time at x axis and y[nr] is the postion with time on y axis – Chewie Wookie Oct 30 '20 at 11:06
  • 6
    And what problems are you encountering, other than the use of non-standard variable-length arrays? (I fixed your layout problems.) – molbdnilo Oct 30 '20 at 11:10
  • Where does `gnuplot_one_function` come from? – Thomas Sablik Oct 30 '20 at 11:21
  • I'm guessing that's the part OP could not write by themself? – Botje Oct 30 '20 at 11:27
  • @Botje That's possible. I thought it could come from a library like http://ndevilla.free.fr/gnuplot/ – Thomas Sablik Oct 30 '20 at 11:31
  • 1
    @molbdnilo Well the graph is not true to its representation, as in, if I enter say an intial position of 10 - the graph starts at 0 still. Likewise for other variables like the number of points to plot is not represented correctly. Could explain to me "the use of non-standard variable-length arrays"? There is no actual compilation error, there is an output of graph. – Chewie Wookie Oct 30 '20 at 13:08
  • 1
    @ThomasSablik Yes from a library like that - I beleive im calling it from #include "gnuplot.cxx". Pardon me but I do not know 100% how it works but I have the gnuplot graph and gnuplot.cxx code in the same directory and access it from c++. I think anyways. – Chewie Wookie Oct 30 '20 at 13:10
  • Where did you get this library from? Do you have a documentation for it? `double x[num], y[num];` is not allowed in ISO C++. Sadly some compilers like gcc provide compiler extensions for this by default. – Thomas Sablik Oct 30 '20 at 13:18
  • 1
    Examine the value of `1/2` and then read about integer division in your favourite C++ book. – molbdnilo Oct 30 '20 at 13:19
  • @ThomasSablik I am using Geany version 1.36 as my compiler "Using GTK+ v2.24.32 and GLib v2.60.6 runtime libraries" - maybe it uses the old standard. I installed gnuplot from: https://sourceforge.net/projects/gnuplot/files/gnuplot/5.2.8/ I do not know if that gives you a clue atleast. I am still new to all of this. – Chewie Wookie Oct 30 '20 at 13:28
  • Chewie Wookie Best to put important info from your comments also into your question. – chux - Reinstate Monica Oct 30 '20 at 13:42

1 Answers1

3

if I enter say an initial position of 10 - the graph starts at 0 still.

At least these problems:

1/2 is zero, integer division

// y[nr] = (1/2 * 9.81 * t * t) + (v0 * t) + x0; 
y[nr] = (0.5 * 9.81 * t * t) + (v0 * t) + x0;

// Better to avoid naked constants
// https://en.wikipedia.org/wiki/Standard_gravity
#define STD_GRAVITY  (9.80665 /* m/(s*s) */)
// Advanced: more efficient and more numerically stable form:
y[nr] = (0.5 * STD_GRAVITY * t + v0) * t + x0;

@molbdnilo

Indexing out of range

This is undefined behavior (UB).
Index the array from 0 to num-1, not 0 to num

double x[num], y[num];
...
// while ((nr <= num) && (t <= tf)) {
while ((nr < num) && (t <= tf)) {
    y[nr] = ...
    ...
}

...or double x[num+1], y[num+1];

Variable Length arrays

Variable Length arrays (VLA) in C++ are not standard. See Why aren't variable-length arrays part of the C++ standard?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256