Friday, July 31, 2009

Does anybody know how to use C++ arrays?

Please help me with this array declaration problem.





Write array declarations for the following:


a. a list of 100 double-precision voltages


b. a list of 50 double-precision temperatures


c. a list of 30 characters, each representing a code


d. a list of 100 integer years


e. a list of 32 double precision velocities


f. a list of 1000 double-precision distances


g. a list of 6 integer code numbers

Does anybody know how to use C++ arrays?
Frank,


I take it from you questions that you are just starting out in C++.


I also take it from you questions that you are not prepared to answer them.





So my next question is what the hell are you doing on a C++ course?


If you are not prepared to take the time and trouble to attempt these simple, yes simple, questions then I would seriously think about dropping the course now.
Reply:Well the first thing you need to decide is if you want to use native C++ arrays (built-in to the language, fixed size, declared like "int x[2]") or STL vectors (more powerful, standard library class, re-sizable). For now we'll assume you want standard c++ arrays.





(a) list of 100 double voltages





//standard declaration


double voltages[100];


voltage[0] = 4.5;


voltage[1] = 3.2;


....





//OR initializer list





double voltages[] = {4.5, 3.2, ..........};





(b) same as (a) except only listing 50 values in the declaration or initializer list





(c)





char codes[30];


code[0] = 'a';


....





//OR


char codes[] = { 'g', 'c', 't', 'a', 'c' };





(d)


int years[100];


//you get the idea





You can figure out the rest

wedding florist

No comments:

Post a Comment