|
Hi everyone!
I'm having trouble reading data from a text file. More specifically, I'm trying to read a function of two parameters from a text file and then pass it to an array of functions of two parameters.
This is what I have so far:
TextFile1.txt:
1+x*x+t*t*t 1.0 0.0 -4.0
Input.h:
#include <iostream><br />
#include <fstream><br />
using namespace std;<br />
void fin (double** (*f)(double, double), double* a, double* b, double* x, char* fin)<br />
{<br />
ifstream file (fin);<br />
file >> f[0] >> a[0] >> b[0] >> x[0];<br />
}
Problem 7 Ode.h:
#include <cmath><br />
#include <iostream><br />
using namespace std; <br />
const double lambda = - 50;<br />
const int n = 20;<br />
class ode<br />
{<br />
double tini;<br />
double ison;<br />
double tend;<br />
double (*sfn)(double t, double x);<br />
public:<br />
ode(double t0, double x0, double T, double (*f) (double t, double x)) <br />
{<br />
tini = t0;<br />
ison = x0;<br />
tend = T;<br />
sfn = f;<br />
} <br />
double* rungekuttaforward (int n) const;<br />
double* rungekuttabackward (int n) const;<br />
};<br />
double* ode::rungekuttaforward (int n) const<br />
{<br />
double* x = new double [n + 1];<br />
double * t = new double [n + 1];<br />
t[0] = tini;<br />
double h = (tend - tini) / n;<br />
x[0] = ison;<br />
for (int i = 0; i <= n; i++) <br />
{<br />
double K1 = h * sfn (t[i], x[i]);<br />
double K2 = h * sfn (t[i] + h/2.0, x[i] + K1/2.0);<br />
double K3 = h * sfn (t[i] + h/2.0, x[i] + K2/2.0);<br />
double K4 = h * sfn (t[i] + h, x[i] + K3);<br />
x[i+1] = x[i] + (K1 + 2.0 * K2 + 2.0 * K3 + K4) / 6.0;<br />
cout << "i = " << i << " " << "t = " << t[i] << " " << "x = " << x[i] << "\n";<br />
t[i+1] = t[i] + h;<br />
}<br />
return x;<br />
}<br />
double* ode::rungekuttabackward (int n) const<br />
{<br />
double * x = new double [n + 1];<br />
double * t = new double [n + 1];<br />
t[n + 1] = tini;<br />
double h = (tend - tini) / n;<br />
x[n + 1] = ison;<br />
for (int i = n + 1; i >= 1; i--)<br />
{<br />
double K1 = h * sfn (t[i], x[i]);<br />
double K2 = h * sfn (t[i] + h/2.0, x[i] + K1/2.0);<br />
double K3 = h * sfn (t[i] + h/2.0, x[i] + K2/2.0);<br />
double K4 = h * sfn (t[i] + h, x[i] + K3);<br />
x[i -1] = x[i] + (K1 + 2.0 * K2 + 2.0 * K3 + K4) / 6.0;<br />
cout << "i = " << i << " " << "t = " << t[i] << " " << "x = " << x[i] << "\n";<br />
t[i -1] = t[i] + h;<br />
}<br />
return x;<br />
Problem 7.cpp:
<br />
#include "Problem 7 Ode.h"<br />
#include "Input.h"<br />
#include <iostream><br />
using namespace std;<br />
void (**f[1])(double, double);<br />
int main()<br />
{<br />
double a[1];<br />
double b[1];<br />
double x[1];<br />
char *f_in = "TextFile1.txt";<br />
fin (f, a, b, x, f_in);<br />
ode part2(a[0], x[0], b[0], f[0]);<br />
double* soln2 = part2.rungekuttaforward (n);<br />
double h = (b[0] - a[0]) / n;<br />
}
This is what I get when I try to run the program:
------ Rebuild All started: Project: Assignment 4 - Problem 7, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'Assignment 4 - Problem 7', configuration 'Debug|Win32'
Compiling...
Problem 7.cpp
c:\documents and settings\jessica ann quan\my documents\visual studio 2005\projects\assignment4\assignment 4 - problem 7\input.h(11) : error C2109: subscript requires array or pointer type
c:\documents and settings\jessica ann quan\my documents\visual studio 2005\projects\assignment4\assignment 4 - problem 7\problem 7.cpp(25) : error C2664: 'fin' : cannot convert parameter 1 from 'void (__cdecl **[1])(double,double)' to 'double **(__cdecl *)(double,double)'
There is no context in which this conversion is possible
c:\documents and settings\jessica ann quan\my documents\visual studio 2005\projects\assignment4\assignment 4 - problem 7\problem 7.cpp(27) : error C2664: 'ode::ode(double,double,double,double (__cdecl *)(double,double))' : cannot convert parameter 4 from 'void (__cdecl **)(double,double)' to 'double (__cdecl *)(double,double)'
There is no context in which this conversion is possible
Assignment 4 - Problem 7 - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Any tips or insight in how to get this to run properly would be greatly appreciated!
Thanks!
|
|
|
|
|
ummmmm...
The problem starts at the top, in input.h.
The compiler can't convert algebraic function text to a C/C++ function pointer.
Beyond that I have NO idea what you are trying to do
|
|
|
|
|
Hi Jessica,
seems to me you want to integrate a differential equation by Runge-Kutta method.
Your class ode expects (constructor):
(1) The starting time t0 .
(2) The initial value of the x variable x0 (x(t0) ).
(3) The ending time T .
(4) The functional description of the equation, strictly speaking, a pointer
to f(x,t) , where x'(t)=f(x,t) .
Now, while it is easy read from a text file three double values, reading the function definition it's not that easy. I suggest you to hard-code the function.
e.g.
double f(double x,double t)
{
return 1. + x*x+t*t*t;
}
int main()
{
int n=1000;
ode part2(0., -4., 1., f);
double* soln2 = part2.rungekuttaforward (n);
return 0;
}
By the way, your code, has many troubles, I think.
Why do you put function definitions in cpp headers?
Why don't you protect your headers for multiple inclusions?
And so on.
|
|
|
|
|
I am currently coding for a GUI but i need mp3 playing support. how can i "graph" the waveform of the mp3 (maybe a waveform in oscilloscope view) and the left and right channels audio output. this will be my thesis and i hope you can help me. please!
chester, chester_rulz2003@yahoo.com
|
|
|
|
|
-------------
modified 24-Nov-11 2:50am.
|
|
|
|
|
SetFileAttributes()
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Hello,
I have images(CBitmap or HBITMAP) on TCP server side, which is implemented with Visual C++.
Is the conversion from hBitmap to Array possible? (RBG rawimage) (CreateBitmap converts Array to hBitmap)
I have implemented Java TCP Client Applet, which should display the "CBitmap or HBITMAP" image. (Server sents this image to Java Client, which should display the image.)
If someone can help, then thanks a lot.
Regards,
Harri
//---------------------------------------------
Example 1
//---------------------------------------------
C++ Example of Windows CBitmap(or HBITMAP) format:
#include <windows.h>
....
HBITMAP hBitmap;
BYTE px[] =
{
0x00,0xFF,0x00,0xFF,0x00,0xFF ,
0x00,0xFF,0x00,0xFF,0x00,0xFF ,
0xFF,0x00,0xFF,0x00,0xFF,0x00 ,
0x00,0xFF,0x00,0xFF,0x00,0xFF
};
hBitmap = CreateBitmap(48 , 4 , 1 , 1 , px);
Can the hBitmap be converted to Array again?
//HBITMAP CreateBitmap(int nWidth , int nHeight ,UINT cPlanes,UINT cBitsPerPel,CONST VOID *lpvBits)
//---------------------------------------------
Example 2:
//---------------------------------------------
Java example drawing picture:
import java.awt.*
Image myPic;
myPic= getImage(getCodeBase(), "picture.gif");
g.drawImage(myPic,100,100,this);
|
|
|
|
|
Try searching CP for one of the many DIB wrapper classes.
|
|
|
|
|
Thanks for hint. I solved this using GetDIBits-method.
btw. Now, I'm able to use my Windows remotely with Java Applet on a Web-page. (bit slow yet, but I have good ideas to improve it.)
|
|
|
|
|
Hi,
Is there anybody whp can explain to me what is the difference of using custom control and picture in a dialog based application? If i wan to display a graph, which one should i choose to use and why?
Thanks in advance
|
|
|
|
|
If you are just displaying a bitmap or icon then a picture (static) control is fine.
If you want user-interaction with the picture then a custom control is a way to go.
Mark
|
|
|
|
|
If i want to draw a squarewave just to display the output of my hardware, is it fine for me to use picture? And later i need to be able to use horizontal scroll and also be able to zoom in/out of the diagram. Can picture still be use for these purposes?
Thank U....
|
|
|
|
|
If you want to display dynamic content, creating a control allows you to just render in the Paint method. Otherwise, you need to duplicate that paint method elsewhere, to draw onto a bitmap. Of course, once you double buffer, you'll probably end up with a bitmap or two anyhow, but putting it in a control makes more sense.
Christian Graus - C++ MVP
|
|
|
|
|
I think it would be easiest to make a window in that case. Controls ARE windows but it doesn't
sound like a situation requiring a control-type interface.
Mark
|
|
|
|
|
When u said window, what do you mean actually? My application is a dialog based and i need to display the graph in a single dialog box. I'm a begineer in vc++,so many things are new to me..
thanks
|
|
|
|
|
If you are using MFC then a window would be a CWnd-derived class, which wraps a Windows window
handle (HWND). If you are not using MFC then it would just be an HWND.
Controls are windows (have an HWND) as well but add a pre-defined set of messages. Here's a
better explanation than I could ever give User Controls[^]
|
|
|
|
|
I am going to compare bitmap images by creating a hash value of the bits for each image. I have at my disposal a crypto library. I'm not asking how to do it, but I would like to know which algorithm would be your first choice? We all know these algorithms are usualy built for strength, so I am looking for one that is fast, has a small output but not so small that the same results would be obtained from different images. Any thoughts?
|
|
|
|
|
My hash of choice is CRC32. It has not let me down yet.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
WalderMort wrote: Any thoughts?
Super Fast Hash[^]
the benchmarks near the bottom also include comparison to CRC32.
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
Don't waste CPU cycles on cryptographically secure hash functions. A CRC-like hash function will do what you want in less CPU cycles.
--
Presented in doublevision (where drunk)
|
|
|
|
|
With the CRC32, I would issue these warnings:
1. If the CRC32 matches, it does not mean the two images are identical.
2. If the CRC32 does NOT match, it means the 2 images are NOT identical.
|
|
|
|
|
Hi! This is my first post on the web ever!
To make it short, I made a routine to change certain bytes in a file in binary mode. It all worked perfectly, until I decided to use a long name directory and it crashes. I made certain check points to find out exactly where it crashes and why. Here is my subroutine code:
void binary_edit (char *cur_dir) {
FILE *fp,*fp2;
char filename[100],filename2[100];
int temp,cur_bit=0;
sprintf(filename ,"%s\\file1.bin",cur_dir);
sprintf(filename2,"%s\\file2.bin",cur_dir);
fp=fopen(filename,"rb");
fp2=fopen(filename2,"wb");
temp=(int)fgetc(fp);
while (!feof(fp)) {
switch (cur_bit) {
case 50:fputc(0,fp2);break;
case 51:fputc(1,fp2);break;
case 52:fputc(2,fp2);break;
case 53:fputc(3,fp2);break;
case 54:fputc(4,fp2);break;
case 55:fputc(5,fp2);break;
default:fputc(temp,fp2);
}
cur_bit+=1;
temp=(int)fgetc(fp);
}
fclose(fp);
fclose(fp2);
unlink(filename);
rename(filename2,filename);
}
Please help me out
Chichi
-- modified at 0:21 Sunday 19th November, 2006
|
|
|
|
|
What do you mean by "it crashes"? Does it give you an error message? If so, what is the message? what line in your code does the error occur on? What are the values of the variables that are being accessed on that line?
You have to provide more information if you want an answer. All you will get otherwise is some wild-ass guesses.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
hihi,
the error is "installer.exe has detected a problem and it has to close......inform Microsoft....debug...dont send". It is in spanish so I do not know if my translation is perfect.
In my code there r 2 comment lines. The first comment is where an error occurs when trying to fgetc if filename is greater than 40 characters. I assume the other error I get is where the first occurrence of fputc if filename2 is greater than 38 characters long.
I am sorry for not being explicit in my POST, but I guess logically oriented people sometimes assume our short explanations are more than enough.
Chichilina
|
|
|
|
|
Does increasing the length of your filename buffers work?
char filename[256],filename2[256];
Seems pretty weird to me, but like PJ said, what errors are you getting?
[edit]This is one of those wild-ass guesses [/edit]
-- modified at 1:04 Sunday 19th November, 2006
- S
50 cups of coffee and you know it's on!
|
|
|
|
|