Question

what am i doing wrong here ? starting out with c++ early objects 9th edition chapter...

what am i doing wrong here ? starting out with c++ early objects 9th edition chapter 10 programming challenge 6

#include <iostream>
using namespace std;


int main() {
  
   double findMedian(int* numbers, int size) {
   double median;
   if (size % 2 == 0)
       median = (double)(*(numbers + ((size - 1) / 2))
           + *(numbers + (((size - 1))) / 2;
   else
               median = *(numbers + ((size - 1) / 2));
   return median;
}

double findMedian(int*, int);
const int SIZE = 5;
   int numbers[SIZE] = { 2, 4, 5, 7, 8 };
   std::cout << "Median of array is:" << median;
   return 0;
}

double findMedian(int* numbers, int size) {

   double median;
   if (size % 2 == 0);
   median = (double)(*(numbers + ((size - 1) / 2)) + *(numbers + (((size - 1) / 2) + 1))) / 2;
   else
       median = *(numbers + ((size - 1) / 2));
   return median;
}

Homework Answers

Answer #1
#include <iostream>

using namespace std;

double findMedian(int *, int);

int main() {
    const int SIZE = 5;
    int numbers[SIZE] = {2, 4, 5, 7, 8};
    std::cout << "Median of array is:" << findMedian(numbers, SIZE) << endl;
    return 0;
}

double findMedian(int *numbers, int size) {
    double median;
    if (size % 2 == 0)
        median = (double) (*(numbers + ((size - 1) / 2)) + *(numbers + (((size - 1) / 2) + 1))) / 2;
    else
        median = *(numbers + ((size - 1) / 2));
    return median;
}

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
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:...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
read a text file into two parrall arrays. The file has a name and a grade...
read a text file into two parrall arrays. The file has a name and a grade here is what I am doing not working #include < iostream> #include < fstream> #include <string> usind namespace std; int main() { int const size =2; string names[size]; int age[size]; ifstream inputFile; inputFile.open("text.txt"); for (int i = 0; i <size; i++) { inputFile >> names[i]; inputFile>> age[i]; cout << age[i] << endl; cout << name[i] << endl; } inputFile.close(); return 0; }
1) What do you think the following program will print if you type from the keyboard...
1) What do you think the following program will print if you type from the keyboard the numbers 3 for a and 8 for b ? #include < iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a > b && b > 7) { if(a < 7) cout << “ a less than 7: “ << a << ‘\n’; else cout << “Else branch executed\n”; return 0; }
What is wrong with this header file? The program wont compile but if i transfer the...
What is wrong with this header file? The program wont compile but if i transfer the body of the header file into the main file it will compile just fine. This is the error message that pops up: [Error] 'cout' was not declared in this scope // Specification file for the Account class. #ifndef ITEM_H #define ITEM_H #include <string> using std::string; class Can {    private:        double price = 0.0;        string company;        string content;...
I'm writing a fairly simple program in C++ and i keep getting an error message when...
I'm writing a fairly simple program in C++ and i keep getting an error message when i try to compile it I don't see what I'm doing wrong if someone could look at this and point me in the right direction? #inlcude <iostream> using namespace std; int main() {    int length, width, area;       cout<<"This program calculates the area of a ";    cout<<"rectangle.\n";    cout<<"What is the length and width of the rectangle ";    cout<<"separated by...
How do I break down a user inputted integer to individual digits and store it into...
How do I break down a user inputted integer to individual digits and store it into my array? I have an if statement that compares the spots of the array to check if it is a palindrome, but I need to make it so the user inputted number is broken up individually into the array. Please break it down into steps so I can understand. #include<iostream> using namespace std; int main() {    char array[6];           cout <<...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include <cmath> using namespace std; int getWeight(); float deliveryCharge(int weight); void displayCharge(int weight); int main () {    displayCharge(getWeight());    return 0; }   int getWeight() {    int weight;    cout << "Enter package weight (oz): ";    cin >> weight;    return weight; } float deliveryCharge(int weight) {    if (weight > 16)    {        return (((weight - 16) / 4) *...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT