Thread: Help!

  1. #1
    Registered User
    Join Date
    Dec 2002


    /*function prototypes*/
    void get_choice(int *choice);
    //void calculate_operation(int choice, int num, double largest, double smallest, double total,
    						  //double *numbers);
    //void max_min_sum(double *numbers, int num, double largest, double smallest, double total);
    void standard(double total, int num, double *average);
    void middle(double max, double min, double *median);
    int main(void)
        double *numbers, total, largest, smallest;
    	int choice, num, i;
    	numbers = (double *)malloc(num * (sizeof(double)));
    	printf("How many numbers would you like to enter? Select 2 through 10.\n");
    	scanf("%d", &num);
    	do {
    		printf("Please enter your numbers:\n");
    		scanf("%lf",(&numbers + i++));
    	} while (i<num);
    	largest = *numbers; 
    	smallest = *numbers;
    	total = *numbers;
    	printf("%lf", numbers);
    	for(i=1; i<num; i++)
    		total = total + *(numbers + i);
    		if (smallest>*(numbers + i)) smallest = *(numbers + i);
    		if (largest<*(numbers + i)) largest = *(numbers + i);		
    when it gets to the largest = *numbers; the program gives me a run time error... I can't figure out what is wrong with it! I've been here for days!!!!! help!

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    > scanf("%lf",(&numbers + i++));

    I don't see a direct error at the line you say causes everything to break - Except for this...

    numbers is already a pointer. It references a memory address. When you do &numbers, you reference the address that references another address. Not necessary - scanf does need an address to reference, so usually you use &.. but in this case you don't. So:

    scanf("%lf", (numbers + i++));
    That post-increment there is a bit dodgy too, but hey

    Also, you might want to use code blocks.

    ps: The actual error is caused because when you do this:

    largest = *numbers;

    The address numbers pointed to was overwritten by the first number the user entered in.

    edit: i left the bloody & in the `fixed' version
    .sect signature

Popular pages Recent additions subscribe to a feed