help with arrays

HI i would like some help with arrays; I need to store information somehow and my first though was using arrays to store information, but I am fairly new to this topic actually;

so... this would work?
double materials[100][8]

array of 100, with each index having 8 values within, furthermore am i able to store a variable and its value on an array ?

say...
materials[1] = { ID: 0, MaterialX: 1... material8 : 12}
any helps is greatly appreciated
Thank you

double materials[100][8]
That means an array for 800 double values. Essentially a 2D table with 100 rows and 8 columns.

You would perhaps benefit more from a struct that holds properties (ID, Material*) and then create an array (or std::vector) of those structs.
I did a 2D table now, I am just trying to figure out one last problem, I need to compare masses between each array, and display the ID that has the lowest mass.

take a look at my code, I can also embed the Header and main file if you like...
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
90
91
92
93
94
#include "functions.h"

minimalMass::minimalMass() :
	lenght(1.219), criticalLoad(350), pi(3.141592654), counter(0)
	{
		cout << "Welcome to the mass ID log" << endl;
	}

void minimalMass::welcomeMessage(){
	cout << "Hello Please Enter ID (enter 0 to finish input): ";
	id = validateData();
	while (id != 0)
		{
			cout << "Enter yield stress (MPa): "; yieldStress = validateData();
			cout << "Enter elasticity (GPa): "; elasticity = validateData();
			cout << "Enter density: "; density = validateData();
			materialLog[counter][0] = id;
			materialLog[counter][1] = yieldStress;
			materialLog[counter][2] = density;
			materialLog[counter][3] = elasticity;
			calculateMass();
			counter++;
			cout << "\nEnter ID(enter 0 to finish input): "; id = validateData();
		}
	displayResult();
}

void minimalMass::calculateMass(){
	C = (criticalLoad*(lenght*lenght))/(2*(pi*pi)*(elasticity*1000));
	diameter = sqrt(4/(pi*(yieldStress*1000000))*((criticalLoad*1000)+pi*(yieldStress*1000000)*C));
	volume = pi*((diameter*diameter)/4)*lenght;
	mass = density*volume;
	materialLog[counter][4] = volume;
	materialLog[counter][5] = mass;
}

void minimalMass::findMin(){
	//find minimal mass right HERE!
}

void minimalMass::displayResult(){
	int fieldLenght = 15;
	if (counter > 0)
	{
		cout << '\n';
		cout << "Here are the results:" << endl;
		cout << "ID" << setw(fieldLenght)<< "Yield Stress" << setw(fieldLenght) << "Density" 
			 << setw(fieldLenght) << "Elasticity" << setw(fieldLenght) << "Volume" << setw(fieldLenght) << "Mass" << endl;
		for (int i = 0; i < counter; i++)
		{
			int fieldLenght = 15;
			cout << " " << materialLog[i][0] << setw(fieldLenght) << materialLog[i][1] << setw(fieldLenght) << materialLog[i][2] 
			 << setw(fieldLenght) << materialLog[i][3] << setw(fieldLenght) << materialLog[i][4] << setw(fieldLenght) << materialLog[i][5] << endl;
		}
	}
}

double minimalMass::validateData(){
	double x;

	while (!(cin >> x))
	{
		cin.clear();
		cin.ignore(2000,'\n');
		cout << "Please enter a valid number: ";
	}
	return x;
}

void minimalMass::reRun(){
	int reRun;
	cout << "Would you like to make another calculation?";
	cout << "\n>> 1. Re-run\n>> 2. EXIT" << endl;
	reRun = validateData();
	while (reRun < 1 || reRun > 2){
		cout << "Please enter a valid number: "; 
		reRun = validateData();
	}
	switch (reRun){
		case 1:
			for (int i =0; i < counter; i++){
				materialLog[i][0]=0; materialLog[i][1]=0;materialLog[i][2]=0; materialLog[i][3]=0;
				materialLog[i][4]=0;materialLog[i][5]=0;
			}
			welcomeMessage();
			break;
		case 2: 
			break;
		default:
			cout << "Please enter a valid number"; 

	}
}
Last edited on
Topic archived. No new replies allowed.