I want to write a program that sorting marks of nth of students in ascending order using an array, and these marks generated randomly from (1 to 100)? How can i get the medain value of these marks? ( it may be even or odd) Like i entered 6 numbers the medain will be 3 and 4(3+4=7/2=3.5) and if i entered 15 numbers, the medain will be 8.

i tried to write it, but for limited number for 10 students. i want nth number of students

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
using namespace std;


void main ()


{
const int size=10;


int array[10],temp;


srand (time(0));



cout<<"\n\n";


for (int i=0;i<size;i++)
{
array= 1 +rand()%100;


cout<<array<<" ";
}


cout<<"\n\n";


for (int i=0;i<size-1;i++)


for (int j=i+1;j<size;j++)


if (array[j]<array)
{
temp = array;
array= array[j];
array[j]= temp;
}


for (int i=0;i<size;i++)


cout<<array<<" ";


cout<<"\n\n";



}

>how can i get the medain value of these marks ? ( it may be even or odd)
If it's odd, take the middle number. If it's even, find the mean of the two middle numbers. It's a cake walk. :)

but this program is for limited number, i need to enter the number first of students.

yes it's easy for an expert not for a beginner like me :'(

Member Avatar for iamthwee

Think about how you would decide whether a set of numbers are either even or odd?

hint: Try looking at the modulus operator.

haven't got the answer, how can i enter nth number of students {if i entered n=10 it will display 10 marks and so on}

i knew that:

if ( number % 2 == 0) {even}
else {odd}, then find the medain of each case.

the problem how to write on that program ><

pls gave me more more explanation pls

Can someone write the same program with correcting it, just how to enter nth number and how to find the medain, plz

i'm not asking too much, plz

>i'm not asking too much, plz
On the contrary. You're asking us to do your work for you. That wastes our time writing pointless programs and it hurts you because you end up learning nothing. Do you still think you're not asking too much?

who said that if u got the answer to ur question means that u end learing nothing, on contrar, it's helped me solving other question similar to it. im not programmer i'm studying electronic, i rather have solution to all problems i have, strating solving, if got stuck problem i go back to the solution and see and the problem. ppls are differ, there is some who can solve by hints and other can't.

sorry to go out side the subject. i asked my question many time but no one answered me.

>who said that if u got the answer to ur question means that u end learing nothing
It's common sense.

>i asked my question many time but no one answered me.
Hmm, perhaps you should ask yourself why that is instead of repeating your question over and over. I'm not the only one with this opinion.

I wrote my question many time hope someone will answer me. And you as a moderator, you should encourage and cheer us not oppose us.

How to reach to the solution is that i asked and other asnwer me until i reach to at least something.

>And you as a moderator, you should encourage and cheer us not oppose us.
As a moderator, the only thing I should do is maintain order. That has no bearing on this conversation. You clearly aren't interested in doing any work because this problem is very simple, even for a beginner. All it takes is a little effort. You haven't shown any effort in helping yourself, so why should I put any effort into helping you?

I wrote that program, what can i change or add on it to enter any number ?

hmm..... To enter nth number means that i should be the same as the size of the array, in this case i don't need to specified the size which is invalid.

>what can i change or add on it to enter any number ?
Try working from this:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

int main()
{
  std::vector<int> v;
  std::vector<int>::size_type size;

  srand( static_cast<unsigned int> ( std::time ( 0 ) ) );

  std::cout<<"Number of items: ";
  
  if ( std::cin>> size ) {
    for ( std::vector<int>::size_type i = 0; i < size; i++ )
      v.push_back ( 1 + rand() % 100 );

    std::sort ( v.begin(), v.end() );

    for ( std::vector<int>::size_type i = 0; i < v.size(); i++ )
      std::cout<< v[i] <<' ';
    std::cout<<'\n';
  }
}

I'm studying C++ now, C is more difficult than C++

I tried to solve from your example and it's work :)

thinx Narue

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main ()

{
const int size=10;

int array[10],temp,n;

srand (time(0));


cout<<"\n\n";


cout<<"Enter the number of student: ";

if (cin>>n)
{

for (int i=0;i<n;i++)
{
array= 1 +rand()%100;

cout<<array<<" ";
}

cout<<"\n\n";

for (int i=0;i<n-1;i++)

for (int j=i+1;j<n;j++)

if (array[j]<array)
{
temp = array;
array= array[j];
array[j]= temp;
}

for (int i=0;i<n;i++)

cout<<array<<" ";

cout<<"\n\n";
}

system ("pause");

}

now, how to find the medain ? i try and post what observe

http://www.daniweb.com/techtalkforums/announcement8-3.html

As for your question, think about this way: how would you go about finding the median with pencil and paper? Write down all the steps. It will become clear when you see the steps you must go through in order to obtain it.

I tried, but couldn't find the answer to find the meddle value of each case. Ok { for odd n+1/2 gave me the middle and for even gives two values n/2, n+2/2 } but these valid if the numbers is in order :(

This program gave random number each time gives varies number.

Need help plz

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main ()

{
       int n;
       int size=n;
       int array[size],temp;

       srand (time(0));


       cout<<"\n\n";


       cout<<"Enter the number of student: ";
       
       
       if (cin>>n)
       {
           cout<<"\n\n";
           for (int i=0;i<n;i++)
           {
               array[i]= 1 +rand()%100;

               cout<<array[i]<<" ";
           }

           cout<<"\n\n";

           for (int i=0;i<n-1;i++)

           for (int j=i+1;j<n;j++)

           if (array[j]<array[i])
           {
               temp = array[i];
               array[i]= array[j];
               array[j]= temp;
           }
           
           for (int i=0;i<n;i++)

           cout<<array[i]<<" ";

           cout<<"\n\n";
       }
       
       
       
     system ("pause");

}

I tried, but couldn't find the answer to find the meddle value of each case. Ok { for odd n+1/2 gave me the middle and for even gives two values n/2, n+2/2 }

So...

  1. Determine whether the number of scores is even or odd
  2. If it's odd, just grab the middle of the list
  3. If it's even, grab the 2 middle elements, and average them

but these valid if the numbers is in order :(

Of course. So find the median after you sort the list.

Thank you, it's looks weird but works :)

This is what I get

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main ()

{
       int n,average;
       int size=n;
       int array[size],temp,medain1,medain2;

       srand (time(0));


       cout<<"\n\n";


       cout<<"Enter the number of student: ";
       
       
       if (cin>>n)
       {
           cout<<"\n\n";
           
           for (int i=0;i<n;i++)
           {
               array[i]= 1 +rand()%100;

               cout<<array[i]<<" ";
           }

           cout<<"\n\n";

           for (int i=0;i<n-1;i++)

           for (int j=i+1;j<n;j++)

           if (array[j]<array[i])
           {
               temp = array[i];
               array[i]= array[j];
               array[j]= temp;
           } 
           
           for (int i=0;i<n;i++)

           cout<<array[i]<<" ";

           cout<<"\n\n";
           
           
           if  (n % 2==0)
           {
               medain1=array[ n / 2 - 1] ;medain2=array[n/2] ;
               
               average= (medain1+medain2)/2;
           
               cout<<"\n\nThe medain= "<<medain1<<setw(5)
               <<medain2<<setw(10)<<"sum= "<<sum<<"\n\n";
           
           }
            
          else  
           {
               medain1= array[(n+2)/2-1];
        
               cout<<"\n\nThe medain= "<<medain1<<"\n\n";
           
            
           }
           
       }
       
       
       
       
     system ("pause");

}
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.