scientific c++ program

This is a discussion on scientific c++ program within the C++ Programming forums, part of the General Programming Boards category; I have an urgent assignment to write a c++ program and I lack time to learn the whole language. Is ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    scientific c++ program

    I have an urgent assignment to write a c++ program and I lack time to learn the whole language. Is there anyone willing to give a detailed outline enabling me to write the program myself? I simplified the assignment and will describe it in terms of algorithms.

    background
    The program describes the partition of heat along a bar, consisting of 100 points. At the first time t=1, the temperature of the first point is 50 (degrees) and the temperature of the other points is 0.

    header file
    The header file has to declare the array temp, an array with 100 members, which are floats, so that this array can be used in several cpp source files.

    cpp file
    input: tf , the final temperature
    output: the array of dimension 100, containing the temperatures of the 100 points of the bar at tf

    algorithm
    This algorithm will work on members of 2 succeeding arrays with 100 members: those containing the 100 temperatures of time t-1 and time t, where t is an integer, because the algorithm applies once in a second (1<=t<=tf). By temp(x,t), a single member of the array, I will mean the temperature of the point at position x (1<=x<=100 or 0<=x<=99 : this depends on the program).

    code, although not exact:
    Code:
    for(t=1, t<=tf, t++)
    {
       if ((x==1)&&(t==1))                   temp(1,1)=50;
    
       if((x==1)&&(t!=1))                   temp(1,t)=3/4*temp(1,t-1)+1/4*temp(2,t-1);
    
       if((x!=1)&&(t==1))                     temp(x,1)=0;
    
       if(1<x<100)                               temp(x,t)=1/2*temp(x,t-1)+1/4*temp(x-1,t-1)+1/4*temp(x+1,t+1);
    
       if(x==100)                                    temp(100,t)=1/2*temp(100,t-1)+1/4*temp(99,t-1);
    }
    Results
    If tf=1, the 100 members array temp(1)={50 , 0,………….,0}.
    If tf=2, the 100 members array temp(2)=={3/4*50 , 1/4*50 , 0,…………0}
    and so on…

    extra remarks
    You have seen that I think the main of the program will be a for loop in time t. Each part of this loop will produce an array of dimension 100. In order to calculate the array temp(t) the program only has to know the array(t-1). Each time the time loop proceeds the array temp(t-1) in the memory storage has to be replaced ( since memory space is limited!) by the array temp(t), to calculate the array temp(t+1), until the array temp(tf), the output, is reached.
    Then there is still something superfluous about this program. If t<100, the elements of the array temp(t) on positions x>t (if 1<=x<=100) or x>=t (if 0<=x<=99) are 0. I do not think this to be crucial, though.

    THANK YOU VERY MUCH!!!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Why do you have an assignment in C++ if you don't want to learn it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    This is an assignment for university. I'm a physics student. I do want to learn the language, but first the elements of it related to this task. I will write the program myself, but I lack time to first learn the language as a whole. I need to be directed a bit.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Ah, that makes sense. Let's see here then...
    Tips about working with several source files: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Arrays tutorial: http://augustcouncil.com/~tgibson/tutorial/arr.html

    As for defining a global array, remember this:
    - You must define the array inside a source file (NOT in a header).
    - Then you can add the type of the array, name and size and put extern before it and put it in a header (eg extern float myarray[100]. That will ensure it will be global.

    You cannot put it inside a header directly since all source files that includes the header will get that array in the code, which will create several arrays with the same name, which is not allowed.
    Last edited by Elysia; 07-24-2009 at 05:17 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    And why would I have to put the extern array in a header when I have already defined it in my source files?
    I thought I could define the array in the header file so that I could use this array in my 2 source files (there are in fact 2 similar tasks to this assignment) by including the header file. Now I can see no advantage any more in including a header file.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by #define:me View Post
    And why would I have to put the extern array in a header when I have already defined it in my source files?
    If the array is defined in A.cpp, then the compiler has no knowledge of it when compiling B.cpp, so you must tell the compiler: "This array exists in another source file".

    I thought I could define the array in the header file so that I could use this array in my 2 source files (there are in fact 2 similar tasks to this assignment) by including the header file. Now I can see no advantage any more in including a header file.
    It works, but not quite as you first thought.
    If you define the array in the header, then A.cpp and B.cpp which includes the header will have their OWN array, thus TWO arrays of the same name will exist. Not only will it not be global, it will cause a linker error.
    Therefore you must define it only in one source file, then tell the compiler that the array exists in another source file (that's easy to do in a header). The linker will automatically find your array defined in the other source file and read/write data to it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    That's clear now.
    And do you think that by reading these 2 tutorials I shall be able to write the program? I'm in particular very confused about the first point of my extra remark (in the original message)?
    And I'm equally not quite sure how to include the fact that this array is a function of time. Do I have to make it a matrix or can it be done as I suggest in my remark?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    It will at least give you something to work on.
    Physics is not my strong side, especially not in English, so as to the whole assignment thingy, I don't know too much. I did figure you might need arrays for your project, however. A big part of it is arrays, I would say.
    But I would urge you to read the tutorial and see if you can produce some code. It's always a start. Then you could make more specific questions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 09:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21