PreparationForExamination_Term1

docx

School

University of Miami *

*We aren’t endorsed by this school

Course

422

Subject

Mechanical Engineering

Date

Oct 30, 2023

Type

docx

Pages

10

Uploaded by CorporalRaven3623

Report
Preparation for Examination (Term 1) Arrays We covered arrays before we covered pointers, but arrays are unique in C/C++ compared to other languages. Recall in Laboratory Exercise 3 how we set up an array (hard coded inside the program and sill use up memory to hold the range of values initialised or assigned to it): #include<iostream> using namespace std; int main() { double lfa[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; double lfb[5] = {2.2, 3.3, 7.1, 6.98, 10.1}; // Do something return 0; } //End of main() We could access any element via: cout << "The third element in array lfb is: " << lfb[2] << endl; Or we could access the entire contents, but in sequence: for ( int i = 0; i < 5 ; i++ ) { cout << "The " << i << " element of the array lfb is: " << lfb[i] << endl; } By the way, you do not have to step through on every element, you could step through every other , say: for ( int i = 1 ; i < 50 ; i + 2) or ( int i = 0 ; i < 50 ; i + 2) . Let’s take an engineering example by considering that all signals that are broadcast in the atmosphere are likely to involve reflections off buildings, etc. The combined effect of a wave and it’s summing with a reflection off a building will give a complex wave. The reflection will have its main components out of phase with the direct signal and this can be represented by: Amplitude ( cos θ + jSinθ ) The j in this equation represent the imaginary part of the signal in real life. The above equation has a direct mathematical equivalent: ( Real + jImaginary )= Amplitude ( cos θ + jSinθ ) Dr D I Armour-Chélu 1 December 2019 Faculty of Engineering and Science The Reflected Part that has combined with the Direct Part as it travels in the air The Direct Part
Preparation for Examination (Term 1) So, signals in real life scenarios will have two components: a real part and an imaginary part. It’s easy to represent an integer , float , or double number in an array: double lfa[5] = {1.273, 1.275, 0.809, -0.759, -1.356}; 1.273 1.275 0.809 -0.759 -1.356 lfa[0] lfa[1] lfa[2] lfa[3] lfa[4] Taking the above array, what you really have is a collection of real numbers without the imaginary parts. So, listing these numbers in complex form should be seen as: 1.273 + j0.000 (First complex number) 1.275 + j0.000 (Second complex number) 0.809 + j0.000 (Third complex number) -0.759 + j0.000 (Fourth complex number) -1.356 + j0.000 (Fifth complex number) And, yes, the imaginary part of a complex number would be a number as well, say: 1.273 +j0.789 So, guess what? In order to represent the real and imaginary part, you’ll need an array that is twice the size – 10 elements instead of 5: 1st 2nd 3rd 4th 5th Real Imag Real Imag Real Imag Real Imag Real Imag 1.273 0.789 1.275 0.000 0.809 0.000 -0.759 0.000 -1.356 0.000 lfa[0] lfa[1] lfa[2] lfa[3] lfa[4] lfa[5] lfa[6] lfa[7] lfa[8] lfa[9] Okay, and now to code it: double lfa[10] = {1.273, 0.789, 1.275, 0.000, 0.809, 0.000, -0.759, 0.000, -1.356, 0.000}; //Real Part: for (int i = 0 ; i <= 10 ; i + 2) { cout << "The " << i << " Real element of the array is: " << lfa[i] << endl; } //Imaginary Part: ~ I’ll leave you to think this one out C/C++ cannot represent complex numbers directly – you have to set up an array to represent the real and imaginary parts and access it accordingly. Dr D I Armour-Chélu 2 December 2019 Faculty of Engineering and Science
Preparation for Examination (Term 1) Note in the previous for() loop that it is already known how big the array is (10). But what if you did not know, or wanted the flexibility of bringing in a larger data set and only wanted to change at one point in a program and not have to go through editing for every instance of it? One way finds the size without you having to think about the data type: int size_of_array; size_of_array = sizeof(lfb) / sizeof(double); //Get the size of array; for ( int i = 0; i < size_of_array ; i++ ) { cout << "The " << i << " element of the array lfb is: " << lfb[i] << endl; } These methods you saw in Laboratory Exercises 5 , 8 and 9 : // commonheader.h #include<iostream> using namespace std; #ifndef _SIGNAL #define _SIGNAL #define BUFFER_SIZE 8 After the implementation of the structure in main() , access to the elements was achieved easily via: for(int i = 0 ; i < BUFFER_SIZE ; i++) { cout << "Data element " << i << " in the signal is currently: << current.data[i] << " Arbitary Units" << endl; } Passing in an array into a function is one of the unique features of C/C++ ( Laboratory Exercise 4 ). Setting up two arrays: #include”commonheader.h” int main() { double lfa[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; double lfb[5] = {2.2, 3.3, 7.1, 6.98, 10.1}; // Do something return 0; } //End of main() Dr D I Armour-Chélu 3 December 2019 Faculty of Engineering and Science
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Preparation for Examination (Term 1) Setting the function inside a declaration: // arraycopy.h – function declaration void arraycopy(double [], double[]); #define ARRAYSIZE 5 and then using a function to copy values of one into another: #include"commonheader.h" #include"arraycopy.h" void arraycopy(double local_lfa[], double local_lfb[]) { for ( int i = 0; i < ARRAYSIZE ; i++ ) { local_lfa[i] = local_lfb[i]; cout << "The " << i << " element of the local array lfa is: " << local_lfa[i] << endl; } } //End of arraycopy() But these arrays had an instance in main() (the calling function): #include”commonheader.h” #include"arraycopy.h" int main() { double lfa[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; double lfb[5] = {2.2, 3.3, 7.1, 6.98, 10.1}; arraycopy(lfa, lfb); return 0; } //End of main() So, what was happening here? Arrays are passed into functions by reference, i.e.: the start address is passed in by default (saves memory and is quicker than passing in arrays directly. The call in main() was simple syntax: arraycopy(lfa, lfb); //Technically, arraycopy(&lfa[0], &lfb[0]); Of course, you could have passed in any of the valid address in the array, say arraycopy(&lfa[2], &lfb[2]); . Watch your bounds checking – it is very easily to fall off the end of an array in C/C++; bounds checking is not automatic in C/C++. Dr D I Armour-Chélu 4 December 2019 Faculty of Engineering and Science
Preparation for Examination (Term 1) Structures. An efficient way of handling a unique collection of variables that represent information that I need to know how to handle in a program. What to tell a program how to handle a bitmap file and the data within it? – a header section of the structure to tell the program what it is, otherwise, the numbers inside are meaningless. Creating as struct is easy – declared in a header file and called into a function block where its need (implementation). For example ( Laboratory Exercise 5 ): // commonheader.h #include<iostream> using namespace std; #ifndef _SIGNAL #define _SIGNAL #define BUFFER_SIZE 8 struct Signal { float fc; // Carrier Frequency float fs; // Sampling Frequency; int delta_f; // Change of Carrier? 1 for yes, 0 for no int ns; // Number of Samples in Buffer double data[BUFFER_SIZE]; // The data, which could be complex }; #endif An instance of which can be done via: #include"commonheader.h" int main() { // Instance of struct Signal, but also initialisation of data members struct Signal current = {0.0, 0.0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; //Confirming initialisation cout <<"Initialising the collecting structure in main()" << endl; for(int i = 0 ; i < BUFFER_SIZE ; i++) { cout << "Data element " << i << " in the signal is currently: << current.data[i] << " Arbitary Units" << endl; } // End of initialisation return 0; } // End of main() Dr D I Armour-Chélu 5 December 2019 Faculty of Engineering and Science This pair of # keywords avoids multiple inclusion of the declaration of data types, in our case struct Signal It will need the closing #endif
Preparation for Examination (Term 1) Notice the initialisation of the instance of the variables in the structure within the client function (in this case, main() ). Don’t forget: building a C/C++ program is a two-stage process. The first section deals with the insertion of pre-defined code into a source (say, what COUT actually is in terms of code) and compiles to a point (object code) ready for linking to other compiled blocks of code and optimised for file size (why duplicate blocks of code within a program if it’s already there - !) Pointers Structures are a very good way of dealing with complex information, but if you wanted to take that structure inside another function, a copy of the data (automatic normally would be made): the same amount of memory is used again with the compiled file that represents the function (each function / class needs to be in a separate file itself ( two files when it comes to a class: the header and its associated definition / encapsulated function definitions - only available via the implementation of the class via public and private . You could argue that ANSI C / Procedural C++ functions need their own independent, specific header file, a header file that is used in a client function if it was required ( Laboratory Exercise 1 ). #include"fahrenheitcentigrade.h" double fahrenheitcentigrade(double fahr_value) { double answer = 0.0, cent_value; // etc …. return answer; } //End of fahrenheitcentigrade() And, of course, with a function-specific header file ( fahrenheitcentigrade.h): #include"commonheader.h" double fahrenheitcentigrade(double); But, what is contained in the address of a pointer, in comparison to a typical scalar data type ( int , float , double , char )? It should be clear to by now how to declare a, say, double type compared to a pointer -to- double type. Initialising the double type is easy, but what do you do to ensure that the pointer -to- double contains what it should contain, and why should it contain this type of information ( Laboratory Exercise 6 )? Moving up an array in a program is straightforward enough, e.g. (Laboratory Exercise 7): #include<iostream> using namespace std; int main() { double lfa[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; Dr D I Armour-Chélu 6 December 2019 Faculty of Engineering and Science
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Preparation for Examination (Term 1) double lfb[5] = {2.2, 3.3, 7.1, 6.98, 10.1}; // Do something return 0; } //End of main() Code so as to display any or all of the elements in the array: cout << "The third element in array lfb is: " << lfb[2] << endl; Note in the above that indexing in arrays starts from 0, i.e.: for the first element in lfb[] is addressed by lfb[0] , the second element by lfb[1] , etc. Develop this code so as to print out each value in lfb[] individually. Sometimes we wish to access each element in an array one-by-one and in sequence. This is done with the use of a for() loop: for ( int i = 0; i < 5 ; i++ ) { cout << "The " << i << " element of the array lfb is: " << lfb[i] << endl; } Pointers to Structures Now let’s consider how a pointer can help with the memory usage of a struct . Laboratory Exercise 7 built on from Laboratory Exercise 5 by bringing in the method of pointing to the implementation of struct Signal inside main() . Once we had, #include"commonheader.h" int main() { // Instance of struct Signal, but also initialisation of data members struct Signal current = {0.0, 0.0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; we could then link our pointer-to-structure type: struct Signal *pcurrent = &current; and, at which point, we could easily pass into any other function that needed indirect access to the data to current . Declare it in its own header file: // pcollect.h #include"commonheader.h" struct Signal* pcollect(struct Signal *); Dr D I Armour-Chélu 7 December 2019 Faculty of Engineering and Science
Preparation for Examination (Term 1) and then use it through a call in main(): pupdated = pcollect(pcurrent); The function declaration being: #include"pcollect.h" struct Signal* pcollect(struct Signal *pl_current) { struct Signal *passigned; // Reassigning the variables pl_current->fc = 8000.0; pl_current->fs = 16000.0; pl_current->ns = 8; cout << "The carrrier frequency is now set to: " << pl_current->fc << "Hz" << endl; cout << "The sampling frequency is initialised at: " << pl_current->fs << "Hz" << endl; cout << "Start of collection - no change in frequency: " << pl_current->delta_f << endl; cout << "The number of samples to collect is now set at: " << pl_current->ns << " samples" << endl; double new_data[BUFFER_SIZE] = {1.0, 1.2, 1.1, 1.05, 1.07, 1.2, 1.3, 1.31}; for(int i = 0 ; i < BUFFER_SIZE ; i++) { pl_current->data[i] = new_data[i]; cout << "Data element " << i << " in the signal is now: " << pl_current->data[i] << " Arbitary Units" << endl; } passigned = pl_current; return passigned; } // End of pcollect() Don’t forget that you tested the amount of memory used for the direct implementation of a struct as well as a pointer to it: // Checking for the amount of memory being used in main() cout << "The amount of memory used for this structure in main() is: " << sizeof(current) << " bytes " << endl; The same process can be used for the pointer-to- any type . Structures to Classes Structures are great, but what is the difference between a struct and class with respect to the public or private access to the member functions and data types? There is that backwards compatibility to structures in C++, but you should be able to appreciate and be able to use the same functionality inside a class . This was the main purpose of Laboratory Exercise 9 (as well as giving you’re the starting point for the assignment!). Recall the use of the class declaration: Dr D I Armour-Chélu 8 December 2019 Faculty of Engineering and Science Set up by pcollect.h: Signal* pcollect(struct Signal *pl_current);
Preparation for Examination (Term 1) // BinaryFileReader.h #pragma once #include"commonheader.h" class BinaryFileReader { public: BinaryFileReader(void); ~BinaryFileReader(void); void binfileread(char *); private: typedef double* DoubleArrayPtr; }; And then the class definition: // BinaryFileReader.cpp #include "BinaryFileReader.h" BinaryFileReader::BinaryFileReader(void) { } BinaryFileReader::~BinaryFileReader(void) { } void BinaryFileReader::binfileread(char * binfile) { // Added in as Local Scope to the Member Function DoubleArrayPtr dmemblock; bool dmemallocated; ifstream binarray(binfile, ios::in|ios::binary|ios::ate); .... As before ..... if(dmemallocated) { free(dmemblock); } } // End of BinaryFileReader::binarrayread() Note, and it is strongly recommended (as far as Radi and I are concerned) that you place the class definition in classname. h and the class definition in classname. cpp – they are considered and act as a pair of files. Finally, also recall how you implement a class in a function directly or via dynamic allocation - if you know how to use a pointer, remember that it can be used on any instance of any data type, scaler or compound. Dr D I Armour-Chélu 9 December 2019 Faculty of Engineering and Science New and Additional Member Function
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Preparation for Examination (Term 1) Dr D I Armour-Chélu 10 December 2019 Faculty of Engineering and Science

Browse Popular Homework Q&A

Q: measure the depth to the water as 88.98 feet. 1) What is the depth of the water surface below land…
Q: 0.3456 g of a solid acid is dissolved in 50 mL of water in a flask  and a few drops of…
Q: A deposit of $10000 is made into an account paying a nominal annual rate of 4.25%. If the interest…
Q: Challenge #2: Sketch the derivative of the function, f(x). T X
Q: By January 2014 the US population had grown to 317.3 million and the US Federal Debt was a reported…
Q: +2 -3 a. solid calcium nitride plus heat Ca Na 3 Ca +2N → b. solid lead (IV) carbonate plus heat…
Q: An investment of $9000 grows to $10,296.59 in 4 years. Find the annual rate of return for annual…
Q: Dilly Farm Supply is located in a small town in the rural west. Data regarding the store's…
Q: Question The graph of f(x) is given below. Which of the three conditions for continuity are not true…
Q: The random variable x is the number of x | P(X=x) 0 1 2 3 4 5 6 7 3.5 Find the mean of the…
Q: In Question 5 (and potentially in Question 6), your analysis considered a price ceiling at a price…
Q: A thin film of oil, on the surface of glass , results in destructive interference. If the wavelength…
Q: B 68 m/BCA= C Don't forget to put the degree symbol in each answer. m/ACD= D m/BAC = m/D= m/CAD=
Q: Classify the following reaction: H₂SO₄(aq) + Fe(s) → H₂(g) + FeSO₄(aq) A) synthesis (or combination)…
Q: 45; and interpret the results. 46. Supply. A supply function for a certain product is given by S(p)…
Q: (a) Prepare an installment payments schedule for the first 4 years. (b) Prepare the entries for (1)…
Q: A man standing 1.50 m in front of a shaving mirror produces an inverted image 20.2 cm in front of…
Q: Milan drove 240 miles using 9 gallons of gas. At this rate, how many gallons of gas would he need to…
Q: calculate the relative rate of a reaction (in seconds) that takes 1 minute and 49 seconds.
Q: 0.40 M solution of hydrocyanic acid, HCN, has a Ka of 4.93 x 10-10.  What is the pH of this…
Q: O Using the Customer database (Customer Database has one table Customer), Create Class Library…
Q: 8. What carbonyl compound and what phosphorous ylide might you use to prepare the following…