LAB 4 – Arrays and Vectors (INDIVIDUAL WORK)

 

In-lab activity (due at the end of the lab session)

Open a new .cpp file, copy the following code and paste it into the newly created file. Save the file and compile it.

As you will notice when you run the executable, it will ask you to input a determined number of integer values from the keyboard, followed by the integer values themselves. Try running the program, providing the following number of integers to input: 2, 6, 10, 14, 20.

Examine the output on each run. Compare them against each other. Now examine the code. Answer the following:

   1 - Draw the memory map associated with the allocated variables, including addresses, base addresses and contents.

   2 - Fix the problem.

 

Turn in your answers to both questions and your version of the fixed program. Do not forget to add your name to every page you turn in.

 

 

#include <iostream>

 

using namespace std;

 

int main(){

   int i, j;

   int arr2[4] = {0};

   int arr1[4];

   cout << &arr1 << " ";

   cout << &arr2 << " " << &i << " " << &j <<endl;

   cout << "How many integers do you want to input? ";

   cin>> j;

   for (i=0; i <j; i++) {

      cout << "Type an integer: ";

      cin >> arr1[i];

   }

   for (i=0; i < 4; i++)

      cout << "arr1[" << i << "] = " << arr1[i] <<

         ", arr2[" << i << "] = " << arr2[i] << endl;

   cout << "i = " << i << endl;

   cout << "j = " << j << endl;

   return 0;

}