Question

This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

This C++ Programs should be written in Visual studio 2017

PROGRAM 1: Comparing Using Arrays to Using Individual Variables

One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it should write out their total.

Submit both versions of the program with this lab along with their test output. Write a short paragraph describing your observations about writing a program like this using individual variables versus using arrays.

PROGRAM 2: Working with Input Using Arrays

Write a program to read in a sequence of up to ten integers terminated with the sentinel value -1 and to store them in an integer array. The -1 value should not be stored in the array. The number of values read (not including the sentinel) should be kept in a separate variable. This is similar to the examples in the notes using the scores array, MAXCLASS, and numScores.

After the user has entered the numbers, the program should show how many numbers were entered, the total of the values, the average of the values, the number of values greater than the average, and the numbers in the reverse order of their entry. Here is an example of how the program might look when run (user input highlighted):

                Enter number: 4

                Enter number: 2

                Enter number: 6

                Enter number: -1

3 numbers were read in.

       The total is 12.

       The average is: 4.00

       Number of values greater than the average: 1

Values in reverse order: 6 2 4

Test the program on lists of length 0, 1, 5 and 10. Submit the source code and copies of the screen dialogue between the user and the program for each test run.

PROGRAM 3: Collecting String Input Values Using Arrays

Arrays can hold string values as well as numeric values. Write a program to read in a sequence of up to ten names from the user, storing each of them into an array of string. The input should be terminated by the user entering “***END***” instead of a name. After the names have been read in, the program should write them out to the screen.

Use a for loop that counts down from numValues-1 to 0, to write out the list in reverse order.

One of the issues with using the cin instruction to read strings is that it will only not read in strings that contain a space. For example, suppose you have the following code:

string name;

cin >> name;

If the user enters a name with a space, like “Hugh Jackman”, only “Hugh” will be entered into the name variable. The “Jackman” part of the input will be saved into an input buffer, and if the program uses cin to read another name, the user will not be given the chance to type anything. Instead, the value “Jackman” that is left over from the first entry will be entered instead.

If you want to be able to read strings that contain spaces, you must use the getline() function, which is part of the iostream library. Here is an example of how to use this function:

string name;

getline(cin, name);

When the program uses getline(), the user can enter a line of text that may contain spaces. The entire line will be stored into the variable (in this case, name).

Here is an example of what this program might look like when it is executed (user input highlighted):

                Enter a name: Hugh Jackman

                Enter a name: Jennifer Lawrence

                Enter a name: Michael B. Jordan

              Enter a name: ***END***

       You entered:

       Hugh Jackman

       Jennifer Lawrence

       Michael B. Jordan

Test your program with 0, 3, and 10 names to make sure it works.

PROGRAM 4: Simple Statistics

Write a program that will read in up to 10 numbers, ending with -1. After the numbers have been read in, your program should compute and display their mean and variance. Recall the definitions of mean and variance:

Mean:                    Add up all the numbers and divide by how many there are. This is usually called the “average”.

Variance:              The variance is obtained by adding together the squares of the differences of each element and the mean.                     This total is then divided by the number of elements. So, if the numbers were in X[0] through X[6]:

variance = ((X[0]-mean)2+(X[1]-mean)2+...+(X[6]-mean)2) / 7.

Here is an example of what the program might look like (user input highlighted):

Enter number 1 (-1 to end): 1

Enter number 2 (-1 to end): 3

Enter number 3 (-1 to end): 5

Enter number 4 (-1 to end): -1

The mean is: 3

The variance is: 2.66667         Test this program several times on lists of different lengths.

Note: this lab is about using arrays, and not about functions. Do not use functions to write this program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In this answer, we have implemented the solutions to the four problems, which highlights the use of arrays. Arrays help us in storing and accessing data easier and faster as compared to storing values in individual variables, for e.g, if we had to store data of 1000 names then it would be really difficult to create 1000 variables first and then take their values, whereas an array just has to run a loop with one same command. Below are the solutions:

Solution 1a:

Code:

#include <iostream>

using namespace std;

int main() {

    //Prompt user for the input
    cout << "Enter 10 integer values: " << endl;
    int size = 10;
    int array[size];
    int sum = 0;
    //Take input
    for (int i = 0; i < size; i++) {
        cin >> array[i];
        sum += array[i];
    }

    cout << "The sum is: " << sum << endl;

    return 0;
}

#include <iostream> using namespace std; int main() { //Prompt user for the input cout << Enter 10 integer values: << endl

Output:

Enter 10 integer values: 1 2 3 4 5 6 7 8 9 10 The sum is: 55 Process returned 0 (0x0) Press any key to continue. execution ti

Solution 1b:

Code:

#include <iostream>

using namespace std;

int main() {

    //Prompt user for the input
    cout << "Enter 10 integer values: " << endl;
    int size = 10;
    int one, two, three, four, five, six, seven, eight, nine, ten;
    int sum = 0;
    //Take input
    cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten;

    sum += one + two + three + four + five + six + seven + eight + nine + ten;
    cout << "The sum is: " << sum << endl;

    return 0;
}

#include <iostream> using namespace std; int main() { //Prompt user for the input cout << Enter 10 integer values: << endl

Output:

Enter 10 integer values: 1 2 3 4 5 6 7 8 9 10 The sum is: 55 Process returned 0 (0x0) Press any key to continue. execution ti

Solution 2:

Code:

#include <iostream>
using namespace std;

int main() {

    //Prompt user for the input
    int size = 10;
    int array[size];
    double sum = 0;
    double average;
    int count = 0;
    //Take input
    for (int i = 0; i < size; i++) {
        cout << "Enter number: ";
        int num;
        cin >> num;
        if (num == -1) {
            break;
        } else {
            array[i] = num;
            sum += array[i];
            count++;
        }
    }

    average = sum / count;
    int greater = 0;
    for (int i = 0; i < count; i++) {
        if (array[i] > average) {
            greater++;
        }
    }
    cout << count << " numbers were read in." << endl;
    cout << "The total is: " << sum << endl;
    printf("The average is: %.2f\n", average);
    cout << "Number of values greater than the average: " << greater << endl;
    cout << "Values in reverse order: ";
    for (int i = count - 1; i >= 0; i--) {
        cout << array[i] << " ";
    }
    cout << endl;
    return 0;
}
#include <iostream> using namespace std; 2 3 4 Hint main() { 5 6 2 8 9 11 12 o 13 14 15 //Prompt user for the input int size

Output:

Enter number: 4 Enter number: 2 Enter number: 6 Enter number: -1 3 numbers were read in. The total is: 12 The average is: 4.0

Solution 3:

Code:

#include <iostream>

using namespace std;

int main() {

    //Prompt user for the input
    int size = 10;
    string names[size];
    int numValues = 0;
    //Take input
    for (int i = 0; i < size; i++) {
        cout<< "Enter a name: ";
        string name;
        getline(cin, name);

        //If terminating string is entered, quit
        if (name.compare("***END***") == 0) {
            break;
        } else {
            names[i] = name;
            numValues++;
        }
    }

    cout << "You entered: " << endl;
    for (int i = 0; i < numValues; i++) {
        cout << names[i] << endl;
    }


    return 0;
}

1 #include <iostream> 2 3 using namespace std; 4 5 Fint main() { ö 7 8 9 10 11 //Prompt user for the input int size = 10; str

Output:

Enter a name: Hugh Jackman Enter a name: Jennifer Lawrence Enter a name: Micheal B. Jordan Enter a name: ***END*** You entere

Solution 4:

Code:

#include <iostream>
using namespace std;

int main() {

    //Prompt user for the input
    int size = 10;
    int array[size];
    double sum = 0;
    double mean;
    int count = 0;
    //Take input
    for (int i = 0; i < size; i++) {
        cout << "Enter number " << (i + 1) << " (-1 to end): ";
        int num;
        cin >> num;
        if (num == -1) {
            break;
        } else {
            array[i] = num;
            sum += array[i];
            count++;
        }
    }

    //Calculate mean and variance
    mean = sum / count;
    double variance;
    for(int i =0;i<count;i++){
        variance += (array[i] - mean) * (array[i] - mean);
    }

    variance /= count;

    cout<<"The mean is: "<<mean<<endl;
    cout<<"The variance is: "<<variance<<endl;

    return 0;
}

1 #include <iostream> using namespace std; 2 3 4 eint main() { 5 6 8 10 12 13 //Prompt user for the input int size = 10; int

Output:

Enter number 1 (-1 to end): 1 Enter number 2 (-1 to end): 3 Enter number 3 (-1 to end): 5 Enter number 4 (-1 to end): -1 The

Add a comment
Know the answer?
Add Answer to:
This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This C++ Program should be written in visual studio 2017 You are to write a program...

    This C++ Program should be written in visual studio 2017 You are to write a program that can do two things: it will help users see how long it will take to achieve a certain investment goal given an annual investment amount and an annual rate of return; also, it will help them see how long it will take to pay off a loan given a principal amount, an annual payment amount and an annual interest rate. When the user...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • C++ Write a program that uses an arrays of at least 20 string . It should...

    C++ Write a program that uses an arrays of at least 20 string . It should call a function that uses the linear search algorithm to locate one of the name. Read list of name from an input file call "StudentName.txt" The function should keep a count of the numbers of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also...

  • Design a program using a RAPTOR flowchart that has two parallel arrays: a String array named...

    Design a program using a RAPTOR flowchart that has two parallel arrays: a String array named people that is initialized with the names of twenty of your friends, and a String array named phoneNumbers that is initialized with your friends’ phone numbers. The program should allow the user to enter a person’s name. It should then search for that person in the people array. If the person is found, it should get that person’s phone number from the phoneNumbers array...

  • Write a java program that specifies three parallel one dimensional arrays name length, width, and area....

    Write a java program that specifies three parallel one dimensional arrays name length, width, and area. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values in the length and width arrays (thus, area[i] =   length [i]* width [i]) after data has been entered display the following output: (in Java)

  • C++ and Using Microsoft Visual Studio. Write 2 programs: One program will use a structure to...

    C++ and Using Microsoft Visual Studio. Write 2 programs: One program will use a structure to store the following data on a company division: Division Name (East, West, North, and South) Quarter (1, 2, 3, or 4) Quarterly Sales The user should be asked for the four quarters' sales figures for the East, West, North, and South divisions. The data for each quarter for each division should be written to a file. The second program will read the data written...

  • Write a C++ program to read in 4 different types of data values using cin, cout,...

    Write a C++ program to read in 4 different types of data values using cin, cout, and the getline() function and display the values in the same order to the screen. Use cin to read in the first value and getline() to read in the remaining characters. Note that the 4 data items are entered on 1 line. The following shows a sample run of the program. Enter 4 data values: 45 abc 12.34 d The 4 data values entered...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT