1. ## 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. 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.

3. 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. 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();

5. 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. And now a lesson in how to program:
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
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.

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

Quzah.