Thread: Median Help!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Median Help!

    Hello,

    I have been attempting to calculate a median, and I have been hitting my head against the wall for a while now. I would greatly appreciate a pointer in the right direction. Here is the code I have written so far.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <set>
    
    using namespace std;
    
    int main()
    {
    char z;
    float characters=0, dig, di=0, ca=0, cb=0, cc=0, cd=0, ce=0, cf=0, cg=0, ch=0, ci=0, cj=0, ck=0, cl=0, cm=0, cn=0, co=0, cp=0, cq=0, cr=0, cs=0, ct=0, cu=0, cv=0, cw=0, cx=0, cy=0, cz=0;
    float a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, zz;
    ifstream inData;
    FILE *file1;
    file1 = fopen("text1.dat", "r");
    
    //counts specific characters and digits
    while(!feof(file1))
    {
    fscanf(file1,"%c",&z);
    characters++;
    if(z=='A' || z=='a')
    {ca++;
    a=ca;}
    else if(z=='B' || z=='b')
    {cb++;
    b=cb;}
    else if(z=='C' || z=='c')
    {cc++;
    c=cc;}
    else if(z=='D' || z=='d')
    {cd++;
    d=cd;}
    else if(z=='E' || z=='e')
    {ce++;
    e=ce;}
    else if(z=='F' || z=='f')
    {cf++;
    f=cf;}
    else if(z=='G' || z=='g')
    {cg++;
    g=cg;}
    else if(z=='H' || z=='h')
    {ch++;
    h=ch;}
    else if(z=='I' || z=='i')
    {ci++;
    i=ci;}
    else if(z=='J' || z=='j')
    {cj++;
    j=cj;}
    else if(z=='K' || z=='k')
    {ck++;
    k=ck;}
    else if(z=='L' || z=='l')
    {cl++;
    l=cl;}
    else if(z=='M' || z=='m')
    {cm++;
    m=cm;}
    else if(z=='N' || z=='n')
    {cn++;
    n=cn;}
    else if(z=='O' || z=='o')
    {co++;
    o=co;}
    else if(z=='P' || z=='p')
    {cp++;
    p=co;}
    else if(z=='Q' || z=='q')
    {cq++;
    q=cq;}
    else if(z=='R' || z=='r')
    {cr++;
    r=cr;}
    else if(z=='S' || z=='s')
    {cs++;
    s=cs;}
    else if(z=='T' || z=='t')
    {ct++;
    t=ct;}
    else if(z=='U' || z=='u')
    {cu++;
    u=cu;}
    else if(z=='V' || z=='v')
    {cv++;
    v=cv;}
    else if(z=='W' || z=='w')
    {cw++;
    w=cw;}
    else if(z=='X' || z=='x')
    {cx++;
    x=cx;}
    else if(z=='Y' || z=='y')
    {cy++;
    y=cy;}
    else if(z=='Z' || z=='z')
    {cz++;
    zz=cz;}
    else if(z=='0' || z=='1' || z=='2' || z=='3' || z=='4' || z=='5' || z=='6' || z=='7' || z=='8' || z=='9')
    {di++;
    dig=di;}
    }
    //checking logic
    characters--;
    cout << "The letter A occurs " << ca << " times and the ratio to non white space is " << setprecision(3) << (ca/characters) << endl;
    cout << "The letter B occurs " << cb << " times and the ratio to non white space is " << setprecision(3) << (cb/characters) << endl;
    cout << "The letter C occurs " << cc << " times and the ratio to non white space is " << setprecision(3) << (cc/characters) << endl;
    cout << "The letter D occurs " << cd << " times and the ratio to non white space is " << setprecision(3) << (cd/characters) << endl;
    cout << "The letter E occurs " << ce << " times and the ratio to non white space is " << setprecision(3) << (ce/characters) << endl;
    cout << "The letter F occurs " << cf << " times and the ratio to non white space is " << setprecision(3) << (cf/characters) << endl;
    cout << "The letter G occurs " << cg << " times and the ratio to non white space is " << setprecision(3) << (cg/characters) << endl;
    cout << "The letter H occurs " << ch << " times and the ratio to non white space is " << setprecision(3) << (ch/characters) << endl;
    cout << "The letter I occurs " << ci << " times and the ratio to non white space is " << setprecision(3) << (ci/characters) << endl;
    cout << "The letter J occurs " << cj << " times and the ratio to non white space is " << setprecision(3) << (cj/characters) << endl;
    cout << "The letter K occurs " << ck << " times and the ratio to non white space is " << setprecision(3) << (ck/characters) << endl;
    cout << "The letter L occurs " << cl << " times and the ratio to non white space is " << setprecision(3) << (cl/characters) << endl;
    cout << "The letter M occurs " << cm << " times and the ratio to non white space is " << setprecision(3) << (cm/characters) << endl;
    cout << "The letter N occurs " << cn << " times and the ratio to non white space is " << setprecision(3) << (cn/characters) << endl;
    cout << "The letter O occurs " << co << " times and the ratio to non white space is " << setprecision(3) << (co/characters) << endl;
    cout << "The letter P occurs " << cp << " times and the ratio to non white space is " << setprecision(3) << (cp/characters) << endl;
    cout << "The letter Q occurs " << cq << " times and the ratio to non white space is " << setprecision(3) << (cq/characters) << endl;
    cout << "The letter R occurs " << cr << " times and the ratio to non white space is " << setprecision(3) << (cr/characters) << endl;
    cout << "The letter S occurs " << cs << " times and the ratio to non white space is " << setprecision(3) << (cs/characters) << endl;
    cout << "The letter T occurs " << ct << " times and the ratio to non white space is " << setprecision(3) << (ct/characters) << endl;
    cout << "The letter U occurs " << cu << " times and the ratio to non white space is " << setprecision(3) << (cu/characters) << endl;
    cout << "The letter V occurs " << cv << " times and the ratio to non white space is " << setprecision(3) << (cv/characters) << endl;
    cout << "The letter W occurs " << cw << " times and the ratio to non white space is " << setprecision(3) << (cw/characters) << endl;
    cout << "The letter X occurs " << cx << " times and the ratio to non white space is " << setprecision(3) << (cx/characters) << endl;
    cout << "The letter Y occurs " << cy << " times and the ratio to non white space is " << setprecision(3) << (cy/characters) << endl;
    cout << "The letter Z occurs " << cz << " times and the ratio to non white space is " << setprecision(3) << (cz/characters) << endl;
    cout << "The digits 0-9 occur " << di << " times and the ratio to non white space is " << setprecision(3) << (di/characters) << endl;
    
    
    
    
    //************Below this line is the problem area!*****************
    // sort & median
    int size;
    float array[27]={a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, zz};
    int middle = size/2;
    float median;
    if (size%2==0)
    {median = static_cast<float>(array[middle-1]+array[middle])/2;}
    else median = static_cast<float>(array[middle]);
    cout << "The median of the array is:" << median << endl;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Saying
    Code:
    int size;
    does not magically put a value into the variable. Similarly, saying
    Code:
    float array[27] = {a bunch of things that you really shouldn't have in the first place};
    does not magically sort those numbers so that you have them in order.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Saying
    Code:
    int size;
    does not magically put a value into the variable. Similarly, saying
    Code:
    float array[27] = {a bunch of things that you really shouldn't have in the first place};
    does not magically sort those numbers so that you have them in order.
    Yeh, did not help with the process of learning the concept. I am having problems understanding the various sort methods, and I was looking for some help with that. I have been reading many forums, many books, and many how-to guides, and I am still having a problem grasping the concept, which is why I brought the code here. The problem with size is something I recognized previously as well, but thank you for pointing it out again so I wont forget it this time.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, this is a text-based forum. I don't know what we can give you here that you aren't getting from other text-based forums.

    Also, you didn't bring the code here: you brought in an absence of code, which was my point. If you intend to sort, you're going to have to write something to do it. If you really don't know how to sort, sit down with a deck of cards, deal them face down, and work out how to sort that deck in order.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    For this type of a program there is something you should know that will make your program IMMENSELY MORE CLEAR!!

    goto this web page first and read this http://www.asciitable.com/\
    This table describes how characters are really encoded as numbers. So, my knee jerk reaction to a problem like this is to do something like this: (I am using c++ for opening files here)
    NORMALLY NEVER USE CHARS LIKE THIS!!! its usually bad, but I am a rule breaker

    Code:
    int frequency[255]
    
    for(int i(0); i< 255; i++) frequency[i]=0;// init
    ofstream file("text1.dat");
    
    while(file){
         char c;
          file>>c;
         frequency[c]++;// incrment
    }
    now, goto the ascii web page and see where the capital A starts, and add that to where the lower case a starts like this. Make sure to get the difference between the two also
    
    char k = 'a' - 'A';// this is the diference between the two so you can add like this!
    for(char s('A'); s < 'A' + 26; s++){// run 26 times for each letter of the alphabet
        string o = " the frequency of" + s;
       o+="is: "
      cout<<o<<frequncy[s] +frequncy[s+k] ;// this gets the capital and lower case sine the letters translate into numbers
    }
    There you go. Most likely there are alot of errors, but you will get it
    Last edited by smasherprog; 05-04-2010 at 09:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with sorting of array and median!
    By Cali_engr in forum C Programming
    Replies: 2
    Last Post: 04-05-2010, 11:27 PM
  2. Median filter help
    By JTEK24 in forum Tech Board
    Replies: 10
    Last Post: 07-16-2009, 06:05 PM
  3. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  4. Computing Mean, Median and Mode Using Arrays
    By Rodneo in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 11:40 PM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 04:32 PM