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

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...
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 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.
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
write a C++ program that uses function swap (a,b) to enter two real numbers from the...
write a C++ program that uses function swap (a,b) to enter two real numbers from the keyboard and uses function min_max(a,b) to return a to be the smaller of the two numbers entered always.
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT