A little help for a new guy? Functions problem!

This is a discussion on A little help for a new guy? Functions problem! within the C++ Programming forums, part of the General Programming Boards category; Forgive me for being such a new guy at this and everything, but I cannot seem to get this code ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    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:
    //Made By: Chris M 9/12/2002
    //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;
    
    }
    =====================================
    [code tags added by ygfperson]
    Thanks to anyone willing to take the time to check this mistake up!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if (choose = 'm')
    You need == to do a comparison.

    What's this bit:
    Code:
    {
    return (h + l)/2;
    return t/c;
    
    }
    Please use code tags.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    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.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    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?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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)
    {
        /* Your code here */
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    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;
    }
    http://uk.geocities.com/ca_chorltonkids

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    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.
    http://uk.geocities.com/ca_chorltonkids

  9. #9
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Or, since this is c++, you could use the STL and get built in sorting.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    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.
    http://uk.geocities.com/ca_chorltonkids

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    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:
    //Made By: Chris M 9/12/2002
    //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);
             printf("\nAfter sorting your numbers are:\n");
             for (i=0;i<num;i++)
             	printf("Number %d: %d\n",(i+1),ptr[i]);
             answer = f_med (ptr);
          	printf("The median for your set of numbers will be:%f\n",answer);
          	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;
          }
    }
    http://uk.geocities.com/ca_chorltonkids

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 09:40 AM
  2. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. a british question/joke
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-02-2002, 01:05 PM

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