Thread: Re-written code needs help

  1. #1
    Registered User
    Join Date
    Sep 2002

    Re-written code needs help

    Yesterday a thread went up with some code asking for help:
    "A little help for a new guy? Functions problem! "
    I have re-written all the code and it nows works properly for all modes of operation required ( although the code maybe messy, I'm still learning ).
    The only problem is is that the request was in C++ and I only know C. I've re-written the program in C and was hoping someone could just change it to C++ for him. I've done the leg work it just needs a cosmetic work over.

    //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: ");
       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));
    	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?: ");
    	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);
    		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);
    	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])
    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;

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    There's no point in doing all the work for someone, when they are trying to learn something.

    Besides, the conversion of a program from C to C++ can be as simple as changing all the print() calls to cout's, or completely redesigning it in an OOP way.
    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
    Sep 2002
    I figured considering it was so v.wrong I'd profide a correct example to use for reference in the future. I understand what your saying but it was also helpful for me because as I've said I'm still only learning myself ( about a month self taught )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM