#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main ()
{
    float nep,b,sum,temp,i,j,median;
    cout<<"Enter the number of Elements";
    cin>>nep;
    double element [10];
    int index;
    for (index=0;index<nep;index++)
    {
        element[index]=0.0;
    
        }
        for (index=0;index<nep;index++)
        {
            cin>>element[1];
            sum=element[1]+element[2];
            element[1]=element[2];
            element[2]=sum;	
         
            }
            b=sum/nep;
            cout<<"Mean:"<<b<<endl;
for(i=0;i<nep;i++)
for(j=i+1;j<nep;j++)
{
if (element[3]>element[4])
{
temp=element[4];
element[3]=element[4];
element[4]=temp;
}
}
if(nep/2==0)
{
median=(element[nep/2]+element[nep/2-1])/2;
cout<<"Median:"<<median<<endl;
}

            getche ();
            return 0;
            }

Good Day!

I'm a beginner in c++ programming that's why i'm having trouble. I'm using dev c++ as my case tool to write programs and when i tried using some sort of functions; it always making some error report. Please do help me. Can anyone edit my code using such functions so that dev c++ can read it. [i'm nor smart] Please help me. Thanks

-MhuR-

Dani AI

Generated

— the posted program has several logical and type errors that prevent correct median/mean calculation: the element count is treated as a float, inputs are read into fixed indices instead of the loop variable, sum is never initialized, the sort/nested loops use wrong indices, and the even/odd check for the median is incorrect. was right to flag the messy indentation and the nonstandard use of <conio.h>, and ’s comment about careful indexing is on target.

A compact, safer approach is shown below. It uses a dynamic container, validates the count, accumulates the sum while reading, sorts the data, then computes mean and median correctly (handles even and odd counts):

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    int n;
    std::cout << "Enter number of elements: ";
    if (!(std::cin >> n) || n <= 0) return 1;

    std::vector<double> a;
    a.reserve(n);
    double x, sum = 0.0;
    for (int i = 0; i < n; ++i) {
        std::cin >> x;
        a.push_back(x);
        sum += x;
    }

    std::sort(a.begin(), a.end());
    double mean = sum / n;
    double median = (n % 2 ? a[n/2] : (a[n/2 - 1] + a[n/2]) / 2.0);

    std::cout << "Mean: " << mean << "\nMedian: " << median << '\n';
    return 0;
}

Notes and quick debugging tips: always use an integer type for counts and indexes; initialize accumulators (like sum); read into a[i] (or push into a vector) rather than hard-coded indices; prefer std::sort instead of hand-rolled swaps unless the loop indices are correct; remove nonstandard headers (like <conio.h>) and rely on standard i/o; add braces around loop bodies to avoid accidental scope errors. These changes address the root causes raised in the thread and will compile cleanly on a standard C++ compiler.

Recommended Answers

All 2 Replies

I can't read this code. The indentation is all messed up. You should format it. You should also stick some brackets in to make it crystal clear what's inside the for-loop starting on line 27.

>> it always making some error report.

What's the error message? You should provide it. Here I am guessing it doesn't like conio.h. To hold the execution window open, replace line 43 with

cin.get();

If that doesn't do the trick, add another

cin.get();

below it.

Delete line 3. You can probably delete line 2 as well, though it's harmless even if it doesn't do anything.

Line 39 you should type cast:

median = (element[(int)(nep/2)] + element[(int)(nep/2-1)]) / 2;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.