# Memory Allocation using C Assignment Solution

## Memory Allocation and Error

Memory allocation and error handling can be tricky. In this program, we'll unravel the complexities, providing clear explanations and practical solutions. Our expert team is here to assist you in c assignment and understanding the complexities of memory allocation and tackling errors effectively.

Given the following array, answer the following questions.

int fib = {0,1,1,2,3,5,8,13,21,34};

1. Is this a legal code? If not, write a correction below.

Answer: It is not a legal code.

Need to modify as follows:

int fib = {0,1,1,2,3,5,8,13,21,34,0,0,0};

2. What is the size of a fib?

Answer: Size of fib = 13 x 4 bytes = 52 bytes

3. What number is in the fourth index of fib?

Answer: The number in the fourth index of fib = 2

4. Which element is 21?

Answer: 21 is located in the ninth element, which is fib

5. What index is 5 located in?

Answer: The required index = 5

6. What would the following code display?

cout << fib;

7. What would the following code display?

cout << fib;

8. The following code has a logic flaw in its output. Run the program to inspect the output. Determine the problem and fix the numbering issue. Create a constant so future programmers do not have to manually type the number 6 throughout the code every time they want to refer to the number of test scores.```#include < iostream> using namespace std; int main() { int tests; // array declaration int sum = 0; float avg; //input test scores cout << "Enter " << 6 << " test scores: " << endl; for (int i = 0; i < 6; i++) { cout << "Enter Test " << i << ": "; cin >> tests[i]; } return 0; } ```

9. The following code creates a Car structure and creates an array of this structure to store data for three vehicles.

a. Fix any logical errors that may exist in the code.

b. Finish the loop to display only those cars that have a manual transmission (isManualTran). Use the given integer arraySize within your condition.

c. The correct output would be as seen below: 2017 Chevrolet Corvette

2011 Hyundai Genesis Coupe

``` #include < iostream> using namespace std; struct Car{ string make; string model; int year; bool isManualTran; }; int main() { // This creates an array of Cars named myVehicles[] Car myVehicles = { {"Chevrolet","Corvette",2017,true}, {"Honda","Accord",1997,false}, {"Hyundai","Genesis Coupe",2011,true}, {"Honda","Civic",2013,false}, {"Toyota","Highlander",2019,false} }; // this code is to calculate the size of a given array // very helpful if your program requires your array to grow in size throughout your program int arraySize = sizeof(myVehicles)/sizeof(myVehicles); for(initialize counter; condition; increment counter) { } return 0; } ```

Solution:

8.

```#include < iostream> using namespace std; const int NUM_TEST_SCORES = 6; int main() { int tests[NUM_TEST_SCORES]; // array declaration int sum = 0; float avg; //input test scores cout << "Enter " << NUM_TEST_SCORES << " test scores: " << endl; for (int i = 0; i < NUM_TEST_SCORES; i++) { cout << "Enter Test " << i << ": "; cin >> tests[i]; sum += tests[i]; } avg = 1.0f * sum / NUM_TEST_SCORES; cout << "Average = " << avg << endl; return 0; }``` 9.``` #include < iostream> using namespace std; struct Car{ string make; string model; int year; bool isManualTran; }; int main() { // This creates an array of Cars named myVehicles[] Car myVehicles = { {"Chevrolet","Corvette",2017,true}, {"Honda","Accord",1997,false}, {"Hyundai","Genesis Coupe",2011,true}, {"Honda","Civic",2013,false}, {"Toyota","Highlander",2019,false} }; // this code is to calculate the size of a given array // very helpful if your program requires your array to grow in size throughout your program int arraySize = sizeof(myVehicles)/sizeof(myVehicles); for(int i = 0; i < arraySize; i++) { if ( myVehicles[i].isManualTran ) { cout << myVehicles[i].year << " " << myVehicles[i].make << " " << myVehicles[i].model << endl; } } return 0; } ```