Question

In: Computer Science

Please write program in C++ format: Write a program to accept five negative numbers from the...

Please write program in C++ format:

Write a program to accept five negative numbers from the user.

(1) Find the average of the five numbers and display the answer to the standard output. Keep the answer two decimal points - 5 points

(2) Output the numbers in ascending order and display the answer to the standard output. - 5 points

Solutions

Expert Solution

1).

Well commented code is given below.

#include<bits/stdc++.h>
using namespace std;
int main()
{

    cout<<"Enter the five negative numbers";//Ask user for input.
    cout<<endl;
    int arr[5];//Array to store user input.
    
    //loop to get input.
    for(int i=0;i<5;i++)
        cin>>arr[i];
        
    float avg=0;//To store the average.
    
    //Formula to calculate average Sum/5.
    avg=(float)(arr[0]+arr[1]+arr[2]+arr[3]+arr[4])/5;
    
    //Output Average up to 2 decimal places.
    printf("Average is : %.2f",avg);
}

Here is the output:

2).

Code.

#include<bits/stdc++.h>
using namespace std;
int main()
{

    cout<<"Enter the five negative numbers";//Ask user for input.
    cout<<endl;
    int arr[5];//Array to store user input.

    //loop to get input.
    for(int i=0;i<5;i++)
        cin>>arr[i];

    //Using simple bubble sort to sort the numbers.
    //Bubble sort- we will compare each number with every other number and move the larger towards the right end.
    for(int i=1;i<5;i++)
    {
        for(int j=0;j<5;j++)
            //if first number is greater than second one.
            if(arr[j]>arr[i])
            {
                //if the condition is true swap the numbers.
                int temp=arr[j];
                arr[j]=arr[i];
                arr[i]=temp;

            }
    }


    //Output the numbers in ascending order.
    cout<<"The numbers in ascending order :";
    cout<<endl;
    for(int i=0;i<5;i++)
        cout<<arr[i]<<"  ";
}

output:

If you like the above information than please do an upvote.


Related Solutions

3. Write a program that will accept only 5 numbers from 50 to 100. The program...
3. Write a program that will accept only 5 numbers from 50 to 100. The program should remind the user if an inputted number is not on the range. Compute the sum and average of the 1st and the 5th inputted numbers.
Using MatLab Write a program which will: a. Accept two numbers from the user m and...
Using MatLab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Accept from the user two row numbers. Multiply the two rows (element by element) and find the sum. Print the result. d. Accept from the user two column numbers. Add the two columns (element by element) and find the sum....
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
Write a C program that prompts the user to enter three sets of five double numbers...
Write a C program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following: a. Store the information in a 3×5 array. b. Compute the average of each set of five values. c. Compute the average of all the values. d. Determine the largest value of the 15 values. e. Report the results. Each major task should...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to...
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to lowest. I already have this code finding the maximum. Need to just add the sorting code onto it. int A, B, C; A = 4; B=3; C=7; Max = A If (B > max) { Max = B; } Else if (c > max) { Max = c; }
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
MIPS Assembly program: Accept N numbers from the user and sort the N numbers using any...
MIPS Assembly program: Accept N numbers from the user and sort the N numbers using any sorting algorithm. Print both the sorted array and unsorted array. N should be greater than or equal to 10.
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT