Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Steps: 1. Create a c++ source file named lab9.cpp. Add your documentation he

ID: 3539480 • Letter: L

Question

Lab Steps:

1.      Create a c++ source file named lab9.cpp. Add your documentation header to the file.

2.      Add the following code (and any needed header files) to your cpp file. This code contains the definition of a simple array, followed by a for loop that prints the entire contents of the array.

int main()

{

    const int arrSize = 10;

    int myList[arrSize];

       

    cout << "Your Name Goes Here" << endl;

    cout << "Array Lab Output" << endl << endl;

    cout << "My first array:" << endl;

   

    for (int i = 0; i < arrSize; i++)

        cout << myList[i] << " " ;

    cout << endl;

   

    system("PAUSE");

    return 0;

}

  

3.      Compile and run this code. What does your output look like? Why?

4.      Add the following code to the end of your main (just before the pause) to initialize the array to all 100s:

for (int i = 0; i < arrSize; i++)

        myList[i] = 100;

5.      After this code, copy and paste the statements to print the array contents again. Add a meaningful label before this section of output, such as "My array listing again after initialization to 100s". Compile and run your code to see the effects.

6.      Add the following code to the end of your program. These statements set the first and last element of the array to 50:

//Now setting first and last element to 50

    myList[0] = 50;

    myList[arrSize - 1] = 50;

7.      Next, you want to print out your array again. This time, we'll use a simple print function to make this easier. Add the following function to the bottom of your code. Add an appropriate prototype as well. Then, add the call as shown to your main, just prior to the pause.

//Definition of the print function (goes below the main function)

void printarray(int arrname[], int size)

{

     for (int i = 0; i < size; i++ )

         cout << arrname[i] << " " ;

     cout << endl;

}

//Print the array using the function here (goes at the end of main, prior to pause)

    cout << endl;

    cout << "My array after setting first and last to 50" << endl;

   printarray(myList, arrSize);

8.      Add the following code to the end of your main, just prior to the pause. This code will ask the user for values for the array:

//For loop to ask for values from the user

    cout << endl;

    cout << "Now prompting for values for the array: " << endl;

    for (int i = 0; i < arrSize; i++)

        {

        cout << "Please enter a value: ";

        cin >> myList[i];

        }

9.      Also add statements to print your array again, using the printarray function as before. Label this result in your output as well.

10. Add a for loop to your program (at the end of main, just before the pause) that will add up all of the values in your myList array. Print this total out, labeled, at the end of your output.

11. As a final step, change the constant representing the size of the array at the beginning of the program to a value other than 10. You may choose any value, but do remember that you are going to be prompted for these values individually! (I wouldn't choose 1000, for example!) Compile and run your code - note that the program works for a list of any length, simply by changing the one constant in the code.

Explanation / Answer

2) required header files

#include<iostream>

using namespace std;

//now copy paste the int main function


3) The output of the programme look like


Your Name Goes Here

Array Lab Output


My first array:

1947206343 0 0 0 4246724 2686720 2686760 2686916 1963429077 20900995


Explanation:

first three lines are the result of first three cout statement.

now for loop runs 10 times and print the values of array named myList. because there is no initiation for array value so there will be 10 random number stored in array which we get as output. As shown in last line. These 10 numbers are garbage value not fixed.