Thread: So I need some help with this program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    31

    Post So I need some help with this program

    Code: pastebin.com/LdDL6EQ7

    I'm not getting an output file at all. I've linked the DevBloodshed compiler to the Ming library.

    Here's the code at the link above:

    Code:
    #include <stdio.h>
    void sortArray(float a[],int num);
    void printArray(float a[],int num,int ls);
    int readArray(float a[],int num);
    float median_function(float a[],int num);
    float mean_function(float a[],int num);
    main()
    {
     int num=100,ls,count;
     float mean,median,a[num];
     count=readArray(a,num);
     ls=7;
     num=count;
     printArray(a,num,ls);
     mean=mean_function(a,num); 
     sortArray(a,num);
     median=median_function(a,num);
     FILE *oF;
     oF=fopen("output.txt","r");
     fprintf(oF,"d% Scores\nMean: %.2f     Median: %.2f\n\n",count,mean,median);
     ls=6;
     printArray(a,num,ls);
     fclose(oF);
    }
    int readArray(float a[],int num)
    {
     FILE *iF;
     iF=fopen("data5a.txt","w");
     int k;
     int v;
     for(k=0;k<num-1;k++)
     {
       fscanf(iF,"%d",&v);
       a[k] = v;
     }
     return k;
     fclose(iF);
    }
    void printArray(float a[],int num,int ls)
    {
     FILE *oF;
     oF=fopen("output.txt","r");
     fprintf(oF,"NAME");
     fprintf(oF,"Mean and Median of Arrayed Scores(Sorted and Unsorted)\n\n");
     int k;
     for(k=0;k<num-1;k++)
      {
      fprintf(oF,"%4.2f",a[k]);
      if ((k++%ls)==0)
       fprintf(oF,"\n");  
      }
    }
    float mean_function(float a[],int num)
    {
     int k;
     float sum=0;
     for(k=0;k<num-1;k++)
      sum+=a[k];
     return(sum/k); 
    }
    void sortArray(float a[], int num)
    {
     int x,y,temp;
     for(x=0;x<num;x++)
     {
      for(y=0;y<num-1;y++)
      {
       if(a[y]>a[y+1])
       {
        temp=a[y+1];
        a[y+1]=a[y];
        a[y]=temp;
       }
      }
     }
    }
    float median_function(float a[],int num)
    {
     float temp;
     int i,j;
     for(i=0;i<num;i++)
      for(j=i+1;j<num;j++)
      {
       if(a[i]>a[j])
       {
       temp=a[j];
       a[j]=a[i];
       a[i]=temp;
       }
      }
     if(num%2==0)
     return (a[num/2]+a[num/2-1])/2;
     else
     return a[num/2];
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Your 'printArray' function open the file for reading ("r").

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Wow. Thanks. I can't believe I overlooked that.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    So what I'm trying to do here is write a program to read in a set of real scores(max of 100), calculate their mean, sort them, and find their median. Print the scores out(unsorted), 7 per line, on a new line print the count of the scores and on the next line print the mean and median of the scores.
    Then print the sorted array last, 6 per line.


    This is what my code looks like:
    Code:
    #include <stdio.h>
    void sortArray(float a[],int num);
    void printArray(float a[],int num,int ls);
    int readArray(float a[],int num);
    float median_function(float a[],int num);
    float mean_function(float a[],int num);
    main()
    {
     int num=100,ls,count;
     float mean,median,a[num];
     count=readArray(a,num);
     ls=7;
     printArray(a,num,ls);
     mean=mean_function(a,num); 
     sortArray(a,num);
     median=median_function(a,num);
     FILE *oF;
     oF=fopen("output.txt","w");
     fprintf(oF,"d% Scores\nMean: %.2f     Median: %.2f\n\n",count,mean,median);
     ls=6;
     printArray(a,num,ls);
     fclose(oF);
    }
    int readArray(float a[],int num)
    {
     FILE *iF;
     iF=fopen("data5a.txt","r");
     int k=0,e;
     float v;
     e=fscanf(iF,"%f",&v);
     while((e==1)&&(k<num))
     {
      a[k] = v;
      k++;
      e=fscanf(iF,"%f",&v);
     }
     fclose(iF);
     return k;
    }
    void printArray(float a[],int num,int ls)
    {
     FILE *oF;
     oF=fopen("output.txt","w");
     fprintf(oF,"NAME\n\n");
     fprintf(oF,"Mean and Median of Arrayed Scores(Sorted and Unsorted)\n\n");
     int k;
     for(k=0;k<num-1;k++)
      {
      fprintf(oF,"%4.2f  ",a[k]);
      if ((k++%ls)==0)
       fprintf(oF,"\n");  
      }
     fclose(oF);
    }
    float mean_function(float a[],int num)
    {
     int k;
     float sum=0;
     for(k=0;k<num-1;k++)
      sum+=a[k];
     return(sum/k); 
    }
    void sortArray(float a[], int num)
    {
     int x,y,temp;
     for(x=0;x<num;x++)
     {
      for(y=0;y<num-1;y++)
      {
       if(a[y]>a[y+1])
       {
        temp=a[y+1];
        a[y+1]=a[y];
        a[y]=temp;
       }
      }
     }
    }
    float median_function(float a[],int num)
    {
     float temp;
     int i,j;
     for(i=0;i<num;i++)
      for(j=i+1;j<num;j++)
      {
       if(a[i]>a[j])
       {
       temp=a[j];
       a[j]=a[i];
       a[i]=temp;
       }
      }
     if(num%2==0)
     return (a[num/2]+a[num/2-1])/2;
     else
     return a[num/2];
    }
    Output:

    Mean and Median of Arrayed Scores(Sorted and Unsorted)

    -2147483648.00
    -2147483648.00 -2147483648.00 -2147483648.00
    -2147483648.00 -2147483648.00 -2147483648.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 -1.#R
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    0.00 0.00 0.00
    12.00 12.00 45.56
    123.00 217372.00 3347092.00
    1155899277001859600000000000000000.00 2243172109985894800000000000000000.00 2260746989981124400000000000000000.00
    3914097152261827900000000000000000.00

    So there are obviously a lot of things wrong with this program, but I've been working on it for about 8 hours. Sorry, I'm a noob. Need lots of help.







  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Whoa, race horse!

    Let's take it one function at a time:

    *input - you apparently aren't reading in the data properly

    Concentrate on that one aspect (input), and forget the rest of your program, for now. It's useless to work on other functions like sorting, when you don't have the data in the first place.

    Your read array function should (I believe), be putting values into a[index++], and not keep putting them in v.

    If you posted a couple lines of the input, we could be more specific about what you needed.

    Try to work with ONE function at a time, following the flow of your data, as you go: input, computations, and then output.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Thanks for the input, Adak.

    I looked through it a little more patiently (one function at a time) a few times and found several errors. My input is: 1 2 3 4 5 6 6 7 5.67 0.788 .88 .5 123
    There's definitely a '123.00' up there in that post above yours.

    Here's my new output:

    Mean and Median of Arrayed Scores(Sorted and Unsorted)

    1.00 2.00 3.00 4.00 5.00 6.00 6.00
    7.00 5.67 0.79 0.88 0.50 123.00

    13 Scores
    Mean: 3.49 Median: 4.00

    0.00 0.00 0.00 1.00 2.00 3.00
    4.00 5.00 5.00 6.00 6.00 7.00
    123.00

    And the code as of right now:
    Code:
    #include<stdio.h>
    int NUM=100;
    void sortArray(float [],int num);
    void printArray(float [],int num,int ls,FILE *);
    int readArray(float [],int num,FILE *);
    float median_function(float [],int num);
    float mean_function(float [],int num);
    void printA(int,float,float,FILE *);
    main()
    {
     int ls,count,e;
     float mean,median,a[NUM],array;
     FILE *oF,*iF;
     oF=fopen("output.txt","w");
     iF=fopen("data5a.txt","r");
     fprintf(oF,"name\n\n");
     fprintf(oF,"            Mean and Median of Arrayed Scores(Sorted and Unsorted)\n\n");
     array=readArray(a,NUM,iF);
     ls=7;
     count=array;
     printArray(a,array,ls,oF);
     mean=mean_function(a,array); 
     sortArray(a,array);
     median=median_function(a,array);
     printA(count,mean,median,oF);
     ls=6;
     printArray(a,array,ls,oF);
     fclose(oF);
     fclose(iF);
    }
    int readArray(float a[],int array,FILE *iF)
    {
     int k=0,e;
     float v;
     e=fscanf(iF,"%f",&v);
     while(e==1&&k<array)
     {
      a[k]=v;
      k++;
      e=fscanf(iF,"%f",&v);
     }
     fclose(iF);
     return k;
    }
    void printArray(float a[],int num,int ls,FILE *oF)
    {
     int k;
     for(k=0;k<num;k++)
      {
      fprintf(oF,"%4.2f  ",a[k]);
      if ((k+1)%ls==0)
       fprintf(oF,"\n");  
     }
     fprintf(oF,"\n");
    }
    float mean_function(float a[],int num)
    {
     int k;
     float sum=0;
     for(k=0;k<num-1;k++)
      sum+=a[k];
     return(sum/k); 
    }
    void sortArray(float a[], int num)
    {
     int x,y,temp;
     for(x=0;x<num;x++)
     {
      for(y=0;y<num-1;y++)
      {
       if(a[y]>a[y+1])
       {
        temp=a[y+1];
        a[y+1]=a[y];
        a[y]=temp;
       }
      }
     }
    }
    float median_function(float a[],int num)
    {
     float temp;
     int i,j;
     for(i=0;i<num;i++)
      for(j=i+1;j<num;j++)
      {
       if(a[i]>a[j])
       {
       temp=a[j];
       a[j]=a[i];
       a[i]=temp;
       }
      }
     if(num%2==0)
     return (a[num/2]+a[num/2-1])*.50;
     else
     return a[num/2]*1.00;
    }
    void printA(int count,float mean,float median,FILE *oF)
    {
     fprintf(oF,"\n%d Scores\nMean: %.2f     Median: %.2f\n\n",count,mean,median);
    }

    Obviously I'm not asking anyone to write my program for me, but any input would be appreciated.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Input: 1 2 3 4 5 6 6 7 5.67 0.788 .88 .5 123 0 0.00
    152.55552222221111111111111111111111111
    1 2 3 4 5 65 6 7 7 8 9 0 8 7 6 65 5 4 3 21426 1234
    123
    35764


    2



    Output:
    Mean and Median of Arrayed Scores(Sorted and Unsorted)

    1.00 2.00 3.00 4.00 5.00 6.00 6.00
    7.00 5.67 0.79 0.88 0.50 123.00 0.00
    0.00 152.56 1.00 2.00 3.00 4.00 5.00
    65.00 6.00 7.00 7.00 8.00 9.00 0.00
    8.00 7.00 6.00 65.00 5.00 4.00 3.00
    21426.00 1234.00 123.00 35764.00 2.00

    40 Scores
    Mean: 1514.86 Median: 5.34

    0.00 0.00 0.00 0.50 0.79 0.88
    1.00 1.00 2.00 2.00 2.00 3.00
    3.00 3.00 4.00 4.00 4.00 5.00
    5.00 5.00 5.67 6.00 6.00 6.00
    6.00 7.00 7.00 7.00 7.00 8.00
    8.00 9.00 65.00 65.00 123.00 123.00
    152.56 1234.00 21426.00 35764.00



    YAYAYAYAYAYAYAYAYAYAYAY!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!11 Although I'm not sure why the columns are messed up on here. They're not messed up in my text file.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Got it?

    The exclamation marks say "yes!". Congrats!

    The forum software pushes everything text over to the left side, and single spaces multiple blank spaces, as well as other stuff. That's why it's so important to use code tags whenever you post code - it's html gibberish, otherwise. Anytime you want to keep columns lined up on the forum, do a diagram, etc., surround it with code tags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM