Program question--need help!!!

This is a discussion on Program question--need help!!! within the C Programming forums, part of the General Programming Boards category; Hi, I am TRYING to make a program but am having absolutely NO SUCCESS. I have read the sticky about ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    20

    Program question--need help!!!

    Hi, I am TRYING to make a program but am having absolutely NO SUCCESS. I have read the sticky about no posting homework up but I have exhausted all my resources and have tried a few things but nothing has worked. If any one could post some help or send me an e-mail with help, I would greatly appreciate it!

    The problem states:

    Write a program that:
    Reads an arbitrary but fixed number n (set value of n to 30) of input records from the text file survey.dat each containing survey results consisting of a unique integer identifier for a customer followed by a single integer rating in the range 1-10 inclusive representing that customer's rating for a newly-marketed product. After all values have been read and stored, sort the table of values by customer ID and print to the text file results.dat the following information:

    (a) the table contents sorted by customer ID suitably labeled
    (b) the arithmetic mean of the ratings, suitably labeled
    (c) the harmonic mean of the ratings, suitably labeled
    (d) the median value of the ratings, suitably labeled (the array of survey information will need to be resorted prior to determining the median value, this time using the rating as the sort key)
    (e) the mode of the ratings, suitably labeled, followed by a list of customer IDs whose ratings were equal to that mode value

    I'm so confused and sorry if this creates an inconvenience to anyone, but I have tried everything and don't know what to do!!! PLEASE HELP!!!

    Thanks and your help is greatly appreciated!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if you read the homework post, then you should know you have to show us what you've done before you actually get some help. So where is it? Have some candy.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    This is some of the work I have done so far.

    #include <stdio.h>
    #include <stdlib.h>

    const int n=30;

    struct custInfo {
    int custId;
    int rating;
    }

    int main(void)
    {
    FILE *ifp, *ofp;

    struct custInfo month_info[n];

    int i, arith_mean, harm_mean, median, mode;

    float arithmeticMean(struct custInfo arr[], int size);
    float harmonicMean(struct custInfo arr[], int size);

    if ((ifp=fopen("survey.dat", "r"))==NULL) {
    fprintf(stderr, "Can't open survey.dat for input.\n");
    return 1;
    }

    if ((ifp=fopen("results.dat", "w"))==NULL) {
    fprintf(stderr, "Can't open results.dat for output.\n");
    return 2;
    }

    for (i=0; i<n; i++)
    fscanf(ifp, "%d %d", &month_info[i].custId, &month_info[i].rating);

    arith_mean=arithmeticMean(month_info, n);
    harm_mean=harmonicMean(month_info, n);

    fclose(ifp);
    fclose(ofp);
    return 0;
    }

    float arithmeticMean(struct custInfo arr[], int size)
    {
    int i, average, sum;
    for(i=0; i<n; i++)
    sum=sum+arr[i];
    average=sum/n;
    return average;
    }

    float harmonicMean(struct custInfo arr[], int size)
    {
    int i, denom, harmonicMean;
    for(i=0; i<n; i++)
    denom=denom+(1/arr[i]);
    harmonicMean=n/denom;
    return harmonicMean;
    }

    void idSort(struct custInfo arr[], int n)
    {
    struct custInfo temp;
    int i, j, minIndex;

    for(i=0; i<n; i++) {
    minIndex=i;
    for(j=i+1; j<n; j++)
    if(arr[j].custId<arr[minIndex].custId)
    minIndex=j;
    temp=arr[minIndex];
    arr[minIndex]=arr[i];
    arr[i]=temp;
    }
    }

    void ratingSort(struct custInfo arr[], int n)
    {
    struct custInfo temp;
    int i, j, minIndex;

    for(i=0; i<n; i++) {
    minIndex=i;
    for(j=i+1; j<n; j++)
    if(arr[j].rating<arr[minIndex].rating)
    minIndex=j;
    temp=arr[minIndex];
    arr[minIndex]=arr[i];
    arr[i]=temp;
    }
    }

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    There is another post called the FAQ that expressly states use code tags when posting code. It's pretty simple.

    Code:
    This is in a code block
      See how the formatting is preserved
    	Try one out for yourself
     
    	if (!(code_tags)) 
    	{
    	   IgnorePost();
    	   FlamePoster();
    	   NotifyModerator();
    	   GiveRedBlocksToPosterReputation();
    	 } else BeFriendlyAndHelpful();
    Last edited by VirtualAce; 11-30-2004 at 10:54 AM.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    Just a little work I've done so far (added code tags)...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    const int n=30;
    
    struct custInfo {
    int custId;
    int rating;
    }
    
    int main(void)
    {
    FILE *ifp, *ofp;
    
    struct custInfo month_info[n];
    
    int i, arith_mean, harm_mean, median, mode;
    
    float arithmeticMean(struct custInfo arr[], int size);
    float harmonicMean(struct custInfo arr[], int size);
    
    if ((ifp=fopen("survey.dat", "r"))==NULL) {
    fprintf(stderr, "Can't open survey.dat for input.\n");
    return 1;
    }
    
    if ((ifp=fopen("results.dat", "w"))==NULL) {
    fprintf(stderr, "Can't open results.dat for output.\n");
    return 2;
    }
    
    for (i=0; i<n; i++)
    fscanf(ifp, "%d %d", &month_info[i].custId, &month_info[i].rating);
    
    arith_mean=arithmeticMean(month_info, n);
    harm_mean=harmonicMean(month_info, n);
    
    fclose(ifp);
    fclose(ofp);
    return 0;
    }
    
    float arithmeticMean(struct custInfo arr[], int size)
    {
    int i, average, sum;
    for(i=0; i<n; i++)
    sum=sum+arr[i];
    average=sum/n;
    return average;
    }
    
    float harmonicMean(struct custInfo arr[], int size)
    {
    int i, denom, harmonicMean;
    for(i=0; i<n; i++)
    denom=denom+(1/arr[i]);
    harmonicMean=n/denom;
    return harmonicMean;
    }
    
    void idSort(struct custInfo arr[], int n)
    {
    struct custInfo temp;
    int i, j, minIndex;
    
    for(i=0; i<n; i++) {
    minIndex=i;
    for(j=i+1; j<n; j++)
    if(arr[j].custId<arr[minIndex].custId)
    minIndex=j;
    temp=arr[minIndex];
    arr[minIndex]=arr[i];
    arr[i]=temp;
    }
    }
    
    void ratingSort(struct custInfo arr[], int n)
    {
    struct custInfo temp;
    int i, j, minIndex;
    
    for(i=0; i<n; i++) {
    minIndex=i;
    for(j=i+1; j<n; j++)
    if(arr[j].rating<arr[minIndex].rating)
    minIndex=j;
    temp=arr[minIndex];
    arr[minIndex]=arr[i];
    arr[i]=temp;
    }
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And now a lesson in how to program:
    1) Break the task up into smaller tasks.
    2) Break each smaller task into even smaller tasks.
    3) Goto 2.
    4) When everything is broken down into the smallest steps possible, turn it into code.

    Usually you do your design phase (all of the above) on paper or in an editor or some such, giving you an outline of what exactly you need to do. This commonly is called pseudocode. Something like the following:
    Your task is to get numbers between 1 and 1000 from the user until they wish to stop. Find which of these numbers is the smalles, the largest, and the most frequently occuring.
    So here's some pseudocode:
    Code:
    while not done
        get number from user
        process number
    Code:
    while not done
        prompt for input
        read input into variable
        check variable against smallest
        check variable against largest
        do something to keep track of the actual number inputted
    And so on...

    Then, if you decide you have need of different functions to do the job, you test the functions one at a time. This makes it much easier to get everything working. In the above example, we'd first prompt for a number and read it. When we know that works right, we'd prompt for a number and read it until they entered whatever it was that signaled "quit". Then we'd stop asking and end the program. After that perhaps take the number we've read, and get "smallest" working. Once we know that works, then we'd go for "largest". And so on...

    Now in the above, you have a whole bunch of functions. I personally am not going to sit around debugging all of them for you. Perhaps you could apply some of what I've illustrated above and use this method to figure out what's wrong with your program.


    [edit] Code tags themselves are nice, but if you don't indent your code, what's the point? [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 09:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 06:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21