Here I'm trying to mimic data feed streaming from stock exchange using random number and subsequently store it in array.
#include <iostream>
#include <array>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <ctime>
#include <stdlib.h>
long int prevcntr=0;
using namespace std;
std::pair<long int, double>feedgenerator(long int len)
{
srand(time(0));
for(long int itr=1;itr<=len;itr++)
{
return {itr, (double)rand()/RAND_MAX};
//should continue evaluating the function without exiting
pause(0.001); //To allow some interval for 'makearray' to store it
}
}
template<size_t nn>
std::array<double, nn> makearray(long int cntr, double value, long int len)
{
std::array<double, nn> outarr; // should be able to pass the value of 'len' to 'nn'
long int itr=0;
begin:
while(cntr <= prevcntr)goto begin; // should wait until the next update
outarr[itr] = value;
prevcntr = cntr;
while(itr<len)
{
itr++;
goto begin; // control goes back to the beginning until all the elements of the array are filled with value
}
//should return the array after it is fully filled
return outarr;
}
int main()
{
double *p=new double[];
long int len = 100000;
*p = makearray(feedgenerator(len), len)
// I should be able to call these as nested functions as above
for(int i=0;i<len;i++)
cout<<*p[i]<<"\n";
return 0;
}
Question is how do I return a value without exiting the feedgenerator function. If I try to get all the values at once then it wouldn't mimic the data feed. Data feed is essentially, the same value being updated, sequentially.
To store the data, makearray is being used (vector shouldn't be used as it is extremely slow).
Overall the idea is, feedgenerator should update the same value with an increasing counter (in the real scenario counter will be the time and value will be price etc.) and makearray should store the data (Unless I store the data, the data would be lost, as in the case of data feed from stock exchange) for subsequent analysis.
In makearray, I want to pass the length of the array as an argument of the function, so that it can be controlled by another program. How can it be done is not clear to me.
The code in it's present form, doesn't compile.
Question is how do I return a value without exiting the feedgenerator function
Because you are trying to mimic the data feed stream from stock exchange, so, I think you should consider to use thread. Example:
#include <unistd.h>
#include <deque>
#include <vector>
#include <algorithm>
#include <mutex>
#include <thread>
#include <iostream>
class FeedGenerator {
long int max_length;
std::deque<double> queue;
std::mutex mutex;
std::thread thread;
bool stop;
public:
FeedGenerator(long int len) :
max_length(len), stop(false),
thread(&FeedGenerator::run, this) {
}
virtual ~FeedGenerator() {
stop = true;
thread.join();
}
// Get the latest `len` values
int getData(std::vector<double>& vec, int len) {
std::lock_guard<std::mutex> lock(mutex);
if (queue.empty()) return 0;
int nlen = std::min((size_t)len, queue.size());
vec.resize(nlen);
std::copy(queue.end() - nlen, queue.end(), vec.begin());
return nlen;
}
private:
void run() {
srand(time(0));
while (!stop)
{
{
std::lock_guard<std::mutex> lock(mutex);
queue.push_back((double)rand()/RAND_MAX);
if (queue.size() >= max_length) {
queue.pop_front();
}
}
usleep(1000);
}
}
};
int main()
{
long int len = 100000;
FeedGenerator feedgenerator(len);
sleep(1);
std::vector<double> p;
feedgenerator.getData(p, 10); // get the latest 10 values
for(int i=0;i<p.size();i++)
std::cout << p[i] << std::endl;
return 0;
}
Here is the whole code.
I know that the problem is somewhere in the hozzaad function, but I don't know to debug it.
I want to create a dynamic 2d array, with exacly vagonszam columns, but the number of rows differ.
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
class Vonat{
int vagonszam, maxtomb;
int **vagonok;
int *seged;
enum szallitmany {szen, fa, ko} ;
public:
void beolvas(int x);
Vonat(): vagonszam(0), maxtomb(0) {}
void kiir();
void hozzaad(int **vagonok, int i, string szallitmany);
};
void Vonat::hozzaad(int **vagonok, int i, string szallitmany)
{
++vagonok[i][0];
seged = (int *) realloc(vagonok[i], ((vagonok[i][0]+1)*sizeof(int)));
seged[vagonok[i][0]]=1;
}
void Vonat::beolvas(int x)
{
char szovegesallomany[]="beVagonTartalma";
szovegesallomany[15]=x+'0';
szovegesallomany[16]='\0';
strcat(szovegesallomany, ".txt");
ifstream f(szovegesallomany);
f>>vagonszam>>maxtomb;
vagonok = new int*[vagonszam];
string szallitmany;
for(int i=0;i<vagonszam;i++)
{
vagonok[i] = new int[1];
vagonok[i][0]=0;
do
{
f>>szallitmany;
cout<<szallitmany<<" ";
hozzaad(vagonok, i, szallitmany);
}
while(szallitmany!="*");
}
f.close();
}
void Vonat::kiir()
{
cout<<vagonszam<<" "<<maxtomb<<endl;
}
int main()
{
Vonat v_52164;
v_52164.beolvas(1);
v_52164.kiir();
Vonat v_54587;
v_54587.beolvas(2);
v_54587.kiir();
return 0;
}
In this code something's wrong, because after running it gets the error message specified before.
Thanks to everyone, who try to help me, all the best.
I'm working on a testing program for a lab and I keep getting the error
[jereminp#bondi hw3]$ make insert_test
g++ -O3 -g -o insert_test.o -c insert_test.cc
g++ -O3 -g -o insert_test insert_test.o
insert_test.o: In function `main':
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:17: undefined reference to `HashTable::HashTable()'
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:24: undefined reference to `HashTable::insert(int)'
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:32: undefined reference to `HashTable::insert(int)'
collect2: ld returned 1 exit status
make: *** [insert_test] Error 1
I'm fairly certain I have the logic, but I can't get past this error and I feel like I'm missing something simple. Here's the parts of the code in question.
Makefile
CC = g++
CFLAGS =
COPTFLAGS = -O3 -g
insert_test: insert_test.o
$(CC) $(COPTFLAGS) -o $# $^
insert_test.cc
#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class HashTable;
int main(int argc, char* argv[])
{
//checks .5 and .9 1x
int numRuns = 0;
int numProbes = 0;
int a=0;
HashTable h;
HashTable.hh
#ifndef HASHTABLE_H
#define HASHTABLE_H
class HashTable
{
public:
HashTable();
/* implement copy constructor, assignment, destructor if needed */
int hashfnc(int key);
int insert (int value);
/* insert the input value and return the number of probes
* return -1 if the table is full and insert fails */
bool find (int value, int& nProbes);
/* Search for the input value in table
* Return true if the search is successful, otherwise false
* Save # probes in 'nProbes' */
// getters
int capacity() { return nSlot; }
int size() { return nElem; }
double load_factor() { return load; }
int getSearchProbes() { return probesSearch; }
private:
/* declare your data */
double load; // track the load factor of table
int nSlot; // # slots i.e. max # elements can hold
int nElem; // current # elements in table
int arr[];
int probesSearch;
};
#endif
HashTable.cc
#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class HashTable
{
public:
HashTable()
{
load = 0;
nSlot = 300;//nSlot is changable in order to optimize
nElem = 0;
arr = new int[nSlot];
probesSearch=0;
for(int i = 0;i<nSlot;i++)
{
arr[i] = NULL;
}
}
Some of the things I've tried are adding any and all includes I could think of, changing the last line shown in insert_test.cc to "HashTable h = new HashTable();"(where I got a different error), adding namespace std to the header. I feel like it should be a quick fix but at the same time I can't find the solution anywhere. Plz send help
I am working with Visual Studio 2010 and the code is written in Visual C++. While debugging, if I am declaring my arrays as global I am unable to see their values, while debugging instead it is showing garbage values. I am unable to understand the root cause for this.
If there is any other way to declare the array please do let me know.
This is how I declared the buffers:
#pragma once
#include <windows.h>
#include <msclr/marshal_cppstd.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <iostream>
#include <stdlib.h>
#include<string.h>
char message_buff[1000] = { 0 };
static char buffer[100] = { 0 };
char data[100] = { 0 };//another buffer to store data // int to char changedby me//buffer to store cell ID and LAC
int tower[1000] = { 0 }; // tower no
int signal[1000] = { 0 }; // signal strength
int mccbuff[1000] = { 0 }; // mcc buf
int mncbuff[1000] = { 0 }; // mnc buffer
int count = 0;
int decimal_to_ascii(int value) //logic to get the ascii value from decimal
void clearbuffer(char *p)
{
}
int data_fetch()
{
}
using namespace std;
and that's the output:
I have been searching all morning how to do this and I can't quite find what I am looking for. I should mention that I am fairly new to C.
I am trying to modulise my code (that is working until I try and do this)
Presently in the main code I have :
#include<stdio.h>
#include<rfftw.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include <fstream>
#include <iomanip>
#include<complex>
#include<omp.h>
struct basic_gal {
double ra,dec,z,dist,fkp,nbar;
double cp[3];
double RSD[3];
};
struct basic_gal *gal;
int NGAL_MAX =200000;
main() {
if(!(gal = (struct basic_gal*)malloc(NGAL_MAX*sizeof(struct basic_gal))-1))
printf("memory allocation problem for galaxies\n");
etc etc..
}
I then go on to read in a file and allocate the various attributes to the structure.
What I would like to do is pass the empty structure (or a pointer to it) to a function, fill in the elements of the structure in the function and then return it to use in my main program again.
This requires 3 files, the main.c, the header file, header.h and the file that contains the function actions bin_gals.c
In the header file I have
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <string>
void bin_NGP(int,int*,struct basic_gal*,int);
In the bin_gals.c file I have
#include "header.h"
#include <all_the_others>
void bin_NGP(int NGAL_MAX, int *NGAL, basic_gal *gal, int flag) {
/*read in files and add data etc*/
}
And in the main.c file I have
#include "header.h"
#include <all_the_others>
struct basic_gal {
double ra,dec,z,dist,fkp,nbar;
double cp[3];
double RSD[3];
};
struct basic_gal *gal;
int NGAL_MAX = 200000;
main() {
if(!(gal = (struct basic_gal*)malloc(NGAL_MAX*sizeof(struct basic_gal))-1))
printf("memory allocation problem for galaxies\n");
int NGAL =0;
int *ipNGAL =&NGAL;
bin_NGP(NRAN_MAX,ipNGAL,gal,1);
}
The problem is that I think I am not passing the structure properly but cannot figure out how to do this.
My error messages look like
bin_gals.c: In function ‘void bin_NGP(int, int*, basic_gal*, int)’:
bin_gals.c:150: error: ISO C++ forbids comparison between pointer and integer
bin_gals.c:151: error: invalid types ‘basic_gal*[int*]’ for array subscript
and I am not sure how to fix this.
Any help would be much appreciated!
EDIT: The actual content of the bin_NGP part of bin_gals.c is:
void bin_NGP(const char *data, int NGAL_MAX, int *NGAL, double *min_x, double *min_y ,double *min_z, double *max_x, double *max_y ,double *max_z ,struct basic_gal *gal, int flag) {
FILE *fp_rand;
if((fp_rand=fopen(data,"r"))==NULL) printf("data file %d not opened\n", flag);
const int bsz=100; char buf[bsz];
fgets(buf, bsz, fp_rand); //header line
while((fgets(buf, bsz, fp_rand))!=NULL) {
double ra, dec, cz;
sscanf(buf,"%lf %lf %lf\n",&ra,&dec,&cz);
if(++NGAL>NGAL_MAX) { NGAL--; break; }
gal[NGAL].ra = ra*pi/180.;
gal[NGAL].dec = dec*pi/180.;
gal[NGAL].z = cz;
gal[NGAL].dist = calc_dp(gal[NGAL].z);
gal[NGAL].cp[0] = (gal[NGAL].dist*cos(gal[NGAL].dec)*cos(gal[NGAL].ra));
gal[NGAL].cp[1] = (gal[NGAL].dist*cos(gal[NGAL].dec)*sin(gal[NGAL].ra));
gal[NGAL].cp[2] = (gal[NGAL].dist*sin(gal[NGAL].dec));
if (flag ==1) {
if (gal[NGAL].cp[0] > max_x) max_x = gal[NGAL].cp[0];
if (gal[NGAL].cp[1] > max_y) max_y = gal[NGAL].cp[1];
if (gal[NGAL].cp[2] > max_z) max_z = gal[NGAL].cp[2];
if (gal[NGAL].cp[0] < min_x) min_x = gal[NGAL].cp[0];
if (gal[NGAL].cp[1] < min_y) min_y = gal[NGAL].cp[1];
if (gal[NGAL].cp[2] < min_z) min_z = gal[NGAL].cp[2];
}
}
fclose(fp_rand);
}
I tried to simplify the question by removing some of the input parameters.. maybe I was disguising a mistake.
EDIT: fixed. For clarity I have included the fixed code incase anyone has the same problem.
void bin_NGP(const char *data, int NGAL_MAX, int *NGAL, double *min_x, double *min_y ,double *min_z, double *max_x, double *max_y ,double *max_z ,struct basic_gal *gal, int flag) {
FILE *fp_rand;
if((fp_rand=fopen(data,"r"))==NULL) printf("data file %d not opened\n", flag);
const int bsz=100; char buf[bsz];
fgets(buf, bsz, fp_rand); //header line
while((fgets(buf, bsz, fp_rand))!=NULL) {
double ra, dec, cz;
sscanf(buf,"%lf %lf %lf\n",&ra,&dec,&cz);
if(++(*NGAL) > NGAL_MAX) { *NGAL--; break; }
gal[*NGAL].ra = ra*pi/180.;
gal[*NGAL].dec = dec*pi/180.;
gal[*NGAL].z = cz;
gal[*NGAL].dist = calc_dp(gal[*NGAL].z);
gal[*NGAL].cp[0] = (gal[*NGAL].dist*cos(gal[*NGAL].dec)*cos(gal[*NGAL].ra));
gal[*NGAL].cp[1] = (gal[*NGAL].dist*cos(gal[*NGAL].dec)*sin(gal[*NGAL].ra));
gal[*NGAL].cp[2] = (gal[*NGAL].dist*sin(gal[*NGAL].dec));
if (flag ==1) {
if (gal[*NGAL].cp[0] > *max_x) *max_x = gal[*NGAL].cp[0];
if (gal[*NGAL].cp[1] > *max_y) *max_y = gal[*NGAL].cp[1];
if (gal[*NGAL].cp[2] > *max_z) *max_z = gal[*NGAL].cp[2];
if (gal[*NGAL].cp[0] < *min_x) *min_x = gal[*NGAL].cp[0];
if (gal[*NGAL].cp[1] < *min_y) *min_y = gal[*NGAL].cp[1];
if (gal[*NGAL].cp[2] < *min_z) *min_z = gal[*NGAL].cp[2];
}
}
fclose(fp_rand);
}
You need to forward declare struct basic_gal* for the declaration of bin_NGP() as struct basic_gal is defined in main.c. However, the definition of struct basic_gal will need to be available to the definition of bin_NGP() so you should move its definition out of main.c into a separate module:
/* basic_gal.h */
#ifndef BASIC_GAL_DEFINITION
#define BASIC_GAL_DEFINITION
struct basic_gal {
double ra,dec,z,dist,fkp,nbar;
double cp[3];
double RSD[3];
};
#endif
Note, if this is C you need to use struct basic_gal.
After edit, this is the offending line:
if(++NGAL>NGAL_MAX) { NGAL--; break; }
as NGAL is an int* and NGAL_MAX is an int. Dereference NGAL:
if(++*NGAL > NGAL_MAX) { (*NGAL)--; break; }
Dereference NGAL when using as array index:
gal[*NGAL].ra = ra*pi/180.;
You need to make your structure visible to both main.c and bin_gals.c. Modify your header bin_gals.h to something like:
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <string>
struct basic_gal {
double ra,dec,z,dist,fkp,nbar;
double cp[3];
double RSD[3];
};
void bin_NGP(int,int*,struct basic_gal*,int);
bin_gals.c then would look like
#include "bin_gals.h"
#include <all_the_others>
void bin_NGP(int NGAL_MAX, int *NGAL, basic_gal *gal, int flag) {
/*read in files and add data etc*/
}
And your main file would be like the following:
#include "bin_gals.h"
#include <all_the_others>
struct basic_gal *gal;
int NGAL_MAX = 200000;
main() {
if(!(gal = (struct basic_gal*)malloc(NGAL_MAX*sizeof(struct basic_gal))-1))
printf("memory allocation problem for galaxies\n");
int NGAL =0;
int *ipNGAL =&NGAL;
bin_NGP(NRAN_MAX,ipNGAL,gal,1);
}