Question

Hello, I feel like I am super close but I can not figure out why my...

Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you!

Here is an example of the output:

Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7}

Output : Mean = 4.5

Median = 4.5

Code so far:  

#include <iostream>
using namespace std;
int main()
{
int a[100];
int n,i,sum=0;
float mean, medium;
//read array size
// read array
cout<<"Enter array size: ";
cin>>n;
//loop for reading
i=0;
while (i<=n-1)
{
cin>>a[i];
++i;
}
for (i=0;i<n;i++)
{
sum +=a[i];
}
mean=sum/(float)n;
cout<<"The mean is: "<<mean<<endl;
//sort the array
int temp, j;
for(int i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
temp=a[i];
//swap
}
//even num
if(n%2==0)
{
medium = (a[n-1/2]+a[n/2])/2;
}
else
{
medium=a[n/2];

}
cout<<"The medium is: "<<medium<<endl;
return 0;
}

Homework Answers

Answer #1

Below is the corrected code. Now mean and median is calculating correctly.

#include <iostream>
using namespace std;
int main()
{
        int a[100];
        int n, i, sum = 0;
        float mean, medium;
        //read array size
        // read array
        cout << "Enter array size: ";
        cin >> n;
        //loop for reading
        i = 0;
        while (i <= n - 1)
        {
                cin >> a[i];
                ++i;
        }
        for (i = 0; i < n; i++)
        {
                sum += a[i];
        }
        mean = sum / (float)n;
        cout << "The mean is: " << mean << endl;
        //sort the array
        int temp, j;
        
        //Sort array using single for loop
        for (int j = 0; j < n - 1; j++)
        {
                // check 2 similtaneous elements of the array
                if (a[j] > a[j + 1])
                {
                        // Swapping the elements. 
                        int temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;

                        // updating the value of j = -1 
                        // so after getting updated for j++ 
                        // in the loop it becomes 0 and 
                        // the loop begins from the start. 
                        j = -1;
                }
        }
        
        //even num
        if (n % 2 == 0)
        {
                medium = (double)(a[(n - 1) / 2] + a[n / 2]) / 2;               
        }
        else
        {
                medium = (double)a[n / 2];

        }
        cout << "The medium is: " << medium << endl;
        return 0;
}

In existing code array sort was not working properly. Corrected code to sort the array. Also used double casting for calculated median Now mean and median is calculating correctly. Below is the output

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
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix?...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix? #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" int main() {       int n;    int a[10], i;    int...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
C++, I am not able to get this program to function correctly, ie not launching at...
C++, I am not able to get this program to function correctly, ie not launching at all #include <iostream> #include <stdio.h> int insert(int num, int location) { int parentnode; while (location > 0) { parentnode =(location - 1)/2; if (num <= array[parentnode]) { array[location] = num; return; } array[location] = array[parentnode]; location = parentnode; } array[0] = num; } int array[100], n; using namespace std; main() { int choice, num; n = 0; while(1) { printf("1.Insert the element \n"); printf("2.Delete...
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT