# A little help for a new guy? Functions problem!

• 09-12-2002
dustmanx
A little help for a new guy? Functions problem!
Forgive me for being such a new guy at this and everything, but I cannot seem to get this code to work right. I'll tell you what's wrong before pasting it all down in one big mess.

This is a school assignment ( we are just getting into C++ (FINALLY! =D) and I have only had experience programming in Pascal. This all seems pretty similar to me, but there are still some wierds about all this. This is a function question...

What seems as if it should be working just isn't for me. It's giving me errors when i goto compile it when things look like they should work. Or at least I think so. So don't laugh at me TOO much when you see this coding x_X.

====================================
Code:

//This program gathers a list from the user and returns
//a median and an average from them.
#include <iostream.h>

float median(float h, float l); //declares the median function

float mean(float t, int c);        //declares averaging function

int main()
{
float x, temp, high, low;
int count, num, quit;
char choose;

quit = 0;
count = 1;
temp = 0;
x = 0;
high = -999999;
low = 9999999;

cout<<"This program will ask the user for a list of numbers, then find the median and/or average."<<endl;
cout<<"How many numbers are you going to put in? > ";
cin>>num;

for (int countdown = 1; countdown <= num; countdown++){

cout<<"Input #"<<count; cout<<" > ";
cin>>x;

if (x < low) //checks to get the highest and lowest nums.
{
low = x;
}

if (x > high)
{
high = x;
}

temp = temp + x; //adds each number put in and stores into one variable.

count = count + 1;

}

count = count - 1;

do{
cout<<"Choose from the following set of options:"<<endl;
cout<<"Get the (m)edian, (a)verage, or (q)uit? > ";
cin>>choose;

if (choose = 'm')
{
cout<<"The median for your set of numbers will be "<<median(high,low);
}

if (choose = 'a')
{
cout<<"The average for your data set will be "<<mean(temp,count);
}

if (choose = 'q')
{
quit = 1;
}

} while (quit = 0); //ends program.

return 0;

}

{
return (h + l)/2;
return t/c;

}
=====================================

Thanks to anyone willing to take the time to check this mistake up!
• 09-12-2002
Hammer
>>if (choose = 'm')
You need == to do a comparison.

What's this bit:
Code:

{
return (h + l)/2;
return t/c;

}

• 09-12-2002
dustmanx
Sorry... new to the board. Not exactly sure what code tags are.

That one bit is something I took note of in the tutorials. I'm not sure of it myself. I was just following the guidelines in the tut's.
• 09-12-2002
Hammer
• 09-12-2002
dustmanx
I will most definately keep this in mind next time ^_^;

Anyway, that one part I suppose is there to make the calculations. It can totally be done much easier by putting that in the coding in the main program, but the assignment calls for us to use functions. Any way you can help me out?
• 09-12-2002
Hammer
Which bit do you want help with?

It looks like you're well underway already... Try writing the functions starting like this (based on the function prototypes you gave):
Code:

float mean(float t, int c)
{
}

• 09-12-2002
crag2804
This still doesn't compile on my machine and doesn't totally fix the code ( I've learnt c not c++ ) but I have tidied it up a bit. Hope this helps:
Code:

#include <iostream.h>

float median(float h, float l); //declares the median function
float mean(float t, int c);        //declares averaging function

int main()
{
float x, temp, high, low;
int count, num,i;
char choose;

count = 1;
temp = 0;
x = 0;
high = -999999;
low = 9999999;

cout<<"This program will ask the user for a list of numbers, then find the median and/or average."<<endl;
cout<<"How many numbers are you going to put in? > ";
cin>>num;

for (i=1;i<=num;i++){

cout<<"Input #"<<count;
cout<<" > ";
cin>>x;

if (x < low) //checks to get the highest and lowest nums.
{
low = x;
}
else if (x > high)
{
high = x;
}

temp += x; //adds each number put in and stores into one variable.

count += 1;

}

count -= 1;

while (choose != 'q') {
cout<<"Choose from the following set of options:"<<endl;
cout<<"Get the (m)edian, (a)verage, or (q)uit? > ";
cin>>choose;

switch (choose) {

case 'm':
cout<<"The median for your set of numbers will be "<<median(high,low);
break;

case 'a':
cout<<"The average for your data set will be "<<mean(temp,count);
break;

default:
break;
}

return 0;
}

• 09-12-2002
crag2804
On thinking about that code you have a problem. You store your numbers by adding them to a value. So if you enterd:
5
4
6
7
The program would only store a value of 22 not the individual values. You wont be able to calculate the averages with that method of storage. You'll need to add the values to an array so that it stores them like this:
my_array[0]=5
my_array[1]=4

Another point is that you'll have to sort the numbers in order to work out the median. The bubble sort algorithm is a simple enough one to understand but not very efficient.
• 09-12-2002
Imperito
Or, since this is c++, you could use the STL and get built in sorting.
• 09-12-2002
crag2804
A patch job for this code is turning into a new program, my eyes are bigger than my belly. PM if you'd like what I've got so far to start a fix.
• 09-13-2002
crag2804
Here is your program almost fully written, it works the only problem is is that it is in C and not C++ ( its all I know, isn't this enough! ). Hope you use it now:
Code:

//This program gathers a list from the user and returns
//a median and an average from them.
//Drastically reformatted to work by Craig Evans ( Crag2804 )
#include <stdio.h>
#include <stdlib.h>
#define SWAP(a,b)  { int t; t=a; a=b; b=t; }  // Macro for swapping

void bubble_srt(int a[], int n);
float f_med(int a[]);
int num = 0,*ptr;

int main()
{
float mean = 0,sum = 0, answer = 0;
int i;
char choose = 'm';

printf("This program will ask the user for a list of numbers.\n");
printf("Then calculate the median or mean average.\n\n");
printf("How many numbers are you going to put in?\n: ");
scanf("%d",&num);

ptr = (int*) calloc ( num,  sizeof (int));    //Creates enough room for the array
if (ptr == NULL) printf("Calloc failed");
else {
for (i=0;i<num;i++)        {                  //Fills the array with inputted values
printf("Number %d:  ",(i+1));
scanf("%d",&ptr[i]);
}

while (choose != 'q') {
while (getchar() != '\n') {}
printf("Choose from the following set of options:\n");
printf("Get the (m)edian, (a)verage, or (q)uit?: ");
scanf("%c",&choose);

switch (choose) {

case 'm':
bubble_srt(ptr, num);
for (i=0;i<num;i++)
printf("Number %d: %d\n",(i+1),ptr[i]);
break;

case 'a':
for (i=0;i<num;i++) {
sum += ptr[i];};
mean = (sum/num);
printf("\nThe mean average for your data set is:%2.2f\n\n",mean);
break;

default:
break;
}
}

}
return  0;

}
void bubble_srt(int a[], int n) {
int i, j;
for(i = 0; i < n; i++)
{
for(j = 1; j < (n-i); j++)
{
if(a[j-1] > a[j])
SWAP(a[j-1],a[j]);
}
}
}

float f_med (int a[]) {
int odd= (num / 2);
float even,neven,average;

if ( (num%2) != 0 ) {
return ptr[odd];}
else {
even = (num/2);
neven = (even + 1);
average = (even + neven)/2;
return average;
}
}