Question 1

Arrays are one of the common, compound data structures in C/C++ and use memory efficiently when passed into functions.

(a) Figure Q1(a) shows the declaration of an array of doubles. Modify the code so as to allow the initialisation of the array.

(b) it has been decided that, after initialisation, the values in lfa[] are to be incremented by the value of 1.0.

  • Modify main() to allow the call of valueincrement() (shown by Figure Q1(b)). What must you also do so as to allow main() to compile with this function call and why?
  • How is it that the pass-in of lfa[] into valueincrement() updates its contents even though the updating is done inside valueincrement()?
  • What is assumed with the current version of valueincrement() and what should also be passed in as parameter? Modify the code blocks to reflect your answer.
  • Assuming that the two functions would be written by different system developers, what would be a better method of managing the size of the array via a common header file? Modify the code to reflect your answer.

Figure Q1(a)

int main()
{
double lfa[5];
return 0;
} // End of main()

Figure Q1(b)

void valueincrement(double l_lfa[])
{
for(int i = 0; i < 5; i++)
{
l_lfa[a] += 1.0;
}
} // End of valueincrement()

Question 2

Pointers in C/C++ are used to help with memory as well as speed of access to data. Pointers must, however, contain an address.

(a) Consider the code extract shown by Figure Q2. The code would compile, but it would give a warning message. Outline what the warning message could say and modify the code so as plfa connects to the start address of lfaa.

(b) The print to STDOUT does not give the expected for the first element of the array, Ifaa[ ]. Why is this so and how should this statement read?

(c) How would you connect plfa to a different variable in the array and not to the first? Support your answer with a modifed statement.

(d) By referring to the code shown by Figure Q2, how much memory would be used by the variable Ifaa? Support your answer with code extract and assume a double uses 8 bytes.

(e) Consider that the function, void arraypassin (double[ ]), prints out the values of the array when called by main(). How much memory would be used for the variable Ifaa[ ] when the function call is made? Explain your answer.

(f) Using pointer arithmetic and code example, show how the mean value of Ifaa can be found. State any assumptions made.

Figure Q2

#include< iostream>
using namespace std;
void arraypassin (double[]);
int main()
{
double Ifaa[] = {1.1, 2.5, 3.26, 6.5, 1.2}; double *plfa;
cout<<"The first value of Ifaa is: " << Ifaa[1] << endl;
arraypassin(Ifaa);
return 0;
} // End of main()

Question 3

Structures are important compound data structures in C++ and are often used to handle complex data (real and imaginary values) in signal collection systems.

(a)

  • Implement the structure shown by Figure Q3 in a main/client function and with the following initialisation of the data variables:
fc = 415000;
fs = 16000;
delta f = 0;
buffer_count = 0;
  • Explain how data[ ] could be initialised to double values of 0.0.

(b) With the use of example code, show how the contents of the implemented structure can be displayed to STDOUT.

(c) There is a requirement to keep track on how many times the implementation of a struct such as Signal is called in a program. Describe (with supporting code if necessary) how the use of a static variable could be used for this purpose. In other words, does the use of int buffer_count achieve this?

(d) How could the real and imaginary parts of a complex number, e.g.: (2.35 +4.00j), be handled in an array?

Figure Q3

#include< iostream>
using namespace std;
#define BUFFER_SIZE 8
struct Signal
{
float fc; // Carrier Frequency
float fs; // Sampling Frequency;
int delta_f; // Change of Carrier?
int buffer_count; // Number of Buffers used
double data[BUFFER_SIZE]; // The data (could be complex)
};

Question 4

Classes are the main building blocks in object-oriented programming.

(a) Define a destructor and explain its six properties.

(b)

  • Define a class to represent a student with the following member data: first and last name, degree name, year of study, id number, mobile number and email address. The first and last name, degree name and email address are of type string, year of study is of type short int, id number and mobile number are of type long int.
  • Define all SEVEN set and all SEVEN get methods for each member data within the class.
  • Define TWO constructors: one default constructor (initialising all member data with empty strings or 0 for the numbers) and another constructor giving specific values to all member data. You could use the set methods defined in Q4. b) ii. or you could write the constructors independently without using the set methods.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.