Question

Write a C++ program that 1) generates a vector containing 10 different random integers with values...

Write a C++ program that

1) generates a vector containing 10 different random integers with values between 1 and 100, then

2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout.

Your program output should look like this (your numbers will be different):

Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1

Average value: 39.6

Homework Answers

Answer #1
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

int main() {
    srand(time(NULL));
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(1 + (rand() % 100));
    }
    cout << "Vector values: ";
    double avg = 0;
    for (int i = 0; i < 10; ++i) {
        cout << v[i];
        avg += v[i];
        if (i != 9) {
            cout << ", ";
        }
    }
    cout << endl << "Average value: " << setprecision(1) << fixed << avg << endl;
    return 0;
}
Know the answer?
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
Similar Questions
Write a program that generates a vector with 30 random integers between –10 and 20 and...
Write a program that generates a vector with 30 random integers between –10 and 20 and then finds the maximum of all the elements that are divisible by 3. if you can explain how you got to finding the maximum it well help. also please use matlab
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
(C language) <stdio.h> decisions (else if or switch) Write a program to calculate the average of...
(C language) <stdio.h> decisions (else if or switch) Write a program to calculate the average of the 2 highest numbers entered. Ask the user to enter 3 integers, determine which 2 integers are the 2 highest, and then calculate and display the average of the 2 highest numbers. Format the answer to 2 decimal places. The numbers can be entered in any sequence - lowest to highest, highest to lowest, or completely random. A decision block will be necessary to...
Write a program in C++ that reads integer values from standard input and writes to standard...
Write a program in C++ that reads integer values from standard input and writes to standard output the smallest of the inputs and the average input value. The program should stop reading when a value equal to -1 or greater than 100 is encountered. It should also stop when an incorrect integer value (i.e., anything that isn't an int) is entered. If there is not an integer value in the input, the program should output "no integers provided"
The function named myRandomNum that returns a random integer in the range [1, 100] is defined...
The function named myRandomNum that returns a random integer in the range [1, 100] is defined as follows: int myRandomNum () { return rand()%100 + 1; } Now, write a program (code fragment) that repeatedly generates and prints random integers in the range [1, 100]. The program must stop when the absolute value of the difference between successive printed values is less than 5. Two sample output is given to help you understand the problem: 1. The program can possibly...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T and (2) a positive integer N. It then reads N integers and counts the numbers that are greater than T and the numbers than are less than T. It then prints out these two counts. Example What is the threshold value? 7 How many values? 5 Enter 5 values: 6 7 9 9 8 3 values are greater than 7 1 values are less...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is...