Skip to content

Instantly share code, notes, and snippets.

@goyalankit021
Created May 29, 2022 17:38
Show Gist options
  • Save goyalankit021/a596353f2c32fdd6729c099ce15d0b5c to your computer and use it in GitHub Desktop.
Save goyalankit021/a596353f2c32fdd6729c099ce15d0b5c to your computer and use it in GitHub Desktop.
Please Plot Graph (Input is added at bottom)
#include <algorithm>
#include <iostream>
#include <vector>
#include "pbPlots.hpp"
#include "supportLib.hpp"
using namespace std;
int main() {
// Rading Count Of Input Values From User
int size;
cout << "Enter count of values: ";
cin >> size;
// Declaring Array Of Force And Elongation
double force[size];
double elongation[size];
cout << "Enter values of Force And Elongation\n\n";
for (int i = 0; i < size; i++) {
cout << "Please enter " << (i+1) <<"th force value (Newton): ";
cin >> force[i] ;
cout << "Please enter " << (i+1) <<"th elongation value (Meter): ";
cin >> elongation[i];
}
// Declaring Spring Constant Array
double springConstant[size];
// Defining Vector For Graphs
vector<double> xs{};
vector<double> ys{};
// Calculating Spring Constant
cout << "\t\t\t\tResult\n\n";
for ( int i = 0; i < size; i++) {
// Pusing Variables In Vector For Graph
xs.push_back(elongation[i]);
ys.push_back(force[i]);
if( force[i] == 0 || elongation[i] == 0) {
springConstant[i] = 0;
} else {
springConstant[i] = force[i] / elongation[i];
}
cout<< "Force: " << force[i] <<"N\n";
cout<< "Change In Length: " << elongation[i] <<"m\n";
cout << "Spring Constant: " << springConstant[i] << "N/m\n\n";
}
bool success;
StringReference *errorMessage = new StringReference();
RGBABitmapImageReference *imageReference = CreateRGBABitmapImageReference();
success = DrawScatterPlot(imageReference, 600, 400, &xs, &ys, errorMessage);
if(success){
vector<double> *pngdata = ConvertToPNG(imageReference->image);
WriteToFile(pngdata, "example1.png");
DeleteImage(imageReference->image);
} else{
cerr << "Error: ";
for(wchar_t c : *errorMessage->string){
wcerr << c;
}
cerr << endl;
}
return 0;
}
Input:
4
10
0.02
20
0.04
25
0.08
70
0.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment