I'm really struggling with this. What I have to do is write a program that stores results from a calculation, uses them in another calculation, and repeats a certain number of times. It then needs to export all the results to an excel spreadsheet to plot a graph. Then I have to do it again with the same data but a different equation. Then the last parts are repeating with different data for the equations.
I'm pretty happy doing fairly simple calculations, if/else stuff, for loops etc, but this is another level! I've never even had to use an array before, never mind something as complex as this.
Here's a link to the tutorial I've been doing if you want to help and need more information! http://imgur.com/jMYT7sS
Thanks for any help, this has been bugging me for a while now!
I'm guessing you know already that there are problems with that, because it won't compile. Lines 33 to 41 aren't legal. You don't declare the type of each element of an array - you've already declared the type of the array on line 29.
Well, you need to do more than that. If you want T to be an array, you need to declare it to be an array. Currently, you have it declared to be just one double.
Hi again, I've made some progress. Unfortunately however, when I run the program it prompts me to press enter a load of times, then prints the results and immediately closes, so it's not really doing what it's supposed to... Any help would be great!
// libraries needed;
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
// definitions;
usingnamespace std;
float delta_x = 0.25; //spacial interval (constant)
float delta_T = 0.1; // change in time/time interval (constant)
float time = 1; // base time
float k = 0.1; // conductivity coefficient (constant)
float number_points = (1 / delta_x) + 1; // finds no. of points to measure the temperature of the rod for.
float T[100][100]; // temperature array (100 used for simplicity)
int a, b; // for counting through the array for T
// fun stuff
float calculate_T(int A, int B); /* Function that returns T for a certain point on the rod in the time
( Where N is time, I is space)*/
ofstream out; // for writing to text file
void populateT_Array(); // cycles through teh temperature array and fills elements
void main() {
populateT_Array(); // array population function
out.open("data.txt"); // Opens file output stream
string mystr;
for (b = 0; b <= (time / delta_T); b++) { // runs through different times
for (a = 0; a < number_points; a++) { // runs through different positions on the rod
cout << "t = " << delta_T * b << " ; a = " << delta_x * a<< " ; T = " << T[b][a];
// Writes results to a text file with a header for time & space
}
cout << "\n";
}
}
void populateT_Array() {
for (a = 0; a < number_points - 1; a++) {
T[0][a] = sin(M_PI * delta_x * a ); // fills the start points (t = 0)
}
for (b = 1; b <= (time / delta_T); b++) {
for (a = 1; a < number_points - 1; a++) {
T[b][a] = calculate_T(b, a); // Calls the temperature calculation function for each element in array
}
}
}
float calculate_T(int A, int B) {
float tempBit = ((T[A - 1][B - 1] - 2 * T[A - 1][B] + T[A - 1][B + 1]) / (delta_x * delta_x));
float coefficient = k * delta_T;
float finish = T[A - 1][B];
float temp = coefficient * tempBit + finish;
system("PAUSE");
return temp; //