Bit of a long one!

Hey there, I'm pretty new to all this so go easy!

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!

lewzz10
Can you post what you've done so far?
Hey, that's fine. I haven't really done much, I don't really have a clue where to start! What I've done so far could be nonsense!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define _USE_MATH_DEFINES

#include <iostream>;

#include <fstream>;

#include <math.h>;

#include <stdio.h>;

using namespace std;

 

 

int main()

{

double Delta_t = 0.1; // change in time

double Delta_x = 0.25; // change in x

double k = 0.1; // conductivity coefficient

double x; // position

double T; // Temperature



float T[0] = 0

float T[1] = sin(M_PI * 0.25)

float T[2] = sin(M_PI * 0.5)

float T[3] = sin(M_PI * 0.75)

float T[4] = 0

 

system("PAUSE");

return 0;

}
 

//old stuff;

/*for (double x = 0; x <= 1; x = x+0.25)

{

cout << "T = " << sin(M_PI * x) << "\n";

if (x == 1.0)

cout << "T = 0 \n \n";

else

cout << "T = " << sin(M_PI * x) << "\n";

}*/
Last edited on
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.
So I should remove the float before each line? I have no idea where to go next!
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.

Ok, how do I do that (I really am pretty clueless!)?
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// libraries needed;


#define _USE_MATH_DEFINES

#include <math.h>
#include <iostream>
#include <fstream>
#include <string>


// definitions;


using namespace 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; // 
	
Any ideas? I only have an couple of hours left!
Topic archived. No new replies allowed.